Reputation: 3
I have a dropdown list with hard coded values:
<asp:DropDownList ID="BulletinTypeDropDown" runat="server">
<asp:ListItem Selected="True" Text="--Select--" Value="--Select--"></asp:ListItem>
<asp:ListItem Text="News" Value="News"></asp:ListItem>
<asp:ListItem Text="report" Value="report"></asp:ListItem>
<asp:ListItem Text="System" Value="System"></asp:ListItem>
<asp:ListItem Text="Reminder" Value="Reminder"></asp:ListItem>
</asp:DropDownList>
I want to use this to insert and edit values from a db. For example when adding a record the user selects 'report' and 'report' gets inserted into the database. Then if the user goes to edit the page since the value in the db is 'report' then the dropdownlist must show 'report' is selected. But i am able to store the value in DB successfully but i couldn't retrieve the selected page during the edit page. I am using Entity frame work so, i tired to so many ways but couldn't get the value in edit page. Here is the way i tried.
DropDown.SelectedValue = bulList.intype.ToString();
//intype is the value i am getting from DB and it is returning the currect value but passing null in the DropDown.selectedvalue.
Can somebody please help me what are the other ways i can work to get the values in my dropdown list.
Thanks
Upvotes: 0
Views: 20827
Reputation: 11
Page_Load time, use an IsPostBack as follows. Can you use the value assignment to the dropdown just below?
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack) return;
string s = bulList.intype.ToString().Trim();
DropDown.SelectedValue = s;
}
Upvotes: 0
Reputation: 5
Here is solution that may work for you. so create your dropdown list.
<asp:DropDownList
ID="DropDownList1"
runat="server"
AutoPostBack="true"
onload="DropDownList1_Load" >
</asp:DropDownList>
protected void DropDownList1_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<string> DDLlist = new List<string>();
DDLlist.Add("Report");
DDLlist.Add("News");
DropDownList1.DataSource = DDLlist;
DropDownList1.SelectedValue = "Report";
DropDownList1.DataBind();
}
}
You can add to the DDLlist to add new items dynamically.
Upvotes: 0
Reputation: 2689
So the dropdownlist stays on default value, if so find a binding method in your code probably inside page load.
Otherwise if bulList.intype
is returning the value there is no way I think the dropdownlist refuses to show selected value. May be check the returned value for spaces. Try this:
string s = = bulList.intype.ToString().Trim();
DropDown.SelectedValue = s;
Upvotes: 0
Reputation: 510
You will have to find the text on the dropdownlist and set the selected property to true Example:
//pass the string you want to set selected to true --> assuming in your case bulList.intype.ToString()
BulletinTypeDropDown.Items.FindByText(bulList.intype.ToString()).Selected = true;
You can also do similar by using FindByValue. More information here: http://forums.asp.net/t/1004541.aspx/1
Upvotes: 0