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
Related
i have a RadGrid and it has a template column which contains a asp:CheckBox and a label , so label has a "for" Attribute which the value is equal to the ID of the CheckBox so i want to set the CheckBox ID to the "for" attribute for the label
this scenario is to change the style of checkbox to Material Design of Google,
and my asp.net is C# ,
i did this scenario in VB language i want to do it in C#
and that's the code that i have did in VB and Works Great ==>
<telerik:GridTemplateColumn AllowFiltering="false">
<ItemTemplate>
<asp:CheckBox ID="cbxSelect" runat="server" onClick="checkboxClicked(event, 'cbxSelect')"></asp:CheckBox>
<label for="<%# DirectCast(Container, GridViewRow).FindControl("cbxSelect").ClientID %>"></label>
</ItemTemplate>
</telerik:GridTemplateColumn>
Any Idea?
This may also work: ((GridViewRow)Container).FindControl("cbxSelect").ClientID
Sounds a bit cluttered, but basically I have a databound repeater. On the ASP side, I have this:
<asp:Label ID="Label2" runat="server" Text='<%#Eval("uMessage") %>'></asp:Label>
I'm using the same template for 4 different datasets, and for 2 of them this should be a hyperlink and for the other 2 it shouldn't. So, I'm guessing you have to add a hyperlink programmatically in the code-behind? Has anyone ever done something like this?
Easiest way without all kinds of code-behind and therefor less code fragmentation, I would say you need a property that is set based on your condition prior to data binding.
protected bool LinkVisible { get; set; }
Then you just do this:
<asp:Label ID="Label2" runat="server" Text='<%#Eval("uMessage") %>' Visible="<%# !LinkVisible %>"></asp:Label>
<asp:HyperLink ID="Link" runat="server" Visible="<%# LinkVisible %>" ><%#Eval("uMessage") %></asp:HyperLink>
This sets the Visible for either the Label or the HyperLink. Visible false means it won't even get rendered. In your markup you can see that there will be a label or a hyperlink and no special things popup from the code behind.
You don't need to add the property LinkVisible, but can do the condition there too.
yes it is possible in code behind on DataItem bound
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl = (Label)e.Row.FindControl("Label2");
if (lbl.Text == "your condition")
{
HyperLink yourLink = (HyperLink)e.Row.FindControl("yourID");
yourLink.enabled = false;
}
}
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>
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.
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