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.
Related
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.
I have just encountered a strange situation with an ASP.NET page. My form has 2 controls that have AutoPostBack="true": a RadioButtonList and a DropdownList. The RadioButtonList also has EnableViewState="False". When I change the selected item of the dropdownlist, the RadioButtonList SelectedIndexChanged event fires as well. If I remove EnableViewState="False", then this behavior goes away. Can anyone explain why this is happening? I have included sample code below so you can see this behavior for yourself:
<div>
<asp:RadioButtonList ID="rblTest" AutoPostBack="true" OnSelectedIndexChanged="rblTest_SelectedIndexChanged" EnableViewState="false" runat="server">
<asp:ListItem Text="Option 1" Value="1" />
<asp:ListItem Text="Option 2" Value="2" />
<asp:ListItem Text="Option 3" Value="3" />
</asp:RadioButtonList>
<br />
<br />
<asp:DropDownList ID="ddlTest" AutoPostBack="true" OnSelectedIndexChanged="ddlTest_SelectedIndexChanged" runat="server">
<asp:ListItem Text="Select One" Value="" />
<asp:ListItem Text="Option 1" Value="1" />
<asp:ListItem Text="Option 2" Value="2" />
<asp:ListItem Text="Option 3" Value="3" />
</asp:DropDownList>
<br />
<br />
<asp:Button ID="btnSubmit" OnClick="btnSubmit_Click" Text="Submit" EnableViewState="false" runat="server" />
<br />
<br />
<asp:Label ID="lblResult" runat="server" />
</div>
Here is the code behind code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
rblTest.SelectedIndex = 0;
}
}
protected void rblTest_SelectedIndexChanged(object sender, EventArgs e)
{
lblResult.Text += "RadioButton List SelectedIndexChanged fired<br />";
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
lblResult.Text += "Submit Button Click Event Fired<br />";
}
protected void ddlTest_SelectedIndexChanged(object sender, EventArgs e)
{
lblResult.Text += "Dropdown List SelectedIndexChanged Event Fired<br />";
}
Additionally, if you comment out this line in the code behind:
rblTest.SelectedIndex = 0;
Then this behavior does not occur until you select something from the radio button list. Any help is greatly appreciated.
When you set EnableViewState to false, it means you are not gonna keep the state of the objects. This means that when you do an autopost, the values are reset to their original values.
When it is a post back, you are changing the state of a radio button (rblTest.SelectedIndex = 0) and that's firing the event. So, you change one radio button and 2 events are fired.
Just something else to point out, remember that every time you do a postback, the page is recreated at server-side. And the life-cycle of the page is executed. The ViewState is used on that process to populate the controls. You can read more about it on: https://msdn.microsoft.com/en-us/library/ms178472%28v=vs.85%29.aspx
For me the selectedindexchanged event was firing also when I was not expecting it. What my code did was change the selected value on selectedindexchange event. So, next time when a postback occured, the selectedindexchange event was also fired again. I solved it by first setting the selectedindex to -1 and then setting the actual value.
Initial code was -
protected void rbl_selectedindexchanged(object sender, event evt)
{
rbl.Items.FindByText("something").Selected = true;
}
After modify -
protected void rbl_selectedindexchanged(object sender, event evt)
{
rbl.SelectedIndex = -1;
rbl.Items.FindByText("something").Selected = true;
}
I need to add a condition to a DropDownList where a method can be executed by button click only if the user has selected a value different than the listItem (default value).
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True"
DataSourceID="SqlDataSource5"
DataTextField="proj_name" DataValueField="proj_name">
<asp:ListItem Text="Select a project to clone" Value="" />
</asp:DropDownList>
How can I structure an if condition to validate that the selected value is not the ListItem (default value)?
You can use asp.net delivered validation controls
Ex:
<asp:RequiredFieldValidator id="rfv1"
ControlToValidate="DropDownList1"
Display="Static"
ErrorMessage="* Select a value"
InitialValue="DefaultValueHere"
runat="server"
ValidationGroup="V1"/>
Then edit your button markup to use ValidationGroup
<asp:Button Id="button1" ValidationGroup="V1" .../>
In your codebehind button click code add this
protected void button1_onlick(Object sender, EventArgs e)
{
If(Page.IsValid)
{
// your existing code here
}
}
See sample code below
if (DropDownList1.SelectValue == "")
{
// Write your code here
}
you can also have:
if (DropDownList1.Text == "Select a project to clone")
{
// Write your code here
}
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.