How can I set two values in HtmlInput control
I have an HtmlInputText
in an EditItemTemplate
in a DataGrid
, Th开发者_如何学Cis control is bound with a value from the SqlDataSource
I want to set in this control two values the JOB_CODE_ID
and the JOB_CODE
instead of just JOB_CODE
.
Note: I don't want to show JOB_CODE_ID
, just save it to use it later in code behind.
I used to use Tag
in the WinForms to set values such this, but in I don't know similar way in ASP.Net.
In my situation I can't use a hidden control to save the JOB_CODE_ID
there, Is there any way to set two values in a HtmlInputText
control ?
The code:
<input type="text" ID="JOB_CODETextBox" runat="server"
value='<%# Bind("JOB_CODE") %>' />
Thanks in advance.
@A_Nablsi, Edit: The JOB_CODE_ID will be used along with the input value in JS function triggered on clicking the input.
You can use a hidden field:
<input type="hidden" value="<%# Bind("JOB_CODE_ID") %>" id="JOB_CODE_ID" />
If you want to add extra data to an existing tag, you can use the Html5 data- tags. These work also in the older browsers:
<input type="text" ID="JOB_CODETextBox" runat="server" value='<%# Bind("JOB_CODE") %>' data-id='<%# Bind("JOB_CODE_ID") %>' />
You can access the data-id
attribute, just like any other attribute.
You could save the other value in the control's name in the rowdatabound event.
Private Sub gvJOB_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvJOB.RowDataBound
If Not (e.Row.RowType = DataControlRowType.DataRow) OrElse (e.Row.Cells(0) Is Nothing) Then
Return
End If
Dim dr As DataRowView = CType(e.Row.DataItem, DataRowView)
Dim JOB_CODETextBox As HtmlInputText = CType(e.Row.FindControl("JOB_CODETextBox "), HtmlInputText)
JOB_CODETextBox.name = dr("JOB_CODE_ID")
End Sub
<input type="text" ID="JOB_CODETextBox" runat="server" value='<%# Bind("JOB_CODE") %>'
onclick='<%# "YourJSFunction(this," + Eval("JOB_CODE_ID") + ")" %>' />
Your js function should look like
function YourFuncName(sender, args){
// the args contains the id you need.
}
You have to trap the DataGrid's ItemDatabound event and format the two values together into the textbox. You use FindControl to get the Textbox, then you can concatenate the values and assign them into the Text property. Here is a link to a similar operation on Code Project:
http://www.codeproject.com/KB/webforms/ItemCreated.aspx
精彩评论