ASP.NET CheckListBox 508 Compliance - asp.net

I have a CheckListBox and specify the ToolTip on the items. When rendered, it shows a span around the checkbox (and label) and it's the span that has he title attribute on it, not the checkbox (input type=checkbox).
Does anyone know how to set the title attribute on the input element instead of the surrounding span?
In order to be 508 compliant, I need to have the title attribute on the input element, not on the span.
Edit: Note that I would prefer to do all the necessary changes in C# on the server side. I would prefer not to have to do it in javascript/jquery.

I should have tested before I posted. The quickest way is to use old fashioned html controls with the attribute runat="server". You can then reference your individual checkboxes by their IDs. For example:
<asp:Panel ID="BrowserCheckBoxList" runat="server">
<ul>
<li><input id="CheckBoxList1_0" type="checkbox" name="" value="Chrome" title="Chrome" runat="server" /> <label for="CheckBoxList1_0">Chrome</label></li>
<li><input id="CheckBoxList1_1" type="checkbox" name="" value="FireFox" title="FireFox" runat="server" /> <label for="CheckBoxList1_1">FireFox</label></li>
<li><input id="CheckBoxList1_2" type="checkbox" name="" value="IE" title="IE" runat="server" /> <label for="CheckBoxList1_2">IE</label></li>
<li><input id="CheckBoxList1_3" type="checkbox" name="" value="Opera" title="Opera" runat="server" /><label for="CheckBoxList1_3">Opera</label></li>
<li><input id="CheckBoxList1_4" type="checkbox" name="" value="Safari" title="Safari" runat="server" /><label for="CheckBoxList1_4">Safari</label></li>
</ul>
</asp:Panel>

Been there. JavaScript wouldn't help you with 508 anyway since it doesn't change the source code.
This should fix it for you:
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
foreach(ListItem li in CheckBoxList1.Items){
li.Attributes["title"] = "my title";
}
}

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 = "";
}

How to handle multiple checkboxes in ASP.NET WEb Form and Code-Behind File

Here is the Code in the .aspx web form What is the best way to handle a group of multiple checkboxes.
<input id="nonunionexempt" type="checkbox" value="0" name="employeeType" tabindex="8" runat="server" />
<input id="nonexempthourly" type="checkbox" value="1" name="employeeType" tabindex="9" />
<input id="eleven99" type="checkbox" value="2" name="employeeType" tabindex="10" />
<input id="nysna" type="checkbox" value="3" name="employeeType" tabindex="11" />
<input id="cir" type="checkbox" value="4" name="employeeType" tabindex="12" />
Here is the code behind file Is there a better way to deal with multiple checkboxes?
protected void SaveEmployee()
{
Employee model = new Employee();
if (nonunionexempt.Checked)
{
model.EmployeeType = nonunionexempt.Value;
}
if (nonunionexempt.Checked)
{
model.EmployeeType = nonexempthourly.Value;
}
IValueProvider provider = new FormValueProvider(ModelBindingExecutionContext);
if (TryUpdateModel<Employee>(model, provider))
{
LoaRepository.saveData(model);
}
else
{
throw new FormatException("Could not model bind");
}
}
First, your code sounds good and clean. But if I were you I would use web controls instead of HTML ones. Also, I don't think there is a need to write the if statements even if not using web controls. Simply assign the 'checked' property which is of value true or false. Finally, if embedding the check boxes in a web user control then in the case of multiple usages this could bring a valuable help and of course better maintenance. Hope it helps, good luck!
If you can use the ASP.NET Checkbox control instead of input tags, you won't need the if statements. In code behind use checkBoxID.Checked property which will return true or false.

How to send text to content page as query string from Master Page

I have a search textbox that I tried wrapping in a form element:
<form id="searchForm" method="GET" action="Search.aspx">
<input name="term" type="text" id="searchTerm" class="nav-search" size="30" />
<input type="submit" value="submit" style="display:none;"/>
</form>
I can't get it to redirect to the content page (Search.aspx) with the query string (term) from the Master Page.
I tried wrapping it in an ASP:Panel and using the DefaultButton Property but that wouldn't call the Click event in code behind.
I'm perplexed as I've searched for a solution for hours and this seems like a such common task. Thanks.
Change your code to this
<input name="term" type="text" id="searchTerm" runat="server" class="nav-search" size="30" />
<asp:Button ID="searchsubmit" OnClick="searchsubmit_Click" runat="server" Text="Search" />
in Site.Master.cs add something like this
protected void searchsubmit_Click(object sender, EventArgs e)
{
Response.Redirect("~/search.apsx?content=" + searchTerm.text);
}
This will allow to search with button click, if you want to hide it, add also panel around first code, saying that this is a button to automatically press when enter is pressed Something like this:
<asp:Panel ID="UserPanel" runat="server" DefaultButton="searchsubmit">

Change the value of a selected radio button on pageload

I have the following code that renders two radio buttons for the selections of Yes and No. On pageload, No is always selected, but I need Yes to be the default (I'm not the best at this type of coding, I've tried some things and my research hasn't clued me in). How can I adjust it? Thanks in advance.
<tr>
<td> Residential?
<input type="radio" name="Residential" value="0" onclick="document.QuoteInfo.save.value='0';document.OrderPanel.submit();" <%If Not Session("oDE").Fieldvalue("QUOTES","Residential") Then Response.Write " checked "%> />
No
<input type="radio" onclick="document.QuoteInfo.save.value='0';document.OrderPanel.submit();" name="Residential" value="1" <%If Session("oDE").Fieldvalue("QUOTES","Residential") Then Response.Write " checked "%> />
Yes
</td>
</tr>
Okay. If I understand correctly you don't need asp blocks to handling default checked attribute. Try to add manually.
<input type="radio" name="Residential" value="0" onclick="document.QuoteInfo.save.value='0';document.OrderPanel.submit();" /> No
<input type="radio" onclick="document.QuoteInfo.save.value='0';document.OrderPanel.submit();" name="Residential" value="1" checked="checked" /> Yes
Edit:
I edited because you need to asp blocks. onclick="document.QuoteInfo.save.value='0';document.OrderPanel.submit();" this is for both of no and yes. At least, for checkbox yes's onclick attribute could be onclick="document.QuoteInfo.save.value='1';document.OrderPanel.submit();" ?

Resources