Applying Bootstrap styles to RadioButtonList - css

I have an ASP.NET WebForms application that has a RadioButtonList that I'd like to apply Bootstrap's CSS classes to.
Using the RepeatLayout="Flow" and RepeatDirection="Vertical" settings, a RadioButtonList will render like this:
<span id="PageContentPlaceHolder_RadioButtonList1">
<input id="PageContentPlaceHolder_RadioButtonList1_0" type="radio" name="ctl00$PageContentPlaceHolder$RadioButtonList1" value="1" />
<label for="PageContentPlaceHolder_RadioButtonList1_0">One</label>
<br />
<input id="PageContentPlaceHolder_RadioButtonList1_1" type="radio" name="ctl00$PageContentPlaceHolder$RadioButtonList1" value="2" />
<label for="PageContentPlaceHolder_RadioButtonList1_1">Two</label>
<br />
<input id="PageContentPlaceHolder_RadioButtonList1_2" type="radio" name="ctl00$PageContentPlaceHolder$RadioButtonList1" value="3" />
<label for="PageContentPlaceHolder_RadioButtonList1_2">Three</label>
</span>
I'd like it to render like this (using Bootstrap's CSS classes):
<span id="PageContentPlaceHolder_RadioButtonList1">
<div class="form-check">
<input id="PageContentPlaceHolder_RadioButtonList1_0" type="radio" name="ctl00$PageContentPlaceHolder$RadioButtonList1" class="form-check-input" value="1" />
<label for="PageContentPlaceHolder_RadioButtonList1_0" class="form-check-label">One</label>
</div>
<div class="form-check">
<input id="PageContentPlaceHolder_RadioButtonList1_1" type="radio" name="ctl00$PageContentPlaceHolder$RadioButtonList1" class="form-check-input" value="2" />
<label for="PageContentPlaceHolder_RadioButtonList1_1" class="form-check-label">Two</label>
</div>
<div class="form-check">
<input id="PageContentPlaceHolder_RadioButtonList1_2" type="radio" name="ctl00$PageContentPlaceHolder$RadioButtonList1" class="form-check-input" value="3" />
<label for="PageContentPlaceHolder_RadioButtonList1_2" class="form-check-label">Three</label>
</div>
</span>
I realize that this may be easier to do if I use a Repeater and put the individual RadioButton and Label controls within the ItemTemplate, where I could apply the CssClass values. However, I also need to attach a RequiredFieldValidator to ensure that the user makes a selection. Basically it's a multiple-choice exam and I cannot pre-select any of the radio button items. I haven't had much luck getting a validator to work with it individual radio buttons. I suppose a CustomValidator might work if I can come up with the client-side and server-side validation code.
What's the best way to go get the result I'm looking for?

I like using radio buttons as sub page menus. They are really nice, and the code behind works really nice as a result.
So say i have this:
My style sheet is thus this:
body {
}
.radionav input {
width: 12px;
margin-top: 0px;
}
.radionav label {
display: inline-block;
border: 1px solid black;
border-radius: 25px;
height: 35px;
padding-top: 18px;
padding-right: 6px;
margin-left: -20px;
padding-left: 25px;
}
.radionav input[type="radio"]:checked + label {
display: inline-block;
border: 1px solid black;
border-radius: 25px;
height: 35px;
padding-top: 18px;
padding-right: 6px;
margin-left: -20px;
padding-left: 20px;
background: #253B82;
color: white;
border-spacing: 10px;
}
.radionav:after {
content: '';
display: inline-block;
width: 20px;
}
And the above markup for the radio becomes this:
<div style="float:left;padding-right:10px">
<asp:RadioButtonList ID="ShowProjects" runat="server" RepeatLayout="Flow"
AutoPostBack="True" RepeatDirection="Horizontal" >
<asp:ListItem Class="radionav" Selected="True">Live Projects</asp:ListItem>
<asp:ListItem Class="radionav" >All Projects</asp:ListItem>
<asp:ListItem Class="radionav" >My Proofs</asp:ListItem>
<asp:ListItem Class="radionav"> Create (start) a new Project </asp:ListItem>
</asp:RadioButtonList>
</div>
And now we get this:
You can of course tweak the css - perhaps make the buttons a bit longer, and say narrow. I had to really chop up the css, but now I just drop in a radiobutton list, style it and then I have a nice code behind event to work with.
So, My code say might be:
Select Case ShowProjects.SelectedIndex
Case 0
strWhere = "(Void = 0)" ' show live projects
Call LoadGrid(strWhere)
Case 1
strWhere = "" ' show all projects
Call LoadGrid(strWhere)
Case 2
' show proofs
Case 3
' create a project
End Select

