Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Mar 27, 2007 10:00 AM by ssab
Mar 22, 2007 01:07 PM|LINK
I need to set value of drop down list on page load. I am doing something like this:
protected void Page_Load(object sender,
DropDownList1.SelectedValue = Convert.ToString(Session["UID"]); //Or any default value; say 1.
But this is not setting value on page load. Only set value when i change the value of dropdown list. You can test this by running this code. This will not show the selected value in textbox for the 1st time when page loads.
Mar 22, 2007 01:57 PM|LINK
this works fine with me like this:
in .aspx page
in .cs page
ddlDropDown.SelectedIndex = 1;
Make sure that the index you are referring to exists...
Mar 22, 2007 02:27 PM|LINK
You need to make sure that Session["UID"] value is a valid item value in the DropDownList. And you should change the code in Page_Load like this
DropDownList1.SelectedValue = Convert.ToString(Session["UID"]);
Hope this helps
Mar 24, 2007 03:53 AM|LINK
Guys thanks for your reply. But this is not working even by using post back on the page.
The value in the session is valid and the selected item in the dropdown list is also being shown. But it dont populate the selected value in text box.
Infact my requirement is to populate another DDL from the selected value of list 1 (but by making the DDL1 invisible at that time). DDL2 is populating against value of DDL1, untile DDL1 is visible true. But when i make DDL1 hide, nothing is coming DDL2.
Thats why i tried to show the selected value of DDL1 in a text box and it is not appearing.
Any suggestion, if i can do this job in some other way.
Mar 24, 2007 04:43 AM|LINK
Mar 24, 2007 09:38 AM|LINK
The code you have written is correct. It should work just fine. Doesn't matter whether u set the DDL's visible property to false.
Problem should be something else. From where does your DDL gets the value. Are you binding it to a Database. or just static items.
Please provide some more details on how you are navigating from other page to this page.
Mar 24, 2007 10:39 AM|LINK
The enableview state of text box is true.
I am binding DDL with database. but it is not giving correct results for me. This thing was also quite unusual for me. Let me explain my scenario a bit more explained.
1. There are 2 drop down lists on the page. (Both are populating from database).
2. List 2 is being populated from selected value of list 1.
3. Now i have to show both lists for role1 and only 2nd list for role2 (So for role2 list2 will be populated from value of session, and i am setting the session value as selected value list1 in this condition so that same code work for both roles).
3. The problem is coming with the role 2, when i set the value for list1 and make it invisible, nothing is populated in list2. Even though the selected value is correct and it exist.
Any thoughts on it. Or if any of u guys can experiment this thing. As this little thing has put me in trouble from last 2 days.
Mar 24, 2007 11:01 AM|LINK
At first make sure that your DDL is getting correct values from the Database.
I have tried a similar case which is working file for me.. here is my aspx page
<form id="form1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"
DataTextField="Name" DataValueField="CategoryID" >
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:OmsConnectionString %>"
OnSelected="SqlDataSource1_Selected" SelectCommand="SELECT [CategoryID], [Name] FROM [Category]">
Class file :
protected void Page_Load(object sender, EventArgs e)
DropDownList1.SelectedValue = Convert.ToString(2); // 2 is the Category Id .
TextBox1.Text = DropDownList1.SelectedValue;
This is working very much file. Please try something like this.
Hope this helps to find your prob.
Mar 25, 2007 09:27 AM|LINK
Thanks for your detailed reply. I am doing exactly like this. I am getting correct value as selected in drop down list. But Text box is empty. Are you getting the selected value in text box?
Mar 25, 2007 10:06 AM|LINK
Yep. My TextBox shows the SelectedValue of the DDL.
Please post your code. It will be easy to trace where the problem is.