Getting value of Radio buttons with Master Pages - asp.net

I have an aspx page that uses a master page. On this page I have a group of radio buttons. I need to get at the checked property for each of the elements in the radio button group in javascript. Before the page was converted to use a master page, the following javascript code worked just fine:
if((!f.rblRegUseVehicles[0].checked) && (!f.rblRegUseVehicles[1].checked))
{
isValid = "false";
}
f is the reference to the form. This worked just fine.
When the page was chanded to use a Master Page, I changed the javascript to the following:
if((!f.<%=rblRegUseVehicles.ClientID%>[0].checked) && (!f.<%=rblRegUseVehicles.ClientID%>[1].checked))
{
isValid = "false";
}
Now, the javascript is failing because it can't find the element. In the "View Source" I have elements with the name:
<input id="ctl00_cphContent_rblRegUseVehicles_0" type="radio" name="ctl00$cphContent$rblRegUseVehicles" value="Yes" />
<input id="ctl00_cphContent_rblRegUseVehicles_1" type="radio" name="ctl00$cphContent$rblRegUseVehicles" value="No" />
The only code that works is
document.<%=Form.ClientID%>.<%=rblRegUseVehicles.ClientID%>_0.checked
I want the javascript to reference the array like before the page was converted to use a Master Page. How can I do that?

This is a part of ASP.NET that has always been difficult. You need to have static javascript that references dynamically created element ids!
This is how I have always solved the problem:
Wrap your RadioButtonList in a div or p or whatever you want:
<div id="yourId">
<asp:RadioButtonList id="radiolist1" runat="server">
<!-- ... -->
</asp:RadioButtonList>
</div>
Which renders to something like this:
<div id="yourId">
<table id="radiolist1" border="0">
<tr>
<td>
<input id="radiolist1_0" type="radio" name="radiolist1" value="Item 1" checked="checked" />
<label for="radiolist1_0">Item 1</label>
</td>
</tr>
<tr>
<td>
<input id="radiolist1_1" type="radio" name="radiolist1" value="Item 2" />
<label for="radiolist1_1">Item 2</label>
</td>
</tr>
</table>
</div>
And this would allow you to have a static javascript function that targets yourId which doesn't get runat="server". Then your javascript would look something like this:
var rbl = document.getElementById("yourId");
isValid = (!rbl.getElementsByTagName("input")[0].checked
&& !rbl.getElementsByTagName("input")[1].checked);

If you want to explore the idea of using jQuery rather than straight javascript, this should do the trick for you.
if ( $('input[id*=rblRegUseVehicles]:checked').length < 0 ) isValid = "false";
The selector gets all checked radiobuttons who's id contains 'rblRegUseVehicles'.
Edit
If you want to stick with your original script,using UniqueID rather than ClientID may work
if((!f.<%=rblRegUseVehicles.UniqueID%>[0].checked) && (!f.<%=rblRegUseVehicles.UniqueID%>[1].checked))
{
isValid = "false";
}

Related

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

getting a value from user control