Related

input as radio button label with selection changing when input gets focus

I would like to have 2 radio buttons with the default option being a standard radio button with a text label, and the second option having an input field as the label. Without using javascript I wish to have the second radio be selected if the input field receives focus.
The html looks like the following:
<form method="post" action="/">
<div class="some_style">
<input type="radio" name="n1" value="v1" id="radio_1" checked="checked"/>
<label for="radio_1">Option 1</label>
</div>
<div class="some_style">
<input type="radio" name="n2" value="v2" id="radio_2"/>
<label for="radio_2">
<input name="n3" value="v3"/>
</label>
</div>
</form>
The label will not work like this because the text input receives the focus on click.
So, without javascript you will not be able to achieve what you want I fear.
One trick: You could position the label above the input text and hide it if the checkbox is checked.
But: You will then have to click twice to give the text input the focus. To make this reasonable to the user, you eg. could hide the border if the checkbox is unchecked and show it if checked.
#mylabel {
display: block;
#background-color: red;
position: relative;
width: 200px;
height: 2em;
left: 2em;
top: -2em
}
#radio_2:checked~#mylabel {
display: none;
}
#radio_2:not(:checked)~#n3 {
border: none;
}
label {
cursor: pointer;
}
<form method="post" action="/">
<div class="some_style">
<input type="radio" name="n1" value="v1" id="radio_1" checked="checked" />
<label for="radio_1">Option 1</label>
</div>
<div class="some_style">
<input type="radio" name="n1" value="v2" id="radio_2" />
<input name="n3" value="v3" id="n3" />
<label for="radio_2" id="mylabel"></label>
</div>
</form>

Dynamic Ids in User Control to avoid duplication in JavaScript and Asp page

I have an ASP.Net user control, it has a text box and list box, I have given them unique ids and classes, as a user control if I drag it twice or more on asp.net page, it will not work because of same ids when compiled, please see the code below -
$("#liAutoCompleteTextBox").html("<ul class='ecm-autocomp-light'>");
$("#ddlAutoCompleteTextBox > option").each(function () {
if($(this).text().toLowerCase().match(txtVal)) {
$("#liAutoCompleteTextBox").append("<li class='ecm-autocomp-light' onclick=updateToAutoCompleteTextBox('" + encodeURI(this.text) + "')><a onclick=updateToAutoCompleteTextBox('" +encodeURI( this.text) + "')>" + this.text + "</a></li>");
}
});
The user control has script like above, this is just one example.
And in aspx we have,
<label class="input" id="AutoCompleteTextBoxText" runat="server">
<asp:TextBox ID="txtAutoCompleteTextBox" AutoCompleteType="None" autocomplete="off" onfocusout="$('#liAutoCompleteTextBox').fadeOut()" onkeyup="liAutoCompleteTextBoxFunc()" runat="server" CssClass="input-sm isReq isRestrictedText txtAutoCompleteTextBoxCls"></asp:TextBox>
</label>
<label class="select" style="display: none">
<asp:DropDownList ID="ddlAutoCompleteTextBox" ClientIDMode="Static" runat="server" CssClass="input-sm ddlAutoCompleteTextBoxCls">
</asp:DropDownList>
<i></i>
</label>
<div id="liAutoCompleteTextBox" class="customAutoCompDiv" style="position: absolute; display: none; padding: 5px; border: 1px #808080 solid; background: #fff; z-index: 1000; width: 90.5%;">
</div>
I want to make all the ids completely dynamic. Is there any specific control id which I can use with all the names and classes, or is there any other way to handle this?
If you change the liAutoCompleteTextBox div to a Panel (which is a div in html) and put the script inside the UserControl then it will work. When it is a Panel you have access to it's ClientID. Now no matter how much controls you put on the parent page, the javascript will still reference the correct controls.
<!-- begin user control -->
<label class="input" id="AutoCompleteTextBoxText" runat="server">
<asp:TextBox ID="txtAutoCompleteTextBox" AutoCompleteType="None" autocomplete="off" onfocusout="$('#liAutoCompleteTextBox').fadeOut()" onkeyup="liAutoCompleteTextBoxFunc()" runat="server" CssClass="input-sm isReq isRestrictedText txtAutoCompleteTextBoxCls"></asp:TextBox>
</label>
<label class="select" style="display: none">
<asp:DropDownList ID="ddlAutoCompleteTextBox" ClientIDMode="Static" runat="server" CssClass="input-sm ddlAutoCompleteTextBoxCls">
</asp:DropDownList>
<i></i>
</label>
<asp:Panel ID="liAutoCompleteTextBox" runat="server" CssClass="customAutoCompDiv" Style="position: absolute; display: none; padding: 5px; border: 1px #808080 solid; background: #fff; z-index: 1000; width: 90.5%;"></asp:Panel>
<script type="text/javascript">
$("#<%= liAutoCompleteTextBox.ClientID %>").html("<ul class='ecm-autocomp-light'>");
$("#<%= AutoCompleteTextBoxText.ClientID %> > option").each(function () {
if ($(this).text().toLowerCase().match(txtVal)) {
$("#<%= liAutoCompleteTextBox.ClientID %>").append("<li class='ecm-autocomp-light' onclick=updateToAutoCompleteTextBox('" + encodeURI(this.text) + "')><a onclick=updateToAutoCompleteTextBox('" + encodeURI(this.text) + "')>" + this.text + "</a></li>");
}
});
</script>
<!-- end user control -->
What is important to remember that when the Control and the Control.ClientID are in the same Page/UserControl their ID will be correct.

