I have a gridview with a bunch of column templates. One of these columns is a hidden column that has a value of 0 or 1. If the grid line is changed, I set the value to 1, otherwise I leave the default value of 0.
The template field is defined as:
<asp:TemplateField> <ItemTemplate> <input type="hidden" id="rowIsChanged" runat="server" /> </ItemTemplate> </asp:TemplateField>
My problem (without unnecessary details), I have a field where I call the javascript function and do some client-side validation. Inside this function, I would like to access this hidden field (rowIsChanged) for this particular row and set the value to 1 (in javascript). That way, when I click the Refresh button on my server side, I can verify that the update has occurred, since I have a status check for rowIsChanged==1 .
So my question is, I have something about this right now:
<asp:TemplateField> <ItemTemplate> <asp:TextBox id="txtMyDate" runat="server" onchange="DoSomeValidation(this)" /> </ItemTemplate> <asp:TemplateField>
And javascript:
function DoSomeValidation(myParam) { //do some validation... //here is where I need to access the hidden field 'rowIsChanged' and set the value to "1" }
So, how can I access this rowIsChanged row field and set it to "1".
Edit
More details:
Asp markup:
<asp:TemplateField HeaderText="Date" SortExpression="LineItemDate"> <ItemTemplate> <ajaxToolkit:CalendarExtender TargetControlID="txtMyDate" ID="CalendarExtender3" runat="server"></ajaxToolkit:CalendarExtender> <asp:TextBox ToolTip="Line Item Date" Width="60px" ID="txtMyDate" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.MyDate") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField>
The problem is that when I first load the page and load the grid data, the system automatically assumes that the change was made on the date (the value of the hidden field is 1). But no changes were made, this is just the initial load ... Therefore, I do not know how to deal with this.
In the gridview row binding event, I have the following code snippet:
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") ....
So, I posted the code for the rowdatabound event, as well as the markup ... but as soon as the page loads with the NO changes made in the txtMyDate field, and I click the Refresh button on my page that does something with this:
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
It seems that each row has a value of 1, as if the row had already been updated (onchange got a call ...). But this is not so. This works great for any other field that does not use a calendar control. For example, if I comment out a line:
tLID.Attributes.Add("onchange", "document.getElementById('" + hiddenField.ClientID + "').value=1")
And just leave onchange for all my other fields, and then click the Refresh button, it works as expected (that is, if I changed the value in the fields, then the hidden field has a value of 1, and if I do not change the values ββin the field have a value 0). But the problem is only in this field ...
Edit 2
Just for testing, I even tried this:
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 <TRIGGERS> 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();"
All I did was add a js function called helloworld () that looks like this:
function helloworld() { alert ("hello world"); }
I run my project, and immediately it notifies the world hello (I have 2 lines in the grid, so it twice informs the world hello). This is without any changes to the data, the question is why onchange is called without any changes? This must be due to the way it sets the date from the data returned from the database, this is considered an onchange event when it sees the markup as follows:
<asp:TextBox ToolTip="Line Item Date" ID="txtMyDate" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.MyDate") %>' />
In particular, DataBinder.Eval(Container, "DataItem.MyDate")...
Edit # 3.Possibly a possible reason
@Tim
I decided to make the small change mentioned in your comments that you populated txtMyDate statically by assigning it a value (instead of pulling the value from the database, as I did). So instead:
<asp:TextBox ToolTip="Line Item Date" ID="txtMyDate" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.MyDate") %>'
Just for a giggle, I changed it to this:
<asp:TextBox ToolTip="Line Item Date" Width="60px" ID="txtMyDate" runat="server" Text="1/1/2011">
That is, I set the value statically, just like you ... I run my project and now the warning does not work, therefore there is no onchange , which is probably why you could not reproduce the error. So it looks like this:
<asp:TextBox ToolTip="Line Item Date" ID="txtMyDate" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.MyDate") %>'
onchange event, which is not very good for me: (... so the question is how to still handle this?
Change 4 !!!
Sorry for all the changes, I'm trying to eliminate the possibilities here ... So, now I did the removal of this line:
<ajaxToolkit:CalendarExtender TargetControlID="txtMyDate" ID="CalendarExtender3" runat="server"></ajaxToolkit:CalendarExtender>
Value I removed the calendar pointer from the markup, and the onchange event no longer fires on a load event. Why did this have to happen. Even if I leave this:
<asp:TextBox ToolTip="Line Item Date" ID="txtMyDate" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.MyDate") %>'>
While I delete the calendar, it works fine ... Did I mention that this gridview is inside the updated panel? Could this be a problem? The problem is that I do not want to delete the calendar. I would like users to be able to select a date and then enter it.