title: access hidden field within gridview control to set a value in javascript?
body: <p>I've got a gridview with a bunch of template columns.  One of those columns is a hidden column that either has a value of 0 or 1.  If a row in a grid view is changed I set the value to 1, otherwise I leave the default 0.</p>
  
  <p>The templatefield is defined as:</p>
  
  <pre><code>&lt;asp:TemplateField&gt;
        &lt;ItemTemplate&gt;
            &lt;input type="hidden" id="rowIsChanged" runat="server" /&gt;
        &lt;/ItemTemplate&gt;
   &lt;/asp:TemplateField&gt;
  </code></pre>
  
  <p>My issue is (without too much details), I've got a field where I call a javascript function and do some validation on the client side.  Inside of this function I'd like to be able to access this hidden field (rowIsChanged) for this specific row and set the value to 1 (in javascript).  That way when I click my server side 'Update' button I can ensure the update occurs given I have a condition checken for <code>rowIsChanged==1</code>.</p>
  
  <p>So my question, I have something to this effect right now:</p>
  
  <pre><code>&lt;asp:TemplateField&gt;
    &lt;ItemTemplate&gt;
      &lt;asp:TextBox id="txtMyDate" runat="server" onchange="DoSomeValidation(this)" /&gt;
    &lt;/ItemTemplate&gt;
  &lt;asp:TemplateField&gt;
  </code></pre>
  
  <p>And the javascript:</p>
  
  <pre><code>function DoSomeValidation(myParam)
   {
     //do some validation...
  
     //here is where I need to access the hidden field 'rowIsChanged' and set the value to "1"
   }
  </code></pre>
  
  <p>So how can I access this particular row's rowIsChanged field and set it to "1".</p>
  
  <h2>Edit</h2>
  
  <p>Here is more details:</p>
  
  <p>The asp markup:</p>
  
  <pre><code>&lt;asp:TemplateField HeaderText="Date" SortExpression="LineItemDate"&gt;
        &lt;ItemTemplate&gt;
          &lt;ajaxToolkit:CalendarExtender TargetControlID="txtMyDate" ID="CalendarExtender3" runat="server"&gt;&lt;/ajaxToolkit:CalendarExtender&gt;
          &lt;asp:TextBox ToolTip="Line Item Date"  Width="60px"  ID="txtMyDate" runat="server" Text='&lt;%# DataBinder.Eval(Container, "DataItem.MyDate") %&gt;'&gt;&lt;/asp:TextBox&gt;
        &lt;/ItemTemplate&gt;
     &lt;/asp:TemplateField&gt; 
  </code></pre>
  
  <p>The issue is when I first load the page and load the grid data the system automatically assumes a change was made to the date (value of hidden field is 1).  But no changes were made it is just an initial load...So I dont know how to handle this.</p>
  
  <p>In the gridview's row databound event I have the following snippet of code:</p>
  
  <pre><code>If e.Row.RowType = DataControlRowType.DataRow Then
              Dim hiddenField As HtmlInputHidden = DirectCast(e.Row.FindControl("rowIsChanged"), HtmlInputHidden)
  
              Dim tLID As TextBox = CType(e.Row.FindControl("txtMyDate"), TextBox)
  
              tLID.Attributes.Add("onchange", "document.getElementById('" + hiddenField.ClientID + "').value=1")
   ....
  </code></pre>
  
  <p>So I've posted code for the rowdatabound event as well as the markup..but as soon as the page loads with <strong>NO</strong> edits done to the field <code>txtMyDate</code> and I click the "Update" button on my page which does something to this effect:</p>
  
  <pre><code>  For Each Row As GridViewRow In Me.gvLineItems.Rows
              Dim hiddenField As HtmlInputHidden = DirectCast(Row.FindControl("rowIsChanged"), HtmlInputHidden)
              'only update rows that have been edited / update IsDirty=1 :)
              If (hiddenField.Value = "1") Then
  </code></pre>
  
  <p>Every row seems to have the value 1 as if the row was already updated (onchange got called...). But this is not the case.  This works fine for any other field that is not using the calendar control.  For instance, if I comment the line:</p>
  
  <pre><code>tLID.Attributes.Add("onchange", "document.getElementById('" + hiddenField.ClientID + "').value=1")
  </code></pre>
  
  <p>And just leave the onchange for all my other fields and then click the "Update" button it works as expected (that is if I change the value in the fields then the hidden field has the value 1 and if i dont change the values in the field they have the value 0).  But the issue is just this one field...</p>
  
  <h1>Edit 2</h1>
  
  <p>Just for testing I even tried this:</p>
  
  <pre><code>Protected Sub gvLineItems_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvLineItems.RowDataBound
          If e.Row.RowType = DataControlRowType.DataRow Then
              'this is used to add a trigger to the &lt;TRIGGERS&gt; tag for the update panel
              'I could not do it in the HTML / ASP code because the button is inside of a gridview control
              Dim hiddenField As HtmlInputHidden = DirectCast(e.Row.FindControl("hdnIsChanged"), HtmlInputHidden)
  
              'the line item date
              Dim tLID As TextBox = CType(e.Row.FindControl("txtMyDate"), TextBox)
  
              'just a test...
              tLID.Attributes("onchange") = "helloworld();"
  </code></pre>
  
  <p>All I did was add a js function called helloworld() that looks like this:</p>
  
  <pre><code> function helloworld()
     {
     alert ("hello world");
     }
  </code></pre>
  
  <p>I run my project and right away it alerts out hello world (I have 2 rows in the grid so it alerts out hello world twice).  This is without any change whatsoever to the data, the question is why is onchange called without any change?  It must have to be with how it is setting the date from the the data returned from the database, is that considered an onchange event when it sees markup like so:</p>
  
  <pre><code>&lt;asp:TextBox ToolTip="Line Item Date" ID="txtMyDate" runat="server" Text='&lt;%# DataBinder.Eval(Container, "DataItem.MyDate") %&gt;' /&gt;
  </code></pre>
  
  <p>Specifically the <code>DataBinder.Eval(Container, "DataItem.MyDate")...</code></p>
  
  <h1>Edit #3..Could be possible cause</h1>
  
  <p>@Tim</p>
  
  <p>I decided to make a small change, you mentioned in your comments you filled in the txtMyDate statically by assigning a value to it (rather then pulling the value from the database like I did).  So instead of this:</p>
  
  <pre><code>&lt;asp:TextBox ToolTip="Line Item Date" ID="txtMyDate" runat="server" Text='&lt;%# DataBinder.Eval(Container, "DataItem.MyDate") %&gt;'
  </code></pre>
  
  <p>Just for giggles I changed it to this:</p>
  
  <pre><code>&lt;asp:TextBox ToolTip="Line Item Date"  Width="60px"  ID="txtMyDate" runat="server" Text="1/1/2011"&gt;
  </code></pre>
  
  <p>That is I set the value <em>statically</em> like you did...I run my project and now the alert doesn't fire, hence no <code>onchange</code>, this is probably why you could not reproduce the error.  So it appears this:</p>
  
  <pre><code>&lt;asp:TextBox ToolTip="Line Item Date" ID="txtMyDate" runat="server" Text='&lt;%# DataBinder.Eval(Container, "DataItem.MyDate") %&gt;'
  </code></pre>
  
  <p>Causes the <code>onchange</code> event to fire, which for me is not good :(...so question is how to still handle this??</p>
  
  <h1>Edit 4!!!</h1>
  
  <p>Wow sorry for all the edits, I am trying to eliminate possibilities here...
  So now what I did was remove this line:</p>
  
  <p><code>&lt;ajaxToolkit:CalendarExtender TargetControlID="txtMyDate" ID="CalendarExtender3" runat="server"&gt;&lt;/ajaxToolkit:CalendarExtender&gt;</code></p>
  
  <p>Meaning I removed the calendarextender from the markup and the onchange event no longer fires on the load event.  Why would this be causing this to happen.  Even If I leave this:</p>
  
  <p><code>&lt;asp:TextBox ToolTip="Line Item Date"  ID="txtMyDate" runat="server" Text='&lt;%# DataBinder.Eval(Container, "DataItem.MyDate") %&gt;'&gt;</code></p>
  
  <p>As long as I eliminate the calendarextender it works fine...Did I mention this gridview is inside an updatepanel?  Could this be the issue??  The problem is I do not want to remove the calendarextender...I would like to be able to allow users to select a date from that rather then typing it in.</p>