C# Updating a dropdown box with value from another dropdown box - c#

I have a dropdown box that lists authors names:
I need to have this box update with the selected value from a 2nd dropdown list.
This clears the values from the authors dropdown list but it does not update the box with the selected value from the 2nd dropdown list. What do I need to include to get the value of the lbAuthorList to display the selected value from DroopDownList1?
protected void update_SelectedItem(object sender, EventArgs e)
{
lbAuthorList.Items.Clear();
lbAuthorList.Text = DropDownList1.SelectedItem.Text;
}
<asp:DropDownList runat="server" ID="lbAuthors" style="float:left;"
DataSourceID="odsAuthorList" DataTextField="DisplayAuthorName" DataValueField="AuthorID"
onselectedindexchanged="lbUserList_SelectedIndexChanged"
AppendDataBoundItems="True" >
</asp:DropDownList>
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True"
DataSourceID="SqlDataSource2" DataTextField="Display_AuthorName" EnableViewState="false"
DataValueField="Display_AuthorName" OnSelectedIndexChanged="update_SelectedItem" AutoPostBack="true">
</asp:DropDownList>

You need to add new item to drop down list,
protected void update_SelectedItem(object sender, EventArgs e)
{
lbAuthorList.Items.Clear();
lbAuthorList.Items.Add(new ListItem(DropDownList1.SelectedItem.Text, DropDownList1.SelectedItem.Text));
lbAuthorList.Items.FindByValue(DropDownList1.SelectedItem.Text).Selected = true;
}

Related

Populating /Refreshing Drop down list based on user input

I am working on an asp.net application. I have a drop down list of customer account no. When i enter customer id card number in text box and click search button, the drop down should be populated with all the account numbers provided against that id card number previously. It works fine but when i add a new id card number and click search the drop down still retains the previous customer's account numbers. I want the drop down to empty and reppulate everytime i click Search and the item at value 1 i.e Other should stay constant rest of the list changes dynamically.
protected void Search_Click(object sender, EventArgs e)
{
ddl_accno.Items.Clear();
ddl_accno.Items.Add(new ListItem("Other", "0"));
string cnic = txt_cnic.Text;
BindControls.ControlBinder.BindDropDown(ddl_accno, UL.GetAccountNo(cnic),"ACCOUNT_NO","ACCOUNT_NO");
}
<td style="width:275px">
<label for="textfield">
Account no.</label>
<asp:DropDownList Font-Size="Small" ID="ddl_accno"
runat="server" AutoPostBack = "True"
Width="319px" AppendDataBoundItems="true"
onselectedindexchanged="ddl_accno_SelectedIndexChanged">
<asp:ListItem Value="0" Selected="True" Text="Select Account No"></asp:ListItem>
<asp:ListItem Value="1" Text="Other"></asp:ListItem>
</asp:DropDownList><br />
I am using UL.GetAccountNO() function to populate ddl with query. If the user selects other a text box will be visible where user can enter an account number apart from the ones in the drop down.
Pasting a sample code:
Design:
<asp:DropDownList ID="AssignedToDropDownList" runat="server" DataSourceID="UserLinqDataSource"
DataTextField="UserName" DataValueField="UserId" AppendDataBoundItems="true"
SelectedValue='<%# Bind("AssignedTo") %>'>
</asp:DropDownList>
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
DDLDataBind();
}
private void DDLDataBind()
{
AssignedToDropDownList.Items.Clear();
AssignedToDropDownList.Items.Add(new ListItem("--did not assign--", "0"));
AssignedToDropDownList.DataBind();
}

Store in a variable the SelectedValue of a DropDownList then call it

I am adding a user to a role in asp.net but i want to get the selected role from a dropdownlist. a single option is working for me but i need to implement a two choices dropD list.
public partial class Register : Page
{
protected void Selection_Change(object sender, EventArgs e)
{
var tr = TakeRole.SelectedValue; // store it in some variable
}
protected void CreateUser_Click(object sender, EventArgs e)
{
// code
if (result.Succeeded)
{
manager.AddToRole(user.Id, "SomeRoleName");
}
}
on aspx i have
<asp:DropDownList id="TakeRole" AutoPostBack="True" OnSelectedIndexChanged="Selection_Change" runat="server">
<asp:ListItem> Supplier </asp:ListItem>
<asp:ListItem Selected="True"> Customer </asp:ListItem>
</asp:DropDownList>
Please use a listbox on multi selection mode and restrict selection to 2 items. Else you can use dropdownlist with jQuery.
If you intend to use checkboxlist, then other than binding source, in check changed event count checked items to 2 and if more items are selected an alert is to be displayed.

Bind one Dropdown based on another Dropdown SelectedValue inside DataList - c#

I am using 3 DropDownList inside DataList. So, each row contains 3 DropDownList. DataList also contains DataKeyField.
Now, if user select value from DropDownList1 then I want to bind DropDownList2 and if user select value from DropDownList2 then I want to bind DropDownList3. I am using SelectedIndexChanged and I am able to get value of related DropDownList selected value. But, If user select DropDownList2 then I also need value of DropDownList1 and also need value of respected DataKeyField.
How to get this ???
My code Sample:
<asp:DataList ID="dlTest" runat="server" OnItemDataBound="dlTest_ItemDataBound"
DataKeyField="TestId">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> </asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"> </asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged"> </asp:DropDownList>
</ItemTemplate>
</asp:DataList>
Here, onSelectedIndexChanged of DropDownList3, I am able to get selectedValue of it but I also need respected row's selectedValue of DropDownList1 and DropDownList2 and also respected DataKeyField value.
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
var DropDownList3 = (DropDownList)sender;
string DDL3 = ((DropDownList)DropDownList3.NamingContainer.FindControl("DropDownList3")).SelectedValue;
// Also need selectedValue of DropDownList1 and DropDownList2 and DataKeyField.
}
I think you are almost there. You just need to. Using your code:
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
var DropDownList1 = (DropDownList)sender;
var DropDownList2 = (DropDownList)sender;
var DropDownList3 = (DropDownList)sender;
string DDL1 = ((DropDownList)DropDownList1.NamingContainer.FindControl("DropDownList1")).SelectedValue;
string DDL2 = ((DropDownList)DropDownList2.NamingContainer.FindControl("DropDownList2")).SelectedValue;
string DDL3 = ((DropDownList)DropDownList3.NamingContainer.FindControl("DropDownList3")).SelectedValue;
}
Either way I would do it a little bit different:
item valueddl1 = DropDownList1.SelectedValue;
And so on; which should work as weel and it is a little bit more simple.

