In my aspx file, I have a drop down list
<asp:DropDownList runat="server" ID="Dlist" CssClass="dropdown" AutoPostBack="true" SelectedIndexChanged="CtrlChanged">
<asp:ListItem Text="Select item" Value="1"></asp:ListItem>
</asp:DropDownList>
I have a radio button list
<asp:RadioButtonList ID="RadioButtonList1" RepeatColumns="1"
RepeatDirection="Vertical" RepeatLayout="Table" runat="server" AutoPostBack="true">
<asp:ListItem>Option 1</asp:ListItem>
<asp:ListItem>Option 2</asp:ListItem>
</asp:RadioButtonList>
Now I want to change the name of one or both of the radio buttons in the radio button list after something has been selected from the dropdown list using C#. Below is my attempt but not working.
protected void CtrlChanged(Object sender, EventArgs e) {
//attempting to change text of first radio button when item has been selected from dropdownlist
RadioButtonList1.SelectedIndex = 0;
RadioButtonList1.SelectedItem.Text = "Text changed!";
}
First, it is OnSelectedIndexChanged, not SelectedIndexChanged. And the ListItems of a RadioButtonList are index based, so you need to access them like this:
protected void CtrlChanged(object sender, EventArgs e)
{
RadioButtonList1.Items[0].Text = "NewValue 1";
RadioButtonList1.Items[1].Text = "NewValue 2";
}
Your way does change the text, but only for the item you set the SelectedIndex of. And it will change the selected radiobutton to the first one, should one already have been selected.
Related
I am using a radio RadioButtonList in asp net to create 2 radio button that when selected are supposed to change color the first one being green and the second one red but i am not able to make the change said color
Tldr:Change to list item1 little circle inside list item green, change to list item 2 little circle go red.
Also for some reason it doesnt recognize my radiobuttonlist1 in RadioButtonList1.Items
This is my html code
<asp:RadioButtonList CssClass="listitemcss" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" ID="RadioButtonList1" RepeatDirection="Horizontal" runat="server">
<asp:ListItem >
<p style="color:transparent;"> s2</p>
</asp:ListItem>
<asp:ListItem Selected="True">
<p style="color:transparent;">2</p>
</asp:ListItem>
</asp:RadioButtonList>
And this my c#
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (RadioButtonList1.Items[0].Selected)
{
RadioButtonList1.Items[0].Attributes.Add("style", "background-color: green;");
}
if (RadioButtonList1.Items[1].Selected)
{
RadioButtonList1.Items[1].Attributes.Add("style", "background-color: red;");
}
}
I don't have and IDE in front of me, but if you want them to toggle with one being red and the other green, try this:
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (RadioButtonList1.Items[0].Selected)
{
RadioButtonList1.Items[0].Attributes.Add("style", "background-color: green;");
RadioButtonList1.Items[1].Attributes.Add("style", "background-color: red;");
}
if (RadioButtonList1.Items[1].Selected)
{
RadioButtonList1.Items[1].Attributes.Add("style", "background-color: green;");
RadioButtonList1.Items[0].Attributes.Add("style", "background-color: red;");
}
}
set autopostback property to true
<asp:RadioButtonList CssClass="listitemcss" AutoPostBack="true" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" ID="RadioButtonList1" RepeatDirection="Horizontal" runat="server">
<asp:ListItem >
I have four dropdownlistbox in my form, i want to load items into three dropdownlists according to the selection of items from the first drop down list , my first dropdown list has folloding items
Amount ,
PAC ,
Base UOM
Whatever i am selecting from first drop down list , i want to load the same selected item into remaining three dropdownlist
I have tried the following code but it is not working as expected
protected void ddl_UOM_SelectedIndexChanged(object sender, EventArgs e)
{
string uom_Name = ddl_UOM.SelectedItem.Value;
ddl_UOM2.Items.Add(uom_Name);
ddl_UOM3.Items.Add(uom_Name);
ddl_UOM4.Items.Add(uom_Name);
}
pls help.
You need to use Ajax Control Tool kit's Cascaded Drop-down
Here is the like for it:
http://www.asp.net/AjaxLibrary/AjaxControlToolkitSampleSite/Walkthrough/CCDWithDB.aspx
and here is the demo:
http://www.asp.net/AjaxLibrary/AjaxControlToolkitSampleSite/CascadingDropDown/CascadingDropDown.aspx
here is a tutorial link:
http://www.dotnetfox.com/articles/ajax-cascading-dropdown-example-with-database-in-Asp-Net-1078.aspx
Hope this material helps.
May be you don't have set AutoPostBack property to true. Try below code sample :
ASPX:
<asp:DropDownList runat="server" ID="parentDropDown" OnSelectedIndexChanged="parentDropDown_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
<asp:ListItem Text="3" Value="3"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList runat="server" ID="child1">
</asp:DropDownList>
<asp:DropDownList runat="server" ID="child2">
</asp:DropDownList>
Code Behind:
protected void parentDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
string uom_Name = parentDropDown.SelectedValue;
child1.Items.Add(uom_Name);
child2.Items.Add(uom_Name);
}
If you want to remove existing item from child before you add then:
protected void parentDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
string uom_Name = parentDropDown.SelectedValue;
child1.Items.Clear();
child2.Items.Clear();
child1.Items.Add(uom_Name);
child2.Items.Add(uom_Name);
}
You have to set AutoPostBack="true" to the first drop down list and clear other drop downs items before adding new item.
ASPX:
<asp:DropDownList ID="ddl_UOM" runat="server" OnSelectedIndexChanged="ddl_UOM_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="Amount" Value="Amount"></asp:ListItem>
<asp:ListItem Text="PAC" Value="PAC"></asp:ListItem>
<asp:ListItem Text="Base" Value="Base"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddl_UOM2" runat="server"></asp:DropDownList>
<asp:DropDownList ID="ddl_UOM3" runat="server"></asp:DropDownList>
<asp:DropDownList ID="ddl_UOM4" runat="server"></asp:DropDownList>
ASPX.CS
protected void ddl_UOM_SelectedIndexChanged(object sender, EventArgs e)
{
string uom_Name = ddl_UOM.SelectedItem.Value;
ddl_UOM2.Items.Clear();
ddl_UOM2.Items.Add(new ListItem(uom_Name, uom_Name));
ddl_UOM3.Items.Clear();
ddl_UOM3.Items.Add(new ListItem(uom_Name, uom_Name));
ddl_UOM4.Items.Clear();
ddl_UOM4.Items.Add(new ListItem(uom_Name, uom_Name));
}
protected void ddl_UOM_SelectedIndexChanged(object sender, EventArgs e)
{
string uom_Name = ddl_UOM.SelectedItem.Value;
ddl_UOM2.Items.Clear();
ddl_UOM3.Items.Clear();
ddl_UOM4.Items.Clear();
ddl_UOM2.Items.Add(uom_Name);
ddl_UOM3.Items.Add(uom_Name);
ddl_UOM4.Items.Add(uom_Name);
}
After writing code in ddl_UOM_SelectedIndexChanged() , call ddl_UOM_SelectedIndexChanged() method from Page_Load()
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
ddl_UOM_SelectedIndexChanged(sender, e );
}
}
catch (Exception)
{
throw;
}
}
You should try to use PageMethods in JS to load the dropdowns any way you like any time you like.
I added a DropDownList from the Toolbox to the Login page on a website I'm working on.
Once I choose a ListItem in the DropDownList (in my case lets say
Gym for example...), when clicked, I want that three bars we'll be opened below my DropDownList(for example, the bars that we'll be opened are Username, Password and ID), I mean three TextBoxes beneath each other.
I think you can either try the SelectedIndexChanged event or Javascript to display the textboxes without a postback.
<select>
<option value="1">Gym 1</option>
<option value="2">Gym 2</option>
<option value="3">Gym 3</option>
<select>
At firest you put your textboxes in a panle then hide this panel and
you should set Autopostback property of you drobdownlist to True and after selecting a item in DropDownList, postback will accur. So you can show that panel include text boxes.
What you might really be needing is dynamic text boxes.
In the html part:
<asp:DropDownList runat="server" ID="DDL1" autopostback = "true" onselectedindexchanged="DDL_SelectChanged" />
<asp:PlaceHolder runat="server" ID="PH1">
</asp:PlaceHolder>
In codebehind:
void DDL_SelectChanged(object sender, EventArgs e)
{
if (DDL1.SelectedIndex == 1)
{
for (int i = 0; i < 3; i++)
{
TextBox newTB = new TextBox();
newTB.ID = "TB" + i;
PH1.Controls.Add(newTB);
}
}
}
You could use MultiveView control. And set active view index in Dropdown's selectedIndexChanged event.
I wrote some example code for you:
ASPx side:
<asp:MultiView ID="multiView" ActiveViewIndex="-1" runat="server">
<asp:View ID="viewGym" runat="server">
<asp:TextBox ID="txtBxUserName" runat="server" />
<asp:TextBox ID="txtBxPassword" runat="server" />
<asp:TextBox ID="txtBxId" runat="server" />
</asp:View>
</asp:MultiView>
<asp:DropDownList ID="Dropdownlist1" runat="server" AutoPostBack="true"
onselectedindexchanged="Dropdownlist1_SelectedIndexChanged">
<asp:ListItem Text="Choose one club" Value="0" />
<asp:ListItem Text="Gym" Value="1" />
<asp:ListItem Text="Shoppers" Value="2" />
</asp:DropDownList>
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
if ( IsPostBack ) //don't forget :)
return;
}
protected void Dropdownlist1_SelectedIndexChanged( object sender, EventArgs e )
{
if ( Dropdownlist1.SelectedValue == "1" ) //Gym item selected
{
multiView.ActiveViewIndex = 0; //Gym view active
}
}
If you want to any view not active when first page load then you set the ActiveViewIndex with -1 in aspx code.
I have a drop down list with multiple values. I have a list item named "Other". When selecting other I want to generate a text box with required field validator. I have written like this:
Markup:
<asp:DropDownList ID="dl_test_name" runat="server"
OnSelectedIndexChanged="SelectedIndexChanged"
Height="22px" Width="103px">
<asp:ListItem>Science</asp:ListItem>
<asp:ListItem>Maths</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="tb_other" runat="server" Width="94px" Visible="False">
</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"
ControlToValidate="tb_other" ErrorMessage="*">
</asp:RequiredFieldValidator>
Code-behind:
protected void SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList dropDownList = (DropDownList)sender;
if (dropDownList.SelectedValue == "Other")
{
tb_other.Enabled = true;
tb_other.Text = string.Empty;
tb_other.Visible = true;
}
else
{
tb_other.Enabled = false;
tb_other.Text = dropDownList.SelectedValue;
}
}
but when selecting on any list item ,control doesn't go to SelectectedIndexChanged event. Only after reloading the page does it work.
What is the problem?
To make your DropDownList post back to the server you need to use the AutoPostback property, like this:
<asp:DropDownList ID="dl_test_name" runat="server"
OnSelectedIndexChanged="SelectedIndexChanged"
Height="22px" Width="103px" AutoPostBack="true">
Arathy, make AutopostBack property of dropdown to true in aspx
I have 2 radio buttons and a drop down list under option 1 and option 2. the user can either select the radio button or the drop down list.
option 1
radiobtn1
radiobtn2
option2
dropdownlistbox
the radiobtn1 under option1 is checked as default. When the user select the value from the dropdown list, both the radio buttons should be disabled or it should deselects the radiobtn1 which is checked as default.
<tr><td>option1</td></tr>
<tr>
<td><asp:RadioButton ID="rdo" GroupName="Month" Text="radiobtn1" runat="server /></td>
<td><asp:RadioButton ID="rdo2" GroupName="Month" Text="radiobtn2" runat="server /></td>
</tr>
<tr><td>option2</td></tr>
<tr><td>
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="ddMonYear_SelectedIndexChanged">
<asp:ListItem Text="opt1"></asp:ListItem>
<asp:ListItem Text="opt2"></asp:ListItem>
</asp:DropDownList>
</td></tr>
protected void Page_Load(object sender, EventArgs e)
{
rdo.Checked=true;
if (ddMonYear.SelectedValue.Length.ToString() != "0")
{
rdo.Checked = false;
rdo.Enabled = false;
}
else
{
rdo.Checked = true;
}
}
The above code doesnt work. the radiobtn 1 is not deselecting when i select the value from the drop down.
Help me in correcting my code.
Thanks.
On your dropdown box, add this code (autopostback)
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="ddMonYear_SelectedIndexChanged" AutoPostBack=True">
What this does is keep the value of the dropdown box. Each time your page is loaded, the dropdown box gets the default value before your pageload event fires. give that a try.