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();
}
Related
Im facing a pretty weird problem today.
I created a DropDownList which adds the selected Item to a List. The List will be binded to the ListView.
This is the Dropdown:
<asp:DropDownList ID="ddlChooseNewApplication" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem Selected="True">Bitte wählen</asp:ListItem>
<asp:ListItem Value="windows">Windows</asp:ListItem>
<asp:ListItem Value="mail">E-Mail</asp:ListItem>
<asp:ListItem Value="app1">App1</asp:ListItem>
<asp:ListItem Value="app2">App2</asp:ListItem>
<asp:ListItem Value="app3">App3</asp:ListItem>
</asp:DropDownList>
Next, when a Item has been clicked it runs the following code:
//This is a global variable
List<NewApplicationModels> applicationName = new List<NewApplicationModels>();
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string value = ddlChooseNewApplication.SelectedValue.ToString();
applicationName.Add(new NewApplicationModels(){ ApplicationName = value});
ListViewNewApplications.DataSource = applicationName;
ListViewNewApplications.DataBind();
}
It will be added to a List<> which should add the selected application to the ListView which looks like this:
<asp:ListView ID="ListViewNewApplications" runat="server">
<ItemTemplate>
<br /><br />
<%# Eval("ApplicationName") %><br />
<asp:Label ID="Label3" runat="server" Text="Titel"></asp:Label><br />
<asp:TextBox ID="tbNewTitle" runat="server"></asp:TextBox><br /><br />
<asp:Label ID="Label4" runat="server" Text="Beschreibung"></asp:Label><br />
<asp:TextBox ID="tbNewDescription" runat="server" TextMode="MultiLine"></asp:TextBox><br /><br />
</div>
</ItemTemplate>
</asp:ListView>
Adding a single Item to the ListView works. The problem is, if I select a new Item in the DropDown, the current object in the ListView will be overwritten. I want it to create a new Item under the current Item.
Where is the mistake?
Many thanks in advance
Editted: Just read here that static property is per application domain, not per user, So the solution should change to use Session State
//Add using on top of .cs
using System.Linq;
//In cs class
public partial class name_of_class : Page
{
private List<NewApplicationModels> applicationName = new List<NewApplicationModels>();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Do page_load stuff....
Session.Remove("name_here"); //reset the Session when first page load
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//Get List from Session
applicationName = (List<NewApplicationModels>)Session["name_here"];
if (applicationName == null)
applicationName = new List<NewApplicationModels>();
string value = ddlChooseNewApplication.SelectedValue.ToString();
if (!applicationName.Any(item => item.ApplicationName == value))
{
applicationName.Add(new NewApplicationModels(){ ApplicationName = value});
Session["name_here"] = applicationName;
ListViewNewApplications.DataSource = applicationName;
ListViewNewApplications.DataBind();
}
}
}
For static property is per application domain, 2 stranger people from 2 different place browse the same page in same server (app domain) will all see it.
That means when userA change dropdownlist 2 times and the listview have 2 items, then userB browse and change dropdownlist, he may get 3 items in listview, 2 of userA and 1 of his recently choose (he may get nothing too).
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.
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;
}
I have a form containing two DropDowns lists:
Marital Status
No. of Children
Now I would like to enable the No. of Children DropDown upon selection of following items in the Marital Status DropDown:
Widow
Divorced
Awaiting Divorce
How can I do it?
In the MaritalStaus DropDownList Selected Index changed event, If the selected values match your options then enable the NoOfChild DropDownList.
protected void MaritalStaus_SelectedIndexChanged(object sender, EventArgs e)
{
//Match the selected value here : for Example:
if (MaritalStaus.SelectedValue.Equals("Divorced") || /*Other Comparisions */)
{
NoOfChild.Enabled = true;
}
}
DropDown lists have ListItem collection in them. Each ListItem has a Text and a Value. Try setting the text as "Divorced" and value as "D" or better to an integer like "1", something similar to ID. You will get these Text/Value from a Database table if you are retrieving it from the Database.
Make the No. of Children DropDown Enabled = false by default and then Enable = true as explained in the code snippet above by ebad86.
you can also use cascading dropdown from ajaxcontroltoolkit
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/CascadingDropDown/CascadingDropDown.aspx
.aspx
<asp:DropDownList ID="ddlMaritalStatus" runat="server" AutoPostBack="true"
onselectedindexchanged="ddlMaritalStatus_SelectedIndexChanged">
<asp:ListItem Text="" />
<asp:ListItem Text="Widow" />
<asp:ListItem Text="Divorced" />
<asp:ListItem Text="Awaiting Divorce" />
</asp:DropDownList>
<asp:DropDownList ID="ddlNoOfChildren" runat="server" Enabled="false">
<asp:ListItem Text="1" />
<asp:ListItem Text="2" />
<asp:ListItem Text="3" />
<!-- and so on -->
</asp:DropDownList>
aspx.cs
protected void ddlMaritalStatus_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlMaritalStatus.SelectedItem.Text == "Widow") // if widow is selected
ddlNoOfChildren.Enabled = true;
else
ddlNoOfChildren.Enabled = false;
}
Just wanna ask u guys for help. I have this dropdown menu in my abc.aspx page. There, user will choose month and enter the expenses and prices in the textbox provided. It will be saved in session :
session["month"]= dropdownlist1.selectedvalue;
session["expense1"] - textbox1.text;
session["price1"] - textbox2.text;
server.transfer ("sdf.aspx");
In the next page, the entered data will be viewed in label:
Label1.Text = session ["month"].ToString();
Label2.Text = session ["expense1"].ToString();
Label3.Text = session ["price1"].ToString();
Ok, my question is, how can make the month in drop down menu can be selected only once? Lets say, if the user choose Febuary, next time if he login, he cant choose the febuary anymore. I have used this code:
asp:DropDownList ID="DropDownList2" runat="server"
onchange="if(this.value!='Please choose') this.disabled='true';" Font-Bold="True">
<asp:ListItem>Please choose</asp:ListItem>
<asp:ListItem>January</asp:ListItem>
<asp:ListItem>Febuary</asp:ListItem>
<asp:ListItem>March</asp:ListItem>
But the problem is when i chose March, in the next page it supposed to show March isnt it? But, it shows 'Please choose'. So, i really hope there's someone here can help me out. Thank you.
Why not try the following:
ASPX
<asp:DropDownList ID="DropDownList1" runat="server" Font-Bold="True" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Text="Please choose" Value=""></asp:ListItem>
<asp:ListItem Text="January" Value="January"></asp:ListItem>
<asp:ListItem Text="February" Value="February"></asp:ListItem>
<asp:ListItem Text="March" Value="March"></asp:ListItem>
</asp:DropDownList>
C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (Session["Month"] != null)
{
if (DropDownList1.SelectedValue == Session["Month"])
{
DropDownList1.SelectedValue = string.Empty;
}
else
{
Session["Month"] = DropDownList1.SelectedValue;
}
}
}
Make this is the ASPX and C# on your first page.