0

I have the same issue as that of a previous poster in which I need to set a session variable prior to page load. Because pageload fires before my gridview.RowCommand, I am trying to use this method found in the article below which incorporates the use of a hiddenfield. My ultimate goal is to remove the onRowCommand event and to use the solution presented in the article. I have been using firebug to debug my javascript but everytime I get an error "is undefined" when I look at this.parent (this is a td and this.parent should be the tr). According to firebug, this.parent.cells[2].textContent is how I get to the OID value I want. As I spend little time writing javascript, any help would be very much appreciated.

Problem Line of Code from JS

this.parent.cells[2].textContent

My GridView

<asp:GridView ID="gridViewOpenOrders" CssClass="gvOpenOrders" runat="server" AutoGenerateColumns="False"
            AllowPaging="True" RowStyle-BorderStyle="None"
            DataKeyNames="AccountNumber,OrderID"
            Width="802px" OnRowCommand="gridViewOpenOrders_RowCommand">
            <Columns>
                <asp:ButtonField Text="Select" HeaderStyle-Width="40px" ItemStyle-CssClass="assignOrderID"/>
                <asp:BoundField DataField="OrderDate" HeaderText="Date" SortExpression="OrderDate"
                    DataFormatString="{0:d}">
                    <HeaderStyle Width="100px" />
                </asp:BoundField>
                <asp:BoundField DataField="OrderID" HeaderText="OID" SortExpression="OrderID">
                    <HeaderStyle Width ="40px" />
                </asp:BoundField>             
            </Columns>
            <HeaderStyle CssClass="ColumnHeaders" />
            <RowStyle CssClass="ColumnRows" />
            <SelectedRowStyle BackColor="#D9D9FF" />
        </asp:GridView>

My Javascript:

<script type="text/javascript">
    $(document).ready(function() {
        $(".assignOrderID").click(function () {
            var testing = $(".assignOrderID")
            alert(this.parent)
            alert(this.parent.cells[2])
            alert(this.parent.cells[2]).textContent
            document.getElementById('ContentPlaceHolder1_formViewOrder_hiddenFieldOrderID').value = this.parent.cells[2].textContent
            alert('hi2');
        });
    });
</script>

Page Load Code:

Dim t As HiddenField = formViewOrder.FindControl("hiddenFieldOrderID")
Session.Add("OrderID", t.Value)

Research: Setting a session variable before AutoPostBack fires in an asp:Button

Community
  • 1
  • 1
Chance
  • 172
  • 1
  • 13

1 Answers1

0

First, your HiddenField ClientIdMode property should be static. Then you can get the HiddenField like this:

HidddenField hdn = this.Page.FindControl("gridViewOpenOrders").FindControl("hiddenFieldOrderID") as HiddenField
Adi Lester
  • 24,731
  • 12
  • 95
  • 110
Mehmet
  • 1,824
  • 3
  • 19
  • 28
  • When debugging what I have is working fine for pulling out the hidden field. If I set the hiddenfield via javascript the value is pulled out properly with what I have in page load. My problem is with the following line in the javascript document.getElementById('ContentPlaceHolder1_formViewOrder_hiddenFieldOrderID').value = this.parent.cells[2].textContent If I do document.getElementById('ContentPlaceHolder1_formViewOrder_hiddenFieldOrderID').value = "t" This works. Therefore my problem is with "this.parent.cells[2].textContent" – Chance Jul 30 '12 at 19:47
  • ok ı understood. but what is "this" in your code.. I think it is your – Mehmet Jul 30 '12 at 20:15
  • I'm sorry for the confusion, the "this" command references a . (The this command in JS not VB) Therefore "this.parent" should reference the , cells[2].textContent should therefore reference the third cell's value which is my OrderID. This is how I get to the value I want via firebug however this always displays as "undefined" for this.parent when I run the code. To be specific, according to an alert, this = object HTMLTableDataCellElement. – Chance Jul 30 '12 at 20:44