Selected item in RadioButton list to do page transfer - radiobuttonlist

I have a radio button list as the following:
<asp:RadioButtonList ID="RBjobSector" runat="server" style="font-size: medium">
<asp:ListItem>apple</asp:ListItem>
<asp:ListItem>orange</asp:ListItem>
</asp:RadioButtonList>
How do I direct users to go either apple.aspx or orange.aspx page if users select either one of them after clicking the "Go" button?

Put below code to selectedIndex changed event
if (RBjobSector.SelectedIndex == 0)
Reponse.Redirect("apple.aspx")
if (RBjobSector.SelectedIndex == 1)
Reponse.Redirect("orange.aspx")

Related

If I change textbox content I have to double click radio button ASP

This is what I want for my code. I have textbox that shows like this:
<telerik:RadMaskedTextBox ID="txtOccurrenceTime" runat="server" BackColor="#FFFFCC" Width="50px" OnTextChanged="txtOccurrenceTime_TextChanged" Mask="##:##" AutoPostBack ="true"></telerik:RadMaskedTextBox>
When you hit enter is all good but when I select one of the radio buttons, it's not actually getting the value and forces the user to double click on it to make it work. This radio button list has a "OnSelectedIndexChanged" event and are "AM" and "PM" items and calculates full time in another box as string (for example 02:03 AM).
<asp:RadioButtonList ID="rblOccurrenceTimeAMPM" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow" OnSelectedIndexChanged="rblOccurrenceTimeAMPM_SelectedIndexChanged" AutoPostBack ="true">
<asp:ListItem Text="AM" Value="AM"></asp:ListItem>
<asp:ListItem Text="PM" Value="PM"></asp:ListItem>
</asp:RadioButtonList>
I tried to set AutoPostBack = false on the textbox and if you hit enter after you change the text on the textbox, of course nothing happens.
Is there a way I can have the "hit enter option" and "select radio button" only once and have the same results? Please let me know.
Thanks!!!

Hide dropdown box when ASP.NET dropdownlist is empty

since I combined an asp:dropdownlist with an asp:checkboxlist, I have the problem in IE(8) and Firefox (Chrome works fine) that everytime I click the DropDownList, the empty box appears in addition to the popup I manually open when the dropdownlist is clicked.
My question now is: How can I hide this empty box (since there are no entries in it), but keep the dropdown element? I don't want to replace this component, since it still should look like a dropdownlist. If I change it to a text box it's not clear anymore that it can be used as a dropdown.
This is what I currently have in place:
<div style="width: 190px;" class="right">
<!-- the original drop down list -->
<asp:DropDownList CssClass="dropdownbox" ID="ddlCountry" runat="server">
</asp:DropDownList>
<cc1:PopupControlExtender ID="ddlCountry_PopupControlExtender" runat="server" DynamicServicePath=""
Enabled="True" ExtenderControlID="" TargetControlID="ddlCountry" PopupControlID="pnlPopup"
OffsetY="20">
</cc1:PopupControlExtender>
<!-- Popup control extender that maps the country list to the dropdown list -->
<asp:Panel ID="pnlPopup" runat="server" CssClass="dropdowncheckbox">
<!-- List of countries with a checkbox for each entry -->
<asp:CheckBoxList ID="countryList" runat="server"
DataTextField="Countries" DataValueField="Countries" AutoPostBack="True"
OnSelectedIndexChanged="countryList_SelectedIndexChanged">
</asp:CheckBoxList>
</asp:Panel>
</div>
If there is a component that fits my purpose better, please let me know.
Thanks a lot in advance for your suggestions.
Place this code in the event you need to trigger the change
This will work for c#
if(dropdownlist.Items.Count == 0)
{
dropdownlist.Attributes["style"] = "display: none;";
}
else
{
dropdownlist.Attributes["style"] = "";
}
Simply set the Drop Down List Enabled property to false and toggle it to true when it has entries.
dropdownbox.Enabled = false;
do something like this in c#:
if(dropdownlist.Items.Count == 0)
{
dropdownlist.Visible = false;
}
else
{
dropdownlist.Visible = true;
}
Encapsulate the dropdown in a div, give the div an id and the runat="server" property
Check in the code behind of the data to load the dropdown is empty set the div to visible false; if not, set the div to visible true.

Listbox.SelectedIndex is not changing, after some asp property changes, What is wrong?

