How can I set two values in HtmlInput control - c#

I have an HtmlInputText in an EditItemTemplate in a DataGrid, This 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

Related

Added tag HyperLink in aspx page without GridView in c#

On MarkUp in my aspx page form I have these two TextBox :
<asp:TextBox ID="Mtl" runat="server" ReadOnly="true" Enabled="false"></asp:TextBox>
<asp:TextBox ID="ps" runat="server"></asp:TextBox>
The HTML view for these two TextBox is :
<input name="Mtl" type="text" value="901" readonly="readonly" id="Mtl" disabled="disabled" />
<input name="ps" type="text" id="ps" />
Now I need insert next to the TextBox with id ps the HyperLink where passed in querystring the value of TextBox with id Mtl, the value is 901.
I need pass this value for working in another aspx page.
I have tried this solution but the HyperLink is not clikable :
<asp:HyperLink ID="HlLink" runat="server"
NavigateUrl='<%# String.Format("~/box.aspx?v={0}&e={1}&l={2}", "y", "IC", HttpUtility.UrlEncode(Eval("Mtl").ToString())) %>'
ImageUrl="~/Images/edit_icon.gif" Target="_blank" Text="Mtl"></asp:HyperLink>
In this aspx page I don't have GridView, maybe it does not work for this reason ?
How to do resolve this ?
Please help me, thank you so much in advance.
Yes you are correct since your control is not inside a gridview (or any databoundcontrol for that matter) that's why it will not work.
Actually, <%# %> is called data bind expressions and they are evaluated for data bound controls only. For your HyperLink control to work with this code nugget you will have to explicitly call the DataBind method on that control like this:-
protected void Page_Load(object sender, EventArgs e)
{
HlLink.DataBind();
}
You can bind a jQuery 'Change' event on your textbox. And in this event you can set correct navigation url by picking up the value from the textbox and appending it in appropriate place in your query string. Its a fairly easy solution. If you don't know how to do it I can provide you a sample.

Find input value and associated html label text in asp.net

I have the following setup:
<label for="<%= inpUserName.ClientID%>">Email</label>
<asp:TextBox id="inpUserName" runat="server" />
As you may noticed the label is not asp's label. How do I get the value of the input and the text of the label?
Edit
I am trying to loop through the posted form keys and get associated labels.
You could use <asp:Label> directly, just need to specify AssociatedControlID
<asp:Label ID="UsernameLabel"
Text="Email"
AssociatedControlID="inpUserName"
runat="server">
<asp:TextBox id="inpUserName" runat="server" />
Make the label server-side:
<label runat="server" id="lblEmail" for="<%= inpUserName.ClientID%>">Email</label>
Then you can access its text in code behind like this:
// label text
string labelText = lblEmail.InnerText;
// input value
string inputValue = inpUserName.Text;
change to:
<label runat="server" id="ClientID" for="<%= inpUserName.ClientID%>">Email</label>
You can find the textbox using the Id var txt = inpUserName.Text; for the label you can't, since its not a server side control and its not a request variable (input with value for instance) then you can't, unless you make it server side too and access in the same manner.

Changing value of input element without changing element id

I have an ASPX page with an input element. On page load I set the value of that input element. Later on I try to get the contents of that field using the elements id(using javascript) but it fails because the id has been changed. Is there any way to stop C# from changing the elements id?
input element:
<input id="conferenceDate" runat="server" style="width:100%;" />
Setting the date in the Page_Load function:
conferenceDate.Value = DateTime.Now.ToString();
The id after the element vale has been set:
<input name="ctl00$PlaceHolderMain$conferenceDate" type="text" id="ctl00_PlaceHolderMain_conferenceDate" style="width:100%;" value="7/21/2014 11:30:55 AM" />
UPDATE: I have tried the following but neither work the id is still changed:
<asp:TextBox ID="conferenceDate" runat="server" ClientIDMode="Static" />
and
When I try to select them later with jQuery I have to use the altered id:
$("ctl00_PlaceHolderMain_conferenceDate").val()
In addition to setting the ClientIDMode to static, you can also find the id using the ClientID property.
<%= conferenceDate.ClientID %>
Using your sample information you can find it like this:
Single or double quotes and dont forget the hash mark (or dot if its a class).
$("#<%= conferenceDate.ClientID %>").val()
Try setting the ClientIDMode of the Textbox to "Static" (More info: http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode(v=vs.110).aspx):
<input id="conferenceDate" runat="server" ClientIDMode="Static" style="width:100%;" />
You can set the ClientIDMode to Static.
This will prevent the controls ID from changing at runtime.
E.g:
<asp:TextBox ID="conferenceDate" runat="server" ClientIDMode="Static" />
Why not to use the asp:textbox and set the ClientIDMode to static?
<asp:TextBox ID="conferenceDate" runat="server" ClientIDMode="Static"></asp:TextBox>

