DropDownList1.SelectedValue = Convert.ToString(Session["UID"]); //Or any default value; say 1.
TextBox1.Text =DropDownList1.SelectedValue;
}
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.
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.
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.
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?
ssab
Member
11 Points
8 Posts
Set value of dropdown list on page load
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, EventArgs e)
{
DropDownList1.SelectedValue = Convert.ToString(Session["UID"]); //Or any default value; say 1.
TextBox1.Text =DropDownList1.SelectedValue;
}
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.
tompy_nation
Contributor
5054 Points
1268 Posts
Re: Set value of dropdown list on page load
Mar 22, 2007 01:57 PM|LINK
this works fine with me like this:
in .aspx page
<form id="form1" runat="server"> <div> <asp:DropDownList ID="ddlDropDown" runat="server"> <asp:ListItem Text="test1" /> <asp:ListItem Text="test2" /> </asp:DropDownList> </div> </form>in .cs page
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlDropDown.SelectedIndex = 1;
}
Make sure that the index you are referring to exists...
}
spvlong
Participant
1025 Points
187 Posts
Re: Set value of dropdown list on page load
Mar 22, 2007 02:27 PM|LINK
Hi ssab,
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
protected void Page_Load(object sender, EventArgs e)
{
If (!IsPostBack)
{
DropDownList1.SelectedValue = Convert.ToString(Session["UID"]);
TextBox1.Text =DropDownList1.SelectedValue;
}
}
Hope this helps
The Vietnamese Stock Market and Economy
ssab
Member
11 Points
8 Posts
Re: Set value of dropdown list on page load
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.
Rivarolle
Member
484 Points
81 Posts
Re: Set value of dropdown list on page load
Mar 24, 2007 04:43 AM|LINK
spy@g
Participant
834 Points
154 Posts
Re: Set value of dropdown list on page load
Mar 24, 2007 09:38 AM|LINK
Hi
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.
Thanks
[ If my post helps you please mark as answer ]
ssab
Member
11 Points
8 Posts
Re: Set value of dropdown list on page load
Mar 24, 2007 10:39 AM|LINK
Hi
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.
Thanks alot.
spy@g
Participant
834 Points
154 Posts
Re: Set value of dropdown list on page load
Mar 24, 2007 11:01 AM|LINK
Hi
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
I have a DropdownList and I am binding it with SqlDataSource from my Category Table with a query <Select CategoryID,Name from Category><form id="form1" runat="server"> <div> <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="CategoryID" > </asp:DropDownList><br /> <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]"> </asp:SqlDataSource> </div> </form>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.
Thanks
[ If my post helps you please mark as answer ]
ssab
Member
11 Points
8 Posts
Re: Set value of dropdown list on page load
Mar 25, 2007 09:27 AM|LINK
Hi
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?
Thanks
spy@g
Participant
834 Points
154 Posts
Re: Set value of dropdown list on page load
Mar 25, 2007 10:06 AM|LINK
Hi
Yep. My TextBox shows the SelectedValue of the DDL.
Please post your code. It will be easy to trace where the problem is.
Thanks
[ If my post helps you please mark as answer ]