I have a form with a dropdownbox two listboxes and two buttons on it.
I removed a "select" button as I just used the DropDownList1_SelectedIndexChanged, but the event would not fire until I used the suggestion from:
Why DropDownList.SelectedIndexChanged event does not fire?
It involved changing the AutoPostBack='true' and EnableViewState="true" properties
So now the DropDownList works but with the two listboxes, the SelectedIndex stays as -1 and does not change even when selected.
Listbox code:
<asp:ListBox ID="ListBox1" runat="server" EnableViewState="true"
Height="207px" Width="168px"></asp:ListBox>
DropDownList Code:
<asp:DropDownList ID="DropDownList1" runat="server" EnableViewState="true"
AutoPostBack="true" style="font-weight: 700; margin-left: 26px">
Button click event code:
If (ListBox1.SelectedIndex < 0) Then 'No user selected
MsgBox("Please select a user to add from the Listbox on the left.", vbCritical, "ListBox2 Validation Error")
Else
The MsgBox now always says no user has been select but there is a user select, very weird.
Any ideas? I think it has something to do with post backs, page_load or the selectedindex changed event of the listbox, it worked perfectly before I made the modifications.
The problem was the postback and that the code to populate the listboxes was in a dropbox_load subroutine. This meant that the listbox would repopulate everytime something changed on the page and hence the selecteditem index would revert back to -1.

Setting DefaultButton to button.UniqueID throws exception

The problem I'm trying to solve:
I have several text boxes in an asp:Panel. When the user hits Enter from any of those boxes, I want the form to submit as if they've clicked btnAddTag. (When the cursor is not in those boxes, I have a different default submit button.)
The aspx:
<asp:Panel id="thePanel" runat="server">
<asp:Button ID="btnAddTag" Text="Add Tag" runat="server" />
</asp:Panel>
The vb:
tagPanel.DefaultButton = btnAddTag.UniqueID
The exception:
The DefaultButton of 'tagPanel' must
be the ID of a control of type
IButtonControl.
The value of btnAddTag.UniqueID is ctl00$phMain$btnAddTag (there's a master page, this section is called phMain).
I've also tried CType(tagPanel.FindControl("btnAddTag"), Button).UniqueID.
do:
tagPanel.DefaultButton = btnAddTag.ID
more info here:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.panel.defaultbutton.aspx
You should set the control's ID not UniqueID:
tagPanel.DefaultButton = btnAddTag.ID

How to find checked RadioButton inside Repeater Item?

I have a Repeater control on ASPX-page defined like this:
<asp:Repeater ID="answerVariantRepeater" runat="server"
onitemdatabound="answerVariantRepeater_ItemDataBound">
<ItemTemplate>
<asp:RadioButton ID="answerVariantRadioButton" runat="server"
GroupName="answerVariants"
Text='<%# DataBinder.Eval(Container.DataItem, "Text")%>'"/>
</ItemTemplate>
</asp:Repeater>
To allow select only one radio button in time I have used a trick form this article.
But now when form is submitted I want to determine which radio button is checked.
I could do this:
RadioButton checkedButton = null;
foreach (RepeaterItem item in answerVariantRepeater.Items)
{
RadioButton control=(RadioButton)item.FindControl("answerVariantRadioButton");
if (control.Checked)
{
checkedButton = control;
break;
}
}
but hope it could be done somehow simplier (maybe via LINQ to objects).
You could always use Request.Form to get the submitted radio button:
var value = Request.Form["answerVariants"];
I think the submitted value defaults to the id of the <asp:RadioButton /> that was selected, but you can always add a value attribute - even though it's not officially an <asp:RadioButton /> property - and this will then be the submitted value:
<asp:RadioButton ID="answerVariantRadioButton" runat="server"
GroupName="answerVariants"
Text='<%# DataBinder.Eval(Container.DataItem, "Text")%>'"
value='<%# DataBinder.Eval(Container.DataItem, "SomethingToUseAsTheValue")%>' />
Since You are using javascript already to handle the radio button click event on the client, you could update a hidden field with the selected value at the same time.
Your server code would then just access the selected value from the hidden field.
I'm pretty sure that the only thing you could use LINQ to Objects for here would be to take the conditions from within the foreach loop and move them to a where clause.
RadioButton checked =
(from item in answerVariantRepeater.Items
let radioButton = (RadioButton)item.FindControl("answerVariantRadioButton")
where radioButton.Checked
select radioButton).FirstOrDefault();

Resources