I have an issue that i believe there is a simple solution for but being new to ASP.NET it is not very clear to me.
I have a user control that has a for loop that makes bunch of hyperlink elements in a list (looks like below>
<li><a href="blahblah.aspx?ID=1..../></li>
<li><a href="blahblah.aspx?ID=2..../></li>
<li><a href="blahblah.aspx?ID=3..../></li>
<li><a href="blahblah.aspx?ID=4..../></li>
Next, that uc is used in another page with in a <form> tag with method="post" and <asp:button> that isn't really used.
Next, when one of the links is clicked it will go to blahblah.asp and get the ID in there via Request.QueryString
So far so good.
What i want to do though (and the reason for this post) is that i want to not use ID as a query parameter. I want to pass the ID in the body. So the link would be blahblah.aspx and it wouldn't show the ID.
What would be the best way to accomplish this. I've tried couple different ways but it's not working.
Not sure if this will work for you, but instead of using html a tags, use the asp LinkButton control. You can then handle the command event of this control so have your for loop generate the following controls (or better yet use a repeater):
<asp:LinkButton id="button1" runat="server" CommandArgument="1" OnCommand="SendToBlah" />
<asp:LinkButton id="button2" runat="server" CommandArgument="2" OnCommand="SendToBlah" />
private void SendToBlah(Object sender, CommandEventArgs e)
{
Session["ID"] = e.CommandArgument;
Response.Redirect("blahblah.aspx");
}
You can then use your session argument inside your blahblah.aspx page however you see fit.
To send the data in the HTTP body instead of the query string, you need to do an HTTP post. This is easy enough, just use a form with a hidden parameter:
<li>
<form action="blahblah.aspx" method="post">
<input type="hidden" name="ID" value="1" />
<input type="submit" value="submit" />
</form>
</li>
To get the data on the target page, use Request.Params instead of QueryString.
Now, if you want to use a hyperlink rather than a "submit" button, you can always submit the form using Javascript:
<li>
<form id="myform1" action="blahblah.aspx" method="post">
<input type="hidden" name="ID" value="1" />
</form>
Submit the form
<form id="myform2" action="blahblah.aspx" method="post">
<input type="hidden" name="ID" value="1" />
</form>
Submit the form
</li>
<script type='text/javascript'>
window.submitForm = function(id) {
document.forms[id].submit();
return false;
}
</script>

How to hide items within a datalist if values are NULL (asp.net)(visual basic)

For example in my datalist if Eval("OptionJ").Tostring = Null I would like the function GetVisible to set visibility of the radio button to false like so:
<input name="Q<%#Eval("ID")%>" type="radio" value="J" visible="<%# GetVisible(Eval("OptionJ").ToString()) %>">
<%#Server.HtmlEncode(Eval("OptionJ").ToString())%>
</option><br />
I then have a codebehind function like so:
Protected Function GetVisible(ByVal Evalresult As String) As String
If Evalresult = Nothing Then
Return "False"
Else
Return "True"
End If
End Function
I have also tried checking EvalResult = String.empty
In the outputted html the visible status is being set to false...
<input name="Q3" type="radio" value="J" visible="False">
But it is still displayed on the page!
Please can you let me know how to get this working? Thanks in advance for your time reading and any answers posted.
Try this one:
<input name="Q3" type="radio" value="J" visible="false" runat="server">
Visible property works only for ASP.NET Server control but here you are using Html Input Control.
So one approach is that add runat="server" attribute in this control to if you want to continue with visible property and second one is that add style="visibility:hidden" attribute for HTML input control as given below:
<input name="Q3" type="radio" value="J" style="visibility:hidden">

ASP.NET radiobuttonlist onclientclick

I've noticed there is no OnClientClick() property for the radiobuttonlist in the ASP.NET control set. Is this a purposeful omissiong on Microsoft's part?
Anyway, I've tried to add OnClick to the radio button list like so:
For Each li As ListItem In rblSearch.Items
li.Attributes.Add("OnClick", "javascript:alert('jon');")
Next
But alas, it doesn't work. I've even checked the source in firebug, and there is no javascript shown in the radiobuttonlist. Does anyone know how to get this very simple thing working? I'm using ASP.NET control adpaters so don't know if that has anything to do with it.
(I wish asp.net/javascript would just work out the box!!!)
I found I could just add a onclick attribute to the RadioButtonList and it would fire the javascript client side as expected.
<asp:RadioButtonList ID="RadioButtonList1" runat="server" onclick="alert('RadioButtonListSelectChange');">
<asp:ListItem Text="One" Value="1"/>
<asp:ListItem Text="Two" Value="2"/>
</asp:RadioButtonList>
You could then write a client script that could determine the currently selected list item.
The onclick attribute function very good in ListItem object. Try in this way:
li.Attributes.Add("onclick", "javascript:showDiv()")
That function to me.
Not really an answer but a few weird observations on the other suggestions. I'll preface this by saying this is a RadioButtonList in an ASP.Net 4.0 app with RepeatDirection=Vertical
1) Adding onclick='myJsFunction(this);' attributes to the ListItems worked fine for me except after a different item on the page causes a post back (a button action for example), the attributes on the ListItems don't get rendered in the result of the postback. They get rendered to start, but not when there's a button action. Go figure.
2) Apparently our legacy code was relying on some murky, less than canonical behavior on the RadioButtonList/ListItem that gets very muddy on postback. If you put an id attribute on a ListItem, ASP.Net 2.0+ will add a span tag around the radio button and put your id value there. If you add an onclick attribute on it, that will go on the radio button - at least when Page.IsPostBack == false.
<asp:RadioButtonList ID="proceedChoice" runat="server" RepeatDirection="Vertical">
<asp:ListItem id="Close_Case_Y" Value="Y" onclick="SelectResolveType(this);" Text="I want to close this case." runat="server" />
<asp:ListItem id="Close_Case_N" Value="N" onclick="SelectResolveType(this);" Text="I want to cancel this and start over." runat="server" />
</asp:RadioButtonList>
renders as
<table id="...proceedChoice" border="0">
<tr>
<td><span id="Close_Case_Y"><input id="..._proceedChoice_0" type="radio" value="Y" onclick="SelectResolveType(this);" /><label for="..._proceedChoice_0">I want to close this case.</label></span></td>
</tr><tr>
<td><span id="Close_Case_N"><input id="..._proceedChoice_1" type="radio" value="N" onclick="SelectResolveType(this);" /><label for="..._proceedChoice_1">I want to cancel this case and start over.</label></span></td>
</tr>
</table>
After a postback from some other button, though the same template renders as
<table id="...proceedChoice" border="0">
<tr>
<td><input id="..._proceedChoice_0" type="radio" value="Y" /><label for="..._proceedChoice_0">I want to close this case.</label></td>
</tr><tr>
<td><input id="..._proceedChoice_1" type="radio" value="N" /><label for="..._proceedChoice_1">I want to cancel this case and start over.</label></td>
</tr>
</table>
But here's where it gets really weird: if you tack on other attributes to ListItem other than id and onclick, just those attributes get preserved. For example if I add hidden="hidden" to one of the ListItems, it gets rendered like this after postback
<table id="...proceedChoice" border="0">
<tr>
<td><span hidden="hidden"><input id="..._proceedChoice_0" type="radio" value="Y" /><label for="..._proceedChoice_0">I want to close this case.</label></span></td>
</tr><tr>
<td><input id="..._proceedChoice_1" type="radio" value="N" /><label for="..._proceedChoice_1">I want to cancel this case and start over.</label></td>
</tr>
</table>
Because it's a list control, there isn't a OnClientClick event. Either use a postback (SelectedIndexChange) or write javascript to grab the click for each radio button.
foreach (PlaceType t in query.ToList())
{
ListItem li = new ListItem(#"<img src=""" + t.ImageRelativePath + #"""/><br/>" + t.PlaceTypeText, t.PlaceTypeID.ToString());
li.Attributes.Add("onclick", "javascript:placeTypeChange('" + t.ImageRelativePath + "')");
rblPlaceTypes.Items.Add(li);
}
So in there I'm
a) Iterating over the results of a EF query.
b) Creating a new list item, and adding it to my radio button list (rblPlaceTypes).
c) The constructor is putting an image in so that displays.
d) the li.Attributs.Add is putting the javascript wireup in.