Dropdown list value not displaying

hi i have gridview that when a row is selected, it populates into textboxes that are used to populate the gridview itself. the last field is a dropdownlist and its not displaying when the gridview is clicked. i set a breakpoint and see that its stuck on the first - 0 index. i dont know why it isnt moving forward... here is the code:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
....
if (DropDownListCurrency.Items.FindByValue(row.Cells[7].Text.ToString().Trim) != null)
{
DropDownListCurrency.SelectedValue = row.Cells[7].Text.ToString().Trim();
}
....
}
<asp:DropDownList ID="DropDownListCurrency" runat="server"
CausesValidation="True"
DataSourceID="CurrencyDropDownListDataSource"
DataTextField="Currency" DataValueField="Currency_ID"
AppendDataBoundItems="True">
<asp:ListItem Value="0" Text="<Select>" Enabled="True" Selected="False"></asp:ListItem>
</asp:DropDownList>
why you want take the value from textbox. Will better use DataKeyNames like this inside that event
GridViewRow row = GridView.SelectedRow;
int id = Convert.ToInt32(GridView.DataKeys[row.RowIndex].Value);
this work if you have only one value in DataKeyName if you look not there a index if you want have more than one value use this
int id = Convert.ToInt32(GridView.DataKeys[row.RowIndex].Values["FirstValue"]);
string name = Convert.ToString(GridView.DataKeys[row.RowIndex].Values["SecondValue"]);
Does the DropDownList contains the words that you wanna show displayed?
You need to set the AutoPostBack property to True:
it should be :
<asp:DropDownList ID="DropDownListCurrency" AutoPostBack="True" runat="server"
CausesValidation="True"
DataSourceID="CurrencyDropDownListDataSource"
DataTextField="Currency" DataValueField="Currency_ID"
AppendDataBoundItems="True">
<asp:ListItem Value="0" Text="<Select>" Enabled="True" Selected="False"></asp:ListItem>
</asp:DropDownList>
made a simple error...
called the wrong field was supposed to be value (Currency_ID) and not the text (Currency)
DropDownListCurrency.SelectedValue = GridView1.DataKeys[row.RowIndex].Values["Currency_ID"].ToString().Trim();

How to get the previous item on DropDownList before OnSelectedIndexChanged fires the event

How would I get the previous item on DropDownList before OnSelectedIndexChanged fires the event?
Example: I had a DropDownList that has names as its items ("John", "Mark"). By default the SelectedIndex is "John". Upon changing its index and selecting "Mark" the event OnSelectedIndexChanged will be triggered. When I use ddlName.SelectedIndex it will return only the index for "Mark" which I want to get is the index of "John".
You can't capture an event prior to the change, but you could easily store the previous value in a variable. Each time SelectedIndexChanged is fired, use the previous value and then set it to the new index (for the next time the event fires). To handle the case when it's a new selection (from the default), you can either set the variable when the page loads, or allow it to be null and have that alert you to the fact it's a new selection (which you can then handle however you like).
<asp:DropDownList ID="ddlName" runat="server" AutoPostBack="true"
onselectedindexchanged="ddlName_SelectedIndexChanged">
<asp:ListItem Text="John" Value="1"></asp:ListItem>
<asp:ListItem Text="Mark" Value="2"></asp:ListItem>
<asp:ListItem Text="Jim" Value="3"></asp:ListItem>
</asp:DropDownList>
.cs file code here:
public static int PreviousIndex;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlName.AppendDataBoundItems = true;
ddlName.Items.Add(new ListItem("Other", "4"));
PreviousIndex = ddlName.SelectedIndex;
}
}
protected void ddlName_SelectedIndexChanged(object sender, EventArgs e)
{
string GetPreviousValue = ddlName.Items[PreviousIndex].Text;
Response.Write("This is Previously Selected Value"+ GetPreviousValue);
//Do selected change event here.
PreviousIndex = ddlName.SelectedIndex;
}
You could use the e.OldValues property.
<asp:DropDownList ID="esDropDownList" runat="server" DataSourceID="SqlDataSourceddlEnrolmentStatus" DataTextField="EnrolmentStatusDescription" DataValueField="EnrolmentStatusID" SelectedValue='<%# Bind("StudentEnrolmentStatus") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceddlEnrolmentStatus" runat="server"
ConnectionString="<%$ ConnectionStrings:ATCNTV1ConnectionString %>" SelectCommand="SELECT [EnrolmentStatusID], [EnrolmentStatusDescription] FROM [tblEnrolmentStatuses] ORDER BY [EnrolmentStatusID]">
</asp:SqlDataSource>
And in code behind (assuming your dropdownlist is in a formview) ...
protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
{
..
String msg = "This is the new value " + e.NewValues["StudentEnrolmentStatus"].ToString()+ " and this is the old value " + e.OldValues["StudentEnrolmentStatus"].ToString();
..
}

Categories