Change the value of a selected radio button on pageload - asp-classic

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();" ?

Related

Bind selected radio button value in KnockOut - ASP.net project

I'm new to KnockOut and I just wanted to bind selected radio button value to td. For my knowledge I didn't do any mistake but my following code doesn't work anymore. Please help me to solve this minor mistake.
Other code parts are working fine in the same page, So nothing wrong other than this code part.
<td data-bind="text: Sex"></td>
<td>
<asp:RadioButton ID="RadioButtonMale" value="Male" GroupName="RadioGroup1" data-bind="checked: Sex" runat="server" Text="Male"/>
<asp:RadioButton ID="RadioButtonFemale" value="Female" GroupName="RadioGroup1" data-bind="checked: Sex" runat="server" Text="Female"/>
</td>
<script type="text/javascript">
var ViewModel = {
Sex : ko.observable("Male"),
};
ko.applyBindings(ViewModel);
</script>
The html that asp .net is generating is incorrect. It needs to have data-bind="checked: Sex" on the input instead of the span. Instead of using <asp:RadioButton do the following:
<input type="radio" ID="RadioButtonMale" value="Male" Name="RadioGroup1" data-bind="checked: Sex" runat="server"><label for="RadioButtonMale">Male</label>
<input type="radio" ID="RadioButtonFemale" value="Female" Name="RadioGroup1" data-bind="checked: Sex" runat="server" Text="Male"><label for="RadioButtonFemale">Female</label>

Set value on ASP:RadioButton

I know this has been working for me in the past, but my attempt to set the value of my radiobutton is ignored and the value is set to the ID of my radio button.
ASP.Net
<asp:RadioButton ID="rb" runat="server" />
Code behind
//Test 1
rb.InputAttributes.Add("value", "foo");
//Test 2
rb.InputAttributes["value"] = "foo";
HTML output
<input id="rb" type="radio" name="rb" value="rb" />
What am I missing here?
Setting the value in ASP.Net markup is working, but I rather do this from codebehind.
You can simple do:
rb.Attributes.Add("value", "foo");
HTML output:
<input id="ContentPlaceHolder1_rb" type="radio" name="ctl00$ContentPlaceHolder1$rb" value="foo">
Code behind :
rb.Attributes.Add("value", "RadioButton");
Output
<input id="rb" type="radio" name="rb" value="RadioButton" />
Reference:
Set HTML Attributes for Controls in ASP.NET Web Pages

ASP.NET CheckListBox 508 Compliance

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

HTML Radiobuton values in ASP.NET

I'm having some real problems with a group of HTML Radiobuttons when I try to pass the selected values back and forth between JavaScript and ASP.NET.
<input type="radio" id="radio1" name="markerSel" value="1" checked=true />
<input type="radio" id="radio2" name="markerSel" value="2" />
<input type="radio" id="radio3" name="markerSel" value="3" />
To begin with, the form loads with these radiobuttons and a user selects a value and submits the ASP.NET form. To get the value of the selected option on the server-side I have tried;
string selVal = Request.Form["markerSel"];
//always returns "1", regardless of the selection made.
And also tried ASP.NET's FindControl method (recursively too), and I just get null.
I want to be able to set the value of this radiobutton group via JavaScript and Asp.NET and be able to read from both environments.
Any help would be appreciated.
Thanks
Is there a reason why you don't use ASP.NET radio buttons instead of <input type=radio />?
If you use
<asp:RadioButton id="radio1" runat="server" Checked="true" />
<asp:RadioButton id="radio2" runat="server" />
<asp:RadioButton id="radio3" runat="server" />
You will be able to access them server-side by doing this.radio1.Checked
If you want to access your elements via javascript, you can do a document.getElementById('radio1') and you will get the DOM element that way.
If you want to use jQuery, you can access the radio buttons with
$('#radio1')
If you want to test if the radio button is checked, you can use
if ($("#radio1").is(':checked')) {//...
hmmm... I would try changing the line to:
string selVal = Request.Form("markerSel");

Building a query string based radiobutton values

I'd like to build a query string based on values taken from 5 groups of radio buttons.
Selecting any of the groups is optional so you could pick set A or B or both. How would I build the querystring based on this? I'm using VB.NET 1.1
The asp:Radiobuttonlist control does not like null values so I'm resorting to normal html radio buttons. My question is how do I string up the selected values into a querystring
I have something like this right now:
HTML:
<input type="radio" name="apBoat" id="Apb1" value="1" /> detail1
<input type="radio" name="apBoat" id="Apb2" value="2" /> detail2
<input type="radio" name="cBoat" id="Cb1" value="1" /> detail1
<input type="radio" name="cBoat" id="Cb2" value="2" /> detail2
VB.NET
Public Sub btnSubmit_click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim queryString As String = "nextpage.aspx?"
Dim aBoat, bBoat, cBoat bas String
aBoat = "apb=" & Request("aBoat")
bBoat = "bBoat=" & Request("bBoat")
cBoat = "cBoat=" & Request("cBoat ")
queryString += aBoat & bBoat & cBoat
Response.Redirect(queryString)
End Sub
Is this the best way to build the query string or should I take a different approach altogether? Appreciate all the help I can get. Thanks much.
The easiest way would be to use a non-server-side <form> tag with the method="get" then when the form was submitted you would automatically get the querystring you are after (and don't forget to add <label> tags and associate them with your radio buttons):
<form action="..." method="get">
<input type="radio" name="apBoat" id="Apb1" value="1" /> <label for="Apb1">detail1</label>
<input type="radio" name="apBoat" id="Apb2" value="2" /> <label for="Apb2">detail2</label>
<input type="radio" name="cBoat" id="Cb1" value="1" /> <label for="Cb1">detail1</label>
<input type="radio" name="cBoat" id="Cb2" value="2" /> <label for="Cb2">detail2</label>
</form>
You could use StringBuilder instead of creating those three different strings. You can help it out by preallocating about how much memory you need to store your string. You could also use String.Format instead.
If this is all your submit button is doing why make it a .Net page at all and instead just have a GET form go to nextpage.aspx for processing?

Resources