Appended content using jQuery 'append' is not submitted?

I'm new to jQuery so this may be a real simple answer. I have a ASP.NET project that I'm trying to dynamically add content to an ASP Label element by typing into a text box and clicking a button. Sounds simple right? Well, I can get the content to added, but when the form is submitted the Label element is still empty? Why is the new data not submitted? What step am I missing? Please help. Here's some sample code:
<asp:Label ID="lblContainer" Text="" runat="server" />
<input type="text" name="tag" id="tag" />
<input type="button" name="AddTag" value="Add" class="button" onclick="addTag();" />
function addTag() {
if ($("#tag").val() != "") {
$("#lblContainer").append('$("#tag").val()' + '<br />');
$("#tag").val("");
}
}
Asp.Net doesn't work that way because it won't send the contents of a <span> tag in the HTTP Post when your form is submitted. You need to add that content to an <input> of some sort. Try something like this instead.
<span id="tagContainer" runat="server"></span>
<input type="text" name="tag" id="tag" />
<input type="button" name="AddTag" value="Add" class="button" onclick="addTag();" />
<!-- Use a hidden field to store the tags -->
<asp:HiddenField id="tagText" runat="server" />
function addTag() {
var newTag = $.trim($("#tag").val());
if (newTag != "") {
$("#tag").val("");
$("#tagContainer").append(" "+ newTag);
// append the newTag to the hidden field
var $tagText = $("#tagText");
$tagText.val($tagText.val() + " " + newTag);
}
}
Then in your asp.net code you can retrieve the value like so..
string myTagText = tagText.value;

Resources