How to create an EditorTemplate for bootstrap checkbox?

I am working with a bootstrap template and its checkbox template is like this:
<div class="checkbox">
<label>
<input type="checkbox" class="checkbox style-1" checked="checked">
<span>Checkbox 1</span>
</label>
</div>
I need an MVC EditorTemplate for boolean values to use this template.
MVC CheckBoxFor default template is not like this template, it has another hidden input field to hold data and there is no span to show checkbox icon (it uses a default icon not stylable by css).
MVC CheckBoxFor default template :
<input checked="checked" data-val="true" data-val-required="required." id="IsActive"
name="IsActive" type="checkbox" value="true">
<input name="IsActive" type="hidden" value="false">
I tried many ways to do this with no success. For example if I use a conditional template like below, it does not return value after submit.
My Boolean EditorTemplate:
#model Boolean?
<div class="checkbox">
<label>
#if (Model.Value)
{
<input id="#ViewData.TemplateInfo.GetFullHtmlFieldId("")" name="#ViewData.TemplateInfo.GetFullHtmlFieldId("")"
type="checkbox" class="checkbox style-1" checked="checked" value="true" />
}
else
{
<input id="#ViewData.TemplateInfo.GetFullHtmlFieldId("")" name="#ViewData.TemplateInfo.GetFullHtmlFieldId("")"
type="checkbox" class="checkbox style-1" value="false" />
}
<span>#Html.LabelFor(m => m)</span>
</label>
</div>
Can anyone help please?
Update:
A part of css codes relevent to checkbox icon :
label input[type=checkbox].checkbox + span:before {
font-family: FontAwesome;
font-size: 12px;
border-radius: 0;
content: " ";
display: inline-block;
text-align: center;
vertical-align: middle;
padding: 1px;
height: 12px;
line-height: 12px;
min-width: 12px;
margin-right: 5px;
border: 1px solid #bfbfbf;
background-color: #f4f4f4;
font-weight: 400;
margin-top: -1px;
}
label input[type=checkbox].checkbox + span:before {
content: " ";
}
label input[type=checkbox].checkbox:checked + span:before {
content: "";
color: #2e7bcc;
}
label input[type=checkbox].checkbox:checked + span {
font-weight: 700;
}
The CheckBoxFor() method generate 2 inputs to ensure a value is posted back (unchecked checkboxes to not submit a value so the hidden input ensures false is submitted), and you should not attempt to change this behavior. Your attempt at an EditorTempate could not work for a number of reasons including a checkbox (which has 2 states) cannot bind to a nullable bool (which has 3 states) and your else block means that a vale of false will always be submitted, even if the checkbox is checked.
Instead, use the CheckBoxFor() method, but adjust your css selectors
<div class="checkbox">
<label>
#Html.CheckBoxFor(m => m.IsActive, new { #class = "checkbox style-1" })
<span>Checkbox 1</span>
</label>
</div>
will generate
<div class="checkbox">
<label>
<input type="checkbox" name="IsActive" class="checkbox style-1" ... value="true">
<input type="hidden" name="IsActive" value="false">
<span>Checkbox 1</span>
</label>
</div>
So your current selector
label input[type=checkbox].checkbox + span:before {
which gets the span element placed immediately after the checkbox element needs to be changed to
label input[type=checkbox].checkbox ~ span:before {
And ditto for the other selectors (i.e. change + to ~). The ~ selector matches the second element if it is preceded by the first, and both share a common parent (refer General sibling selectors)

Add style sheet for a specific browser language

I am trying to obtain a particular style for a input considering the browser language.
<input type="text" runat="server" id="name" onchange="setNameValue" class="name" />
and the css file looks like:
.name
{
width: 120px;
}
i would like this line to be considered for a es-MX(spanish) language, but to not affecting other regions. something like...but is not working
.name:lang("es-MX")
{
width: 120px;
margin-right:6px;
}
Can someone advice?
What about setting the lang attribute to the element you want to target:
<input type="text" runat="server" id="name" onchange="setNameValue" class="name" lang="es" />
You can find more about the lang property at the bottom of this article https://developer.mozilla.org/en-US/docs/HTML/Controlling_spell_checking_in_HTML_forms
Worked added different style class considering user browser language for me with:
name:lang(de)
{
width: 120px;
margin-right:6px;
}
name:lang(en)
{
width: 120px;
margin-right:16px;
}
and <input type="text" runat="server" id="name" onchange="setNameValue" class="name" lang="es"/>

Applying CSS to .NET ImageButton

I have the following CSS that I can apply to an HTML input tag.
#headerSearch
{
width: 265px;
}
#headerSearch .text
{
width: 215px;
}
#headerSearch #searchButton
{
background: url(../images/searchButton.png) no-repeat 0 0;
width: 36px;
border: 1px solid #ccc;
margin: 0;
}
#headerSearch #searchButton:hover
{
background: url(../images/searchButton.png) no-repeat 0 -28px;
}
And the HTML to which I apply it...
<div id="headerSearch" class="float">
<input id="txtSearch" class="text left" type="text" />
<input id="searchButton" class="submit right" type="submit" value="" />
</div>
It works wonderfully.
However, I want to use an ImageButton control instead of the input tag because I want the page submit behavior of the ImageButton (which occurs when you click on it and click event is raised, etc.), but I am not sure how to go about mixing CSS with the ImageButton. I tried something simple like
<asp:ImageButton ID="ibtnSrch" runat="server" CssClass="searchBtn" onclick="ibtnSrch_Click" AlternateText="Search" />
but what occurs is the image displays with a red X in a white box (the default image is missing icon) over top of it.
So, more succinctly, how do I mix elegant CSS with the .NET ImageButton?
based on the sample code you have set the <asp:ImageButton /> CssClass to "searchBtn" but there is no CSS for .searchBtn
perhaps add this to your css
.searchBtn {
background: url(../images/searchButton.png) no-repeat 0 0;
border: 1px solid #ccc;
margin: 0;
}
.searchBtn:hover {
background: url(../images/searchButton.png) no-repeat 0 -28px;
}
an <asp:ImageButton /> renders down to <input type="image" name="ibtnSrch" id="ibtnSrch" class="searchBtn" src="" alt="Search" style="border-width:0px;" />
since the control is an image input with no image source that is why you get a red x
If you change your searchButton style to be a class, then you can just use an <asp:Button>
<asp:Button ID="ibtnSrch" runat="server"
CssClass="submit right searchButton" OnClick="ibtnSrch_Click" />
You can then put that button in a separate ValidationGroup or set CausesValidation="false".
If you want to keep it all client-side and do the redirect to the search page in JavaScript but also want to take advantage of the ASP.NET validation you've set up on your controls, you can use the client-side ASP.NET validation.
In short I would not use the asp image button.
I would use your current html controls and then add some javascript to click a hidden asp:Button control when your submit input is clicked.
<div id="headerSearch" class="float">
<input id="txtSearch" class="text left" type="text" />
<input id="searchButton" class="submit right" type="submit" value="" onclick="<% hiddenSearch.ClientID %>.click();" />
<asp:Button ID="hiddenSearch" runat="server" style="display:none;" />
</div>
I don't quite recall if that is the correct syntax to get the client id...

Resources