Select label based on its 'for' value in c# - c#

I have a asp.net page which contains checkbox controls like below:
<input name="REQUEST_DA_TYPE$0" disabled="disabled" id="REQUEST_DA_TYPE_0" type="checkbox" checked="checked"/>
<label for="REQUEST_DA_TYPE_0">
Text - Accept
<input name="REQUEST_DA_TYPE$1" disabled="disabled" id="REQUEST_DA_TYPE_1" type="checkbox" checked="checked"/>
<label for="REQUEST_DA_TYPE_1">
Text - Refuse
Now I need to dynamically change both label's text in C# page.
Help me to solve this.

Why don't you use server controls instead of native html elements directly? That's one of the main features of ASP.NET, isn't it? So, instead of doing this...
<input name="REQUEST_DA_TYPE$0" disabled="disabled" id="REQUEST_DA_TYPE_0" type="checkbox" checked="checked"/>
<label for="REQUEST_DA_TYPE_0">
<input name="REQUEST_DA_TYPE$1" disabled="disabled" id="REQUEST_DA_TYPE_1" type="checkbox" checked="checked"/>
<label for="REQUEST_DA_TYPE_1">
Why don't you do this...?
<asp:CheckBox ID="REQUEST_DA_TYPE_0" runat="server" Checked="True" Enabled="false" Text="Text - Accept"/>
<asp:CheckBox ID="REQUEST_DA_TYPE_1" runat="server" Checked="True" Enabled="false" Text="Text - Refuse"/>
Then, from code behind you can change whatever you want. For example, the text...
REQUEST_DA_TYPE_0.Text = "I've been changed";
enable it...
REQUEST_DA_TYPE_0.Enabled = false;
the font color...
REQUEST_DA_TYPE_0.ForeColor = System.Drawing.Color.Blue;
etc, etc.

Related

Programmatically put labels in div or label

I would like to set the value of my label (or div) as an element and not text.
Example, this code creates a list of checkboxes as followed in this post How to use Checkbox inside Select Option :
<asp:Label runat="server" id="checkboxesList">
<label for="one">
<input type="checkbox" id="one" />First checkbox</label>
<label for="two">
<input type="checkbox" id="two" />Second checkbox</label>
<label for="three">
<input type="checkbox" id="three" />Third checkbox</label>
</asp:Label>
I need to create my List in CodeBehind and not directly in ASPX I tried :
checkboxesList.Text = "<label for=\"one\"> < input type = \"checkbox\" id = \"one\" /> First checkbox </ label > <label for= \"two\" > < input type = \"checkbox\" id = \"two\" /> Second checkbox </ label >";
Doing so it only prints as a string and do not create the differents labels.
How to implement it from the code behind having just :
<asp:Label runat="server" id="checkboxesList">
</asp:Label>
It's not necessary to use server-side controls to generate all HTML elements. It is possible to use HTML directly for container elements. Use server-side controls only for elements that should be accessible from server code.
<div id="checkboxes">
<label>
<asp:CheckBox ID="one" runat="server" Text="First checkbox" />
</label>
<label>
<asp:CheckBox ID="two" runat="server" Text="Second checkbox" />
</label>
<label>
<asp:CheckBox ID="three" runat="server" Text="Third checkbox" />
</label>
</div>
The snippet allows you to get/set values and text of checkboxes on server-side. Also you will be able to add server-side click event handler for each of the checkbox.
Note, the for attribute is used to bound the <label> with <input> that is not placed inside the <label>. Moreover the attribute is not supported by IE. So, in your case it is possible to place checkboxes into labels instead of using of for attribute.
Also you need to know the HTML id attribute in general case is not equal to ASP ID value. Therefore, if you want to link controls using for attribute, then set id attribute value through ClientID property.
Thank you all for the answers.
I've finally decided to use a CheckBoxList :
<asp:Label runat="server" id="LabelList">
<asp:CheckBoxList runat="server" ClientIDMode="Predictable" DataValueField="Value" DataTextField="Name" ID="specificInfoListControl" Width="150">
</asp:CheckBoxList>
</asp:Label>
And I retrieve the values with :
foreach (ListItem item in specificInfoListControl.Items)
{
if (item.Selected)
{
}
}

