Multiple dropdownlist - c#

i have 4 dropdownlist. when i change the 1st dropdown, onselectindexchanged i need to change all the remaining dropdown values.
I am working on it but i could change only the corresponding or next dropdown value on onselectindexchanged. Please provide me any example or guide me to any good link.
Pls help.. Thanks in advance.

You need to set AutoPostBack of all dropdowns to true and need to add SelectedIndexChanged event for first three drop downs. On SelectedIndexChanged of first dropdown you need to set selected value of second and it will fire SelectedIndexChanged of second and there you will have code to set selected index of third and similarly you will do on wards.
In Html
<asp:DropDownList ID="dropdownlist1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="dropdownlist1_SelectedIndexChanged">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="dropdownlist2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="dropdownlist2_SelectedIndexChanged">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="dropdownlist3" runat="server" AutoPostBack="true" OnSelectedIndexChanged="dropdownlist3_SelectedIndexChanged">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="dropdownlist4" runat="server" AutoPostBack="true" >
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
</asp:DropDownList>
1
2
In code behind
protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
{
dropdownlist2.SelectedIndex = someIndexValue;
}
protected void dropdownlist2_SelectedIndexChanged(object sender, EventArgs e)
{
dropdownlist3.SelectedIndex = someIndexValue;
}
protected void dropdownlist3_SelectedIndexChanged(object sender, EventArgs e)
{
dropdownlist4.SelectedIndex = someIndexValue;
}

Related

Asp.net GridView Get DropDownList value from itemlist on edit row

Im trying to update a cell from from DropdownList control which is using item list, but when i select value and click on update, the updated cell is empty:
<asp:TemplateField HeaderText="Done BY">
<ItemTemplate>
<asp:Label ID="lblUser" width="100px" runat="server" Text='<%#Eval("User")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownListUser" runat="server" AppendDataBoundItems="true">
<asp:ListItem Value="Alex" Text="Alex"></asp:ListItem>
<asp:ListItem Value="Yan" Text="Yan"></asp:ListItem>
<asp:ListItem Value="Yagi" Text="Yagi"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
You have to add OnSelectedIndexChanged="DropDownListUser_SelectedIndexChanged" to your code, and write the following code on the server side. This will let you change the name dynamically and post it on the label;
protected void DropDownListUser_SelectedIndexChanged(object sender, EventArgs e)
{
lblUser.Text = DropDownListUser.SelectedValue.ToString();
}

If condition for required field validation

I have a DropDownList and a TextBox in c#.net. If DropDownList value is "No" then some value must enter in TextBox. If dropdownlist value is yes then required field validator is not needed for that TextBox. How to make it possible?
<asp:DropDownList ID="dropdownlist1" runat="server"
CssClass="NormalText" Width="155px" AutoPostBack="true"
onselectedindexchanged="ddls_SelectedIndexChanged">
<asp:ListItem Selected="True" Value=""></asp:ListItem>
<asp:ListItem Value="Yes">Yes</asp:ListItem>
<asp:ListItem Value="No">No</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<asp:TextBox ID="Textbox1" runat="server" CausesValidation="True"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="Textbox1" ErrorMessage="Explanation needed If you select NO">
</asp:RequiredFieldValidator>
</td>
protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
{
}
Use this code for conditional validation:
protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
{
if(dropdownlist1.SelectedValue == "No")
{
RequiredFieldValidator1.Enabled = true;
}
else if(dropdownlist1.SelectedValue == "Yes")
{
RequiredFieldValidator1.Enabled = false;
}
}
Optionally, you may also have to set Submit Button Validation group:
ButtonSubmit.ValidationGroup = string.Empty;
when field validator is not enabled.

How to find the data which relatd to dropdown selected value asp.net

I want to fil the book ISBN on text box which matched with the Title selected on Dropdown menu. Please advice me how to proceed me ...
First of all you need to change your mark-up like this:-
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" AppendDataBoundItems="true"
DataSourceID="SqlDataSource1" DataTextField="Book_Title"
DataValueField="isbn" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Text="Select Book" Value="" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Knowledge_CenterConnectionString %>"
SelectCommand="select Book_Title,isbn from Title"></asp:SqlDataSource>
<asp:Label ID="Label1" runat="server" Text="ISBN"></asp:Label>
<asp:TextBox ID="txtISBN" runat="server"></asp:TextBox>
Then simply on dropdownlist selected index change event bind the value to textbox:-
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
txtISBN.Text = DropDownList1.SelectedValue;
}