The server tag is not well formed.(databinder.eval)

I am trying to working the following code.
<asp:DataGrid ID="Grid" runat="server" DataKeyField="KeyID" CssClass="grid"
...
<asp:CheckBox runat="server" ID="checkBox-<%#DataBinder.Eval(Container.DataItem,"KeyID")%>" AutoPostBack="false"></asp:CheckBox>
When I run the code,I have got the below error:
Error 25 The server tag is not well formed.
Note : This is working without runat="server".
What is the remedy for this problem ?
You don't need to set the ID of the CheckBox to do what you want to do (change the background color). Your CheckBox should look like this (I added the KeyID as the text value if you want to display it... or you can just remove that if you only want the checkbox):
<asp:CheckBox runat="server" ID="checkbox" Text='<%# Eval("KeyID") %>' AutoPostBack="false"></asp:CheckBox>
Now your checkbox will render something like this:
<input id="MainContent_Grid_checkbox_0" type="checkbox" name="ctl00$MainContent$Grid$ctl02$checkbox" /><label for="MainContent_Grid_checkbox_0">Value of KeyID</label>
Since all the names end with "checkbox", you can apply a function on the change event for those elements whose name ends with "checkbox". You didn't specify that this was a JavaScript question or if you are using jQuery... this answer uses jQuery:
<script>
$('input[name$="checkbox"]').change(function () {
if ($(this).is(':checked')) {
$(this).parent().css('background-color', 'yellow');
}
else {
$(this).parent().css('background-color', 'white');
}
});
</script>
That will determine if the checkbox is checked, and if so it will set the background-color of its parent (the <td> that it is in, inside the DataGrid rendered HTML), depending on the value.
Likewise, you can go up to the next parent() and highlight the entire row.
Resources:
jQuery Selectors
OnCheckedChanged -- an event that you would process in the code behind, not in JavaScript.

Why I can't set a value on a asp:CheckBox?

There isn't the attribute Value :
<asp:CheckBox ID="CheckBox1" runat="server" />
while on standard HTML this is allowed :
<input type="checkbox" ID="CheckBox1" value="My Valyue" />
why?
The Text property is used to render a label for the checkbox.
The control has an InputAttributes property that you can add to:
myChk.InputAttributes.Add("value", "My Value");
I believe that if you simply add the value attribute to the markup, this will also get populated.
You can access the value like so:
myChk.InputAttributes["value"];
To answer the question of why Value is not a build in attribute to the CheckBox control:
A CheckBox in isolation (just by itself) needs no value. By definition it is a boolean and is identified by its ID. All you need to do is check whether it was checked or not.
The value comes into play when you group checkboxes and there is a control for that - the CheckBoxList that uses ListItem - each ListItem does have a Value property.
Instead of using the asp:CheckBox control, use the html input checkbox, and run it at the server.
<input type="checkbox" id="ck" runat="server" value='<%# Eval("Value") %>' />
<asp:Label ID="lbl" runat="server" AssociatedControlID="ck" Text='<%# Eval("Name") %>'></asp:Label>
Now you can reference it from codebehind as an HtmlInputCheckBox (my latest example is inside a repeater, so I can decorate this substitute for a checkbox list with other elements, like a tool tip image).
foreach (RepeaterItem repeaterItem in repCheckboxes.Items)
{
HtmlInputCheckBox listItem = (HtmlInputCheckBox)repeaterItem.FindControl("ck");
if (listItem.Checked)
{
string val = listItem.Value;
...
I know this does not answer the "why" of the OP, but this comes up high in searches for this exact problem, and this is a good solution. As for why, I think MS goofed by leaving it out, since you don't have control over the html in a CheckBoxList

Categories