Set a CSS class on an ASP.NET RadioButtonList ListItem - asp.net

Is there any way to set a CSS class on an input item in a radio button list? I'd like to reference these form values by class in jQuery.
Given an ASP.NET RadioButtonList with classes set on the ListItems:
<asp:RadioButtonList ID="RadioButtonList" runat="server">
<asp:ListItem class="myClass" Text="Yes" Value="1" />
<asp:ListItem class="myClass" Text="No" Value="0" />
</asp:RadioButtonList>
Will render as:
<span id="RadioButtonList" class="radioButtonListField myClass">
<span class="myClass">
<input id="RadioButtonList_0" type="radio" value="1"
name="RadioButtonList"/>
<label for="RadioButtonList_0">Yes</label>
</span>
<span class="myClass">
<input id="RadioButtonList_1" type="radio" value="0"
name="RadioButtonList"/>
<label for="RadioButtonList_1">No</label>
</span>
</span>
Unfortunately, the "myClass" class is added to the <span> that contains the radio button item, not the <input> itself. How could I get it to look like this instead:
<span id="RadioButtonList" class="radioButtonListField myClass">
<span>
<input id="RadioButtonList_0" class="myClass" type="radio" value="1"
name="RadioButtonList"/>
<label for="RadioButtonList_0">Yes</label>
</span>
<span>
<input id="RadioButtonList_1" class="myClass" type="radio" value="0"
name="RadioButtonList"/>
<label for="RadioButtonList_1">No</label>
</span>
</span>
(This does not even get into the issue of adding the classes to dynamically bound RadioButtonLists.)

Yes, RadioButtonList renders horrible HTML.
Anyway, it's still easy to get them from jQuery.
Try
$('span.myclass input:radio')
Above will get all the radio buttons inside the span with class 'myclass'.

An alternative you may want to try to do is change your CSS class to accomindate the Server Control Rendering.
So in your CSS instead of the following:
.myClass { ... }
You Use this:
.myClass input { ... }
I don't know if you can set the class attribute (via class or CSSClass) in a declarative manner; however, the above should give you a work around.

untested
$('.myclass input:radio').each(function() {
this.css({'style-name':'style-value'})
)};
once the list is rendered, you can possibly manipulate the css client side using the above statement.

Using .NET you could create an event handler to the RowBinding event. This would allow you to access the individual controls within each item in the list. Once you pull the RadioButton control, you could programaticaly set it's CssClass to apply your class on the RadioButton level.
You could also use jquery to find the radio button controls that have myClass applied, and then apply another class to that.
Third, you could just change the definition of MyClass to also specify that it's only applied to input elements under elements that have MyClass applied.

the desigeek response is pretty good..
To get this to work for radiobuttons I had to do this on document.ready()
$('#<%=radRisksMarginTrading.ClientID %>_0').addClass("validate[required]");
$('#<%=radRisksMarginTrading.ClientID %>_1').addClass("validate[required]");
I'm sure there is a much better way to do it by looping but I need a quick fix for 5 radiobutton lists..

Related

How do I attach datepickers to text boxes with the same ID?