Binding data to textbox, making it uneditable

I have a textbox which I would like bound with data from the logged in user, so that the user can't edit it. Would this need to be a label, or is there another way?
<p>Practice:
<input class="form-control" type="text" value="6666666 - Siya's Awesome world" readonly="readonly" id="txtName" />
</p>
Ps. I don't want the values entered as I did above. Your help is appreciated.
Use input like this :
<input readonly="readonly" disabled="disabled" class="text-box single-line readonly" id="field-id" name="field-name" />

How to access value of of input type="text" control of HTML5 in asp.net usercontrol + Umbraco

I am using ascx page. which I am injecting in UMBRACO.
following is my .ascx Code.
<form id="main-contact-form" name="contact-form" method="post" action="#">
<div class="form-group">
<input type="text" runat="server" name="name" id="txtName" class="form-control" placeholder="Name" required />
</div>
<button type="submit" class="btn-primary">Send Message</button>
</form>
I have try to get value of text box (txtName) from back end but I am not abe to access it.
I have tried following method to do so..(Where strtxtName is a string variable)
1) strtxtName = txtName.Value
2) strtxtName = Request.QueryString("txtName")
3) Request.Form.AllKeys -> return Nothing.
But not able to get value of text Box ,
I don't think Umbraco is making any issue.?

asp:controls transformations from html form

I have this code and I want it to transforms it to asp.net controls in label and literal ?
How?
` <input type="text" id="_Email" class="contact-form" name="Email" placeholder="Email address">`
That particular element would actually be a TextBox MSDN TextBox
It would look like this <asp:TextBox runat="server" ID="Email" CssClass="contact-form" />. this would then render <input type="text" id="_Email" class="contact-form" />
The Literal control doesn't produce an HTML element and allows you to add text/html to your page and not wrap it in any other HTML.
The Label control actually outputs a <span></span> and puts your Text in between it.

If condition show error for radio button id

I have following input tags inside table in asp.net
<td class="style18">
<asp:CheckBox ID="chkContentTone" onclick="enableGroupBoxTone()"
runat="server" Text="Content Tone" CssClass="ChkBoxStyle"/>
</td>
<td class="styleCntTon">
<input type="radio" id="rdBtnPositive" name="q2" title="Posotivetitle" value="positive"/>
<label for="rdbtnPositive" class="RadioGroup">Positive</label>
<input type="radio" id="rdBtnNegative" name="q2" title="Negativetitle" value="negative"/>
<label for="rdBtnNegative" class="RadioGroup">Negative</label>
<input type="radio" id="rdBtnNeutral" name="q2" title="Neutraltitle" value="neutral"/>
<label for="rdBtnNeutral" class="RadioGroup">Neutral</label>
</td>
but when I put an if condition in, the radio button's the show an error.
Any ideas why this is occurring?
Try to use Asp.net controls:
<asp:RadioButton runat="server" ... />
The markup you posted does not include server side controls hence those controls cannot be accessed via server side code.
Looking at what you want seems like you would need an asp:RadioButton
once you add a asp:radiobutton you can access these controls in your server side code.
Change this:
<input type="radio" id="rdBtnPositive" name="q2" title="Posotivetitle" value="positive"/><label for="rdbtnPositive" class="RadioGroup">Positive</label>
To this:
<asp:RadioButton id="rdBtnPositive" Label="Positive" runat="server" />
Then you can do:
if(rdBtnPositive.Checked)
{
//code it...
}
You'll need to do that for all three radiobutton's, each should have a unique id and be a server side control, namely asp:RadioButton.
you should use asp:RadioButton to be able to access in code behind.
So basically replace all "input" tags with "asp:RadioButton" and remove non sensible properties like Type.
asp:RadioButton usage example

Categories