About RadioButton CheckedChanged event - c#

Hi there im using a single TextBox for querying differents types of chossings, for example when i select the first RadioButton (Last Name) I search by Client last name, when i choose second RadioButton (Doc. Code) I search by code and so on, please how can i manage or handle exceptions when for example if user choose "Search by Date" and send a string type?
Im using C# 3.5 - Asp.net
I would like to make it with regular expressions and add that in the RadioButton event, so when users changes radio, he could type just some characters in optionA, other more in Option B, and just dates in OptionC ... (regular expression)
Thanks in advances

if you are using the asp web control radiobuttonlist then you can make lots of changes when their is a postback. you can set the attribute to SelectIndexChanged,so whenever their is a change it cause a postback and then you can do whatever from their(verifications).
ex:
<asp:radioButtonList
id="radio1" runat="server"
autoPostBack="true"
cellSpacing="20"
repeatColumns="3"
repeatDirection="horizontal"
RepeatLayout="table"
textAlign="right"
OnSelectedIndexChanged="radio_SelectedIndexChanged">
<asp:ListItem text="10pt" value="itsMe"/>
<asp:ListItem text="14pt" value="itsYou"/>
<asp:ListItem text="16pt" value="Neither"/>
</asp:radioButtonList>
on the server you should have
protected void radio_SelectedIndexChanged(object sender, EventArgs e)
{
//do whatever you want by calling the name of the radio id
//example
if(radio1.SelectedItem.Value=="(whatever you want to test)"
}

Related

If Else Statement not working ASP.NET

Index.aspx
<asp:RadioButton ID="rbt1" runat="server" Text="By Customer" GroupName="summary" OnCheckedChanged="enabled_CheckedChanged" Checked="True"/>
<asp:RadioButton ID="rbt4" runat="server" Text="With Target" OnCheckedChanged="enabled_CheckedChanged" GroupName="summary"/>
Index.aspx.cs
public void enabled_CheckedChanged(object sender, EventArgs e)
{
if (rbt4.Checked == true)
{
rbt1.Checked = true;
}
else
{
rbt1.Checked = false;
}
}
The purpose of the code is when rbt1 is checked, rbt4 will be checked. User can also check rbt4 after checking rbt1. However, if rbt4 is checked, rbt1 will be checked automatically. The code above was used but it doesn't seem to be working. Did I miss out something or is there error in my coding? Please advice. Thanks in advance.
Both radio buttons have the same GroupName. Only one of them can be checked at a time. Change GroupName property of radio buttons to fix the error. refer this link for clarity. Also add `AutoPostBack="true" property to radio button code.
You are using radio button and want to select multiple but radio button are mutually exclusive with same GroupName. You can not select more then one RadioButton with same group. You probably need to use the CheckBox for selecting multiple, or Giving different GroupName to exceptional cases may sort out the problem or you may need combination of checkboxes with radiobuttons or nested html-checkboxes.
Use the GroupName property to specify a grouping of radio buttons to
create a mutually exclusive set of controls. You can use the GroupName
property when only one selection is possible from a list of available
options. When this property is set, only one RadioButton in the
specified group can be selected at a time, MSDN.
If you want to make some relation in checkbox like if one is selected then some other should be selected and nothing more then that then you may use client side script to save unnecessary PostBacks and smooth user experience.
Refer this coding Note: AutoPostBack="true". Remove the Same group name for Both Radio Button.
<asp:RadioButton ID="RadioButton1" runat="server" OnCheckedChanged="RadioButton1_CheckedChanged" AutoPostBack="true" />
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
RadioButton2.Checked = true;
}
Please add : AutoPostBack="true" in your html code
<asp:RadioButton ID="rbt1" runat="server" AutoPostBack="true" Text="By Customer"GroupName="summary" OnCheckedChanged="enabled_CheckedChanged" Checked="True" />
<asp:RadioButton ID="rbt4" runat="server" AutoPostBack="true" Text="With Target" OnCheckedChanged="enabled_CheckedChanged" GroupName="summary" />

Selected Index doesn't change

I have an ASP.NET DropDownList control, with a onSelectedIndexChanged event. I also have the AutoPostBack="true" that many have said would fix the problem. However I don't think that is where the problem lays... My Html code and C# code are below for reference. The thing is the code works, but only when I press the enter key while editing the drop down box. If I simply click on an object in the drop down then the event will not fire. If I change the selected item so the "selected" text in the drop down says "ASP" and I then inspect the element using the browser I see that the Selected="True" part of the ListItem is still on the first item... It doesn't change in there. It changes with an enter key but not with a mouse click. Any help is welcome and much appreciated.
HTML:
<div class="ui-widget">
<asp:DropDownList id="Select1" OnSelectedIndexChanged="Select1_SomethingChange" runat="server" AutoPostBack="true">
<asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
<asp:ListItem Value="Select one...">Select one...</asp:ListItem>
<asp:ListItem Value="ActionScript">ActionScript</asp:ListItem>
<asp:ListItem Value="AppleScript">AppleScript</asp:ListItem>
<asp:ListItem Value="Asp">Asp</asp:ListItem>
<asp:ListItem Value="BASIC">BASIC</asp:ListItem>
</asp:DropDownList>
</div>
C#:
protected void Select1_SomethingChange(object sender, EventArgs e)
{
//something is meant to happen here
}
It may be caused by data binding your dropdownlist in Page_Load method.
Please, surround it (data binding) with
if(!IsPostBack){
// data binding.
}
Hope, it help!
AutoPostBack="true"
maybe you miss this option...
Your code works fine, there could be something in code which changes the implementation. I have debug your code and it's showing the selected item in output window. Please verify if there is some javascript code which is causing issue to call dropdown selectedIndexChanged event.
protected void Select1_SomethingChange(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
Debug.WriteLine(ddl.SelectedItem.Text);
}

checkbox list to generate one textbox

So i have this checkbox list and im trying to generate a textbox when the MISC checkbox is checked.
<asp:CheckBoxList ID="chbxEquipmnt" runat="server"RepeatColumns="4" RepeatDirection="Horizontal" ValidationGroup="Equipment" OnSelectedIndexChanged="chbxEquipmnt_SelectedIndexChanged">
<asp:ListItem Value="Laptop">Laptop</asp:ListItem>
<asp:ListItem Value=" Label Printer">Label Printer</asp:ListItem>
<asp:ListItem Value="Printer">Printer</asp:ListItem>
<asp:ListItem Value="Fax Line">Fax Line</asp:ListItem>
<asp:ListItem Value="PC">PC</asp:ListItem>
<asp:ListItem Value="MFD">MFD</asp:ListItem>
<asp:ListItem Value="Phone Line">Phone Line</asp:ListItem>
<asp:ListItem Value="Misc">Misc</asp:ListItem>
</asp:CheckBoxList>
ON my Cs page I have.........
protected void chbxEquipmnt_SelectedIndexChanged(object sender, EventArgs e)
{
if (chbxEquipmnt.SelectedValue == "Misc")
{
TextBox txt = new TextBox();
txt.ID = "txtMiscCheckBox";
Page.Form.Controls.Add(txt);
}
}
debugging i have tried with Postback which continuously on returns the first check box that was checked...for example i checked laptop instead of Misc first, the value in the debugger always show me laptop. Im not sure if a simple loop to go thru the all the button clicks would help.....I'm fresh out of school and this is a career change for me so thanks for the patience
I would change the approach: don't create the Textbox at run time.
Have it created at design time, hide it by default (Visible property), then show it when Misc option is selected.
You will run into less issues that way. Creating controls in runtime require some extra effort : dealing with ViewState, re-creating the control on PostBack, etc.

How to access a user selected item from a dropdown list that is within a fieldset and a content template in asp.net using c#

I am fairly new to asp.net and I am having trouble with adding a role form the registration page. I am using the pre-made registration page that is given when you create a new website using asp.net framework 4. I have added this label and drop down list inside the here is the code inside the .aspx file:
<p>
<%--This is for the user type--%>
<asp:Label ID="UserTypeLabel" runat="server">What type of user are you? Please select from below. </asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="0">Please select an item below...</asp:ListItem>
<asp:ListItem Value="1">Builder</asp:ListItem>
<asp:ListItem Value="2">Investor</asp:ListItem>
<asp:ListItem Value="3">Other</asp:ListItem>
</asp:DropDownList>
<%--END USER TYPE--%>
</p>
Now I want to know what the user has selected and when I attempt to add code in the .aspx.cs file it does not seem to "see" the dropdown list. I have tried using the following to access:
1) registeruser.dropdownlist1.value (error message Error1 'System.Web.UI.WebControls.CreateUserWizard' does not contain a definition for 'dropdownlist1' and no extension method 'dropdownlist1' accepting a first argument of type)
2)dropdownlist1.value (error message the name dropdownlist1 does not exist in the current context)
Is there an include file that I need or is there a method to get the aspx.cs page to access the ddl? NOTE: the field set is contained within a and a and a not sure if that's important.
Not sure if this is what you expect, but you can create an event handler.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
var item =((DropDownList)sender).SelectedItem;
}
Not sure if code is correct but this is an idea.

Changing dropdownlist selection causes full postback. (it's more interesting than it sounds)

Here's the scenario.
I have a search page with a TextBox that allows someone to type in a search term and press enter. (Which fires TextChanged). I have a DropDownList that specifies the kind of search that will be performed. It is defined in the markup as follows:
<asp:DropDownList ID="lstSearchType" runat="server" AutoPostBack="false">
<asp:ListItem Value="0">Last, First</asp:ListItem>
<asp:ListItem Value="1">Last</asp:ListItem>
<asp:ListItem Value="2">First</asp:ListItem>
<asp:ListItem Value="3">Liberty ID</asp:ListItem>
<asp:ListItem Value="4">E-mail</asp:ListItem>
<asp:ListItem Value="5">Telephone</asp:ListItem>
<asp:ListItem Value="6">Birthday (exact m/d/yyyy)</asp:ListItem>
<asp:ListItem Value="7">SSN (exact ###-##-####)</asp:ListItem>
<asp:ListItem Value="8">Address</asp:ListItem>
</asp:DropDownList>
As you can see, AutoPostBack is set to false, and there is no event hookup.
Pressing enter fires the OnTextChanged event for the TextBox, which performs a search and updates a GridView in an UpdatePanel. This UpdatePanel has its UpdateMode set to conditional and has one trigger: the TextChanged event of the search TextBox.
It's very simple.
And it works beautifully, almost.
Whenever I change the search type, the very next search does a full postback. All subsequent searches do partial postbacks (as desired) unless I change the search type again.
There is one exception to this rule: if I load the page and immediately change the search type, it doesn't do a full postback. So the first change of the DropDownList before any postback (full or partial) does not trigger a full postback.
Full Disclosure:
I'm doing a lot of JavaScript to change the appearance of the gridview during async requests. I don't detail it here because it seems unrelated. This problem only occurs when a DropDownList with no JavaScript wired up is changed.
Any ideas?
This is driving me crazy. Everything else is working.
Thanks in advance,
Clif
I figured it out. The problem was that the DropDownList was not in an UpdatePanel. It had no way to get the value without doing a full postback. The TextBox was immune to this because of the TextChanged event wiring.

Categories