I have an ASPX that has a repeater. Inside the repeater is a text box with a JQuery datepicker attached to it. The text box's ID is static, so it is the same for all of those items within the repeater. However, when I make a change to the second or subsequent repeater item's date box using a datepicker, it changes the first item's date box and leaves the one actually attached to the selected datepicker alone.
Other than the obvious solution of "don't define items with the same ID," which results in another problem that will probably end up in another question, is there a way to fix this problem?
This example
<asp:Repeater runat="server"
ID="rptWhatever"
ClientIDMode="Predictable">
<ItemTemplate>
<asp:TextBox runat="server"
ID="tbxWhoCares"
CssClass="ImaDatePicker">
</asp:TextBox>
</ItemTemplate>
</asp:Repeater>
Will render the textbox to something like:
<input id="tbxWhoCares" type="text" class="ImaDatePicker"/>
So regardless of the id you should be able to do this in your script:
$(".ImaDatePicker").datepicker();
The problem with non-unique ID's is a big one but should be an easy fix if you set the attributes in the Repeater to ClientIDMode="Predictable" and leave the same attribute in the TextBox to its default.
Update
It seems jquery UI datepicker requires a unique id.
So you can select multiple inputs using a common css class but the id's must be unique otherwise you run into that problem.
It shouldn't work that way...but it does.
Alternatively you could just use the native <input type="date" ... />
$(function() {
$(".ImaDatePicker").datepicker();
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.13.2/themes/base/theme.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.13.2/themes/base/jquery-ui.min.css" rel="stylesheet"/><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.13.2/jquery-ui.min.js"></script>
<input id="tbxWhoCares" type="text" class="ImaDatePicker" />
<input id="tbxWhoCares" type="text" class="ImaDatePicker" />
<input id="tbxWhoCares" type="text" class="ImaDatePicker" />
<br><br><br>
<input id="tbxWhoCares_1" type="text" class="ImaDatePicker" />
<input id="tbxWhoCares_2" type="text" class="ImaDatePicker" />
<input id="tbxWhoCares_3" type="text" class="ImaDatePicker" />

Styling ASP.net controls with bootstrap

I am trying to style my quiz with checkbox/radio button lists controls in asp.net to match that of the bootstrap static variants found here: http://getbootstrap.com/javascript/#buttons-checkbox-radio
This works fine using the following lines of code:
<asp:RadioButtonList ID="rblQuestion1" runat="server" RepeatDirection="Vertical" RepeatLayout="Flow" CssClass="btn-group" data-toggle="buttons">
<asp:ListItem class="btn btn-default radio-inline" Value="1">Answer One</asp:ListItem>
<asp:ListItem class="btn btn-default radio-inline" Value="2">Answer Two</asp:ListItem>
</asp:RadioButtonList>
But i have three problems:
The items are rendered in steps like so: http://i.imgur.com/aM1A1Gh.png, I believe the cause to be the "RepeatLayout=Flow" property, but without this element isn't displayed as shown on the bootstrap website http://getbootstrap.com/javascript/#buttons-checkbox-radio
When displayed on a smaller screen, some of the items do not fit as the text within them is quite long, what would be the best way to fix this? Image: http://imgur.com/OpCSbLD
and lastly:
After the form is submitted, it postbacks to the page disabling the control via VB code, and highlighting the correct answer. However, this removes the element styling
This is the HTML displayed when taken from the browser:
<span id="ContentMain_wizQuestions_rblQuestion1" disabled="disabled" class="aspNetDisabled btn-group" data-toggle="buttons">
<span class="aspNetDisabled">
<input id="ContentMain_wizQuestions_rblQuestion1_0" type="radio" name="ctl00$ContentMain$wizQuestions$rblQuestion1" value="1" disabled="disabled"><label for="ContentMain_wizQuestions_rblQuestion1_0">Answer One</label></span><br>
<span class="aspNetDisabled"><input id="ContentMain_wizQuestions_rblQuestion1_1" type="radio" name="ctl00$ContentMain$wizQuestions$rblQuestion1" value="2" disabled="disabled">
<label for="ContentMain_wizQuestions_rblQuestion1_1">Answer Two</label></span><br>
<span class="aspNetDisabled"><input id="ContentMain_wizQuestions_rblQuestion1_2" type="radio" name="ctl00$ContentMain$wizQuestions$rblQuestion1" value="3" checked="checked" disabled="disabled">
<label for="ContentMain_wizQuestions_rblQuestion1_2">Answer Three</label></span><br>
<span class="aspNetDisabled"><input id="ContentMain_wizQuestions_rblQuestion1_3" type="radio" name="ctl00$ContentMain$wizQuestions$rblQuestion1" value="4" disabled="disabled">
<label for="ContentMain_wizQuestions_rblQuestion1_3">Answer Four</label>
</span>
I believe the aspNetDisabled span tag, is interferring with the bootstrap styles. How can i make them persist through this, or is there an alternative better way to disable an element?
Thanks
Read the following MSDN document
That is because of controlRenderingCompatibilityVersion. If your framework version is above 4, then this property will be set default to "pages controlRenderingCompatibilityVersion="4.0"
Change the controlRenderingCompatibilityVersion="3.5" and you can see class="aspNetDisabled" will be removed from html.
Web Control facility after .NET version 3.5
Method 1 :
please add following in your web.config for remove the aspNetDisabled tag
<system.web>
<pages enableEventValidation="true" controlRenderingCompatibilityVersion="3.5" />
</system.web>
Method 2 :
you can also ended up doing the following, which effectively removes the insertion of the extra class for disabled items.
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
WebControl.DisabledCssClass = "";
}

ASP.net server control checkbox misunderstanding

I am trying to use the server control for an asp checkbox:
<asp:CheckBox ID="generalInformation"
ClientIDMode="Static"
class="SetupChecklist"
name="generalInformation"
CssClass="SetupChecklist"
runat="server" />
However it does not seem to be retaining the class, which I need it to do. Am I missing something?
Here is how it renders on the web page:
<input type="checkbox"
name="ctl00$ContentPlaceHolder1$generalInformation"
id="generalInformation">
ah.......
this is the problem:
runat="server" checkbox is render to span + input...
the span gets the class not the input....
<span class="SetupChecklist" class="SetupChecklist" name="generalInformation">
<input id="generalInformation" type="checkbox" name="generalInformation" /></span>
You might want to use the CssClass property instead:
<asp:CheckBox ID="generalInformation"
ClientIDMode="Static"
name="generalInformation"
CssClass="SetupChecklist"
runat="server" />
Edit: I've only just seen that you've already tried it. Ok, CssClass actually applies the class to a span that wraps the check box, so try this instead:
generalInformation.InputAttributes("class", "SetupChecklist");
It will render the HTML like below.
<span class="SetupChecklist" class="SetupChecklist" name="generalInformation">
<input id="generalInformation" type="checkbox" name="generalInformation" />
</span>
Assuming that you are trying to design the input tag.
Now your style sheet can be like below for the <input> tag
<style type="text/css">
.SetupChecklist input
{
border:0px;
}
</style>
Originally, the span tag was occupying the class information.
remove the class attribute and just use the CssClass

Disabling a checkbox in asp.net

I am dynamically creating a CheckBox and setting the disabled property like this:
chk.Disabled = false.
This renders the following HTML:
<input type="checkbox" disabled="disabled" .../>
On the click of the checkbox, I have a JS function that tries to enable it by doing a:
//get reference to the checkbox
chk.disabled = false;
This does not work. Any help?
If your checkbox is disabled, your onclick wont be called
What you're trying to do is a bit odd. You're trying to enable a checkbox by clicking on the checkbox, which is disabled to start with. So, the onclick will not be registered until the checkbox is actually enabled.
Try the below to see what I mean.
<html>
<body>
<input id="cb" type="checkbox" disabled="disabled" onclick="this.disabled=!this.disabled;" />
<label for="cb">Click me</label>
<input type="button" value="Click me instead!" onclick="cb.disabled=!cb.disabled;" />
</body>
</html>
Hope that helps you out.
How are you dynamically creating the check box?
Keep in mind that ASP.NET will change the name of the check box you give it, especially if you add it on the code behind.
What you need to do is send to your JS function the clientId, which is the id the HTML item will get when render.

for attribute - standards wise

How this code should be (asp.net standard)?
<label for="jobno">Job#</label>
<input id="TextBox_JobID" name="jobno" runat="server"/>
<input type="button" value="Show Job" class="fbutt"
id="Button_showJob" onserverclick="Button_showJob_ServerClick"
runat="server" />
</p>
<p>
<asp:Label ID="Label_error" runat="server" class="error"></asp:Label></p>
I think the for attribute is not ok, or not written in correct way?
The value of the for attribute must match the id of a form control (such as an input element or a select element), not the name.
As the textbox is marked runat="server", I would suggest using the ClientID property of the control:
<label for="<%=TextBox_JobID.ClientID%>">Job#</label>
Then if you use master pages/user controls etc, you can be sure it will always contain the right value.
It should probably read
<label for="TextBox_JobID">Job#</label>

Resources