dpreznik
dpreznik

Reputation: 247

How to access a data key?

I have a GridView with textboxes in its cells. A JavaScript function is called on changing textbox's contents. I want to pass to the function a data key of the row to which the textbox belongs, but don't know how I can do that.

The following gives me an error message:

 <asp:TextBox ID="txtWFShipDate" runat="server"
        Text='<%#Eval("StrShipDate") %>' BorderStyle="None" Width="80%"
        CssClass="date"
        onchange="JSSaveWorkflowChanges(this, '<%# Eval("BookStemCommon") %>');">
 </asp:TextBox>

BookStemCommon is the data key name.

Upvotes: 0

Views: 398

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

Try like this:

onchange='<%# string.Format("JSSaveWorkflowChanges(this, \"{0}\");", Eval("BookStemCommon")) %>'

or a better and safer way using JavaScriptSerializer to properly encode the value that will be passed to the javascript function:

onchange='<%# string.Format("JSSaveWorkflowChanges(this, {0});", new JavaScriptSerializer().Serialize(Eval("BookStemCommon"))) %>'

Upvotes: 1

Related Questions