RadioButtonList set to AutoPostBack = true but still not firing OnSelectedIndexChanged

I have 2 RadioButtonLists and I want only one option to be selected, f.i. if you select an option in List 1 the selection in List 2 should be cleared.
AutoPostBack and EnableViewState is set to true, but still the Method never fires. I have also checked if the Index actually changes, it does. I think that the PostBack just doesn't occur, but I don'T know why.
I'm thankful for any help.
ascx:
<asp:RadioButtonList ID="_listOne" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ListOneIndexChanged">
</asp:RadioButtonList>
<asp:RadioButtonList ID="_listTwo" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ListTwoIndexChanged">
</asp:RadioButtonList>
Code Behind:
protected void ListOneIndexChanged(object sender, EventArgs e)
{
_listTwo.ClearSelection();
}
protected void ListTwoIndexChanged(object sender, EventArgs e)
{
_listOne.ClearSelection();
}
change the control to RadioButton and add GroupName to them will do the trick:
<asp:RadioButton ID="_listOne" runat="server" GroupName="xx" AutoPostBack="true" OnSelectedIndexChanged="ListOneIndexChanged">
</asp:RadioButton>
<asp:RadioButton ID="_listTwo" runat="server" GroupName="xx" AutoPostBack="true" OnSelectedIndexChanged="ListTwoIndexChanged">
</asp:RadioButton>
try to use just one radibuttonlist and options.
<asp:RadioButtonList id="radiolist1" runat="server" OnSelectedIndexChanged="ListOneIndexChanged">
<asp:ListItem selected="true">Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
<asp:ListItem>Item 4</asp:ListItem>
</asp:RadioButtonList>

Dropdown box chained filters not working?

I am trying to implement a simple filter from one dropdown box to another.
The second dropdown box deos not populate (with any item) when I select an item from the first dropdown.
I am not sure what I am missing.
Please advise.
Here is the ascx code:
<div id="SubmitSection" style="width:auto; height:auto;" class="SubmitSectionStyle">
<div id="DropdownSection" style="text-align:center;">
<asp:DropDownList ID="DropDown1" runat="server" AppendDataBoundItems="true"
onselectedindexchanged="Type_SelectedIndexChanged" ToolTip="Select Category">
<asp:ListItem Text="--Select Category--" Value="" />
<asp:ListItem Value="1">Department</asp:ListItem>
<asp:ListItem Value="2">Safety</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDown2" runat="server">
<asp:ListItem Text="--Select One--" Value="" />
</asp:DropDownList>
</div>
And here is my code behind:
protected void Type_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDown1.SelectedValue == "1")
{
DropDown2.Items.Clear();
DropDown2.Items.Add("DeptTest");
DropDown2.DataBind();
}
else if (DropDown1.SelectedValue == "2")
{
DropDown2.Items.Clear();
DropDown2.Items.Add("SafetyTest");
DropDown2.DataBind();
}
}
In your first dropdownlist, set AutoPostBack="True"
AutoPostBack = "true" // AutoPostBack attribute is missing in DropDown1 due to which the event does not fire
// change your dropdown1 code as
<asp:DropDownList ID="DropDown1" AutoPostBack = "true" runat="server" AppendDataBoundItems="true"
onselectedindexchanged="Type_SelectedIndexChanged" ToolTip="Select Category">
<asp:ListItem Text="--Select Category--" Value="" />
<asp:ListItem Value="1">Department</asp:ListItem>
<asp:ListItem Value="2">Safety</asp:ListItem>
</asp:DropDownList>
You need to set auto postback to true if you want it to update the page on item change.
<asp:DropDownList ID="DropDown1" AutoPostBack="True" runat="server" AppendDataBoundItems="true" onselectedindexchanged="Type_SelectedIndexChanged" ToolTip="Select Category" >
<asp:ListItem Text="--Select Category--" Value="" />
<asp:ListItem Value="1">Department</asp:ListItem>
<asp:ListItem Value="2">Safety</asp:ListItem>
</asp:DropDownList>
You might also want to consider wrapping both these DropDownList controls in an update panel, so that you don't refresh the whole page every time a user changes a selection.

Categories