Showing posts with label menu. Show all posts
Showing posts with label menu. Show all posts

Thursday, March 29, 2012

Poplulating Text Field with DropDown selection

Hi folks. Here's a simple question. Trying to populate some text fields based on a dropdown menu selection, but it doesn't work. When a selection is made in the dropdown menu, it seems to default to the selectedValue =2. Not sure why. Any advice would be much appreciated. Thanks! Here's the code:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { TextBox1.Text =""; TextBox2.Text =""; TextBox3.Text ="";if (DropDownList1.SelectedValue =="0") TextBox1.Text ="blah";if (DropDownList1.SelectedValue =="1") TextBox1.Text ="blahblah";if (DropDownList1.SelectedValue =="2") TextBox1.Text ="blahblahblah"; }

Are you dynamicalluy populating the dropdown list in your code behind? If you are, the Page_OnLoad event is running which is populating your dropdownlist on every postback. To make sure this doesnt happen, you should populate when teh page is not posted back. in other words...

if(!Page.IsPostback){// populate my dropdownlist}

If you are not dynamically populating the dropdownlist, please make sure your viewstate is turned on, that way the server knows that index to preserve when the page is loaded again.

EnableViewState="true"

Hope this helps.


If you are adding the items to the dropdown through code in Page_Load then check if they are inside If (!Ispostback) block.


hi..

i think u can have a break point in the page load or start of selected index changed event..

and find the value..

if not..

check in the design view that item 2 isSelected=true..

correct me if i'm wrong..


Thanks to all for the suggestions. The dropDownList was statically populated from the control properties in the ASPX page. I tinkered with this for a long while, but could NOT get it to work. So, I changed approach and populated the list by making an array in the code, and now it works as designed. Thanks again. Here is the code, in case anybody is interested.

protected void Page_Load(object sender, EventArgs e) {if (!IsPostBack) {// 2 dimensional array for dropDown liststring[,] forms = { {"blah","0"}, {"blah blah","1"}, {"blah blah blah","2"} };// populate listfor (int i = 0; i < forms.GetLength(0); i++) {//add both text and value DropDownList1.Items.Add(new ListItem(forms[i, 0], forms[i, 1])); } } }protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) {if (DropDownList1.SelectedIndex != -1) { TextBox1.Text = DropDownList1.SelectedItem.Text; } }

FYI... regarding the orginal post, the reason the textBox wasn't populated correctly was a simple syntax error. Used cascading IF statement without ELSE IF or switch statement.

Wednesday, March 21, 2012

Populating a dropdown box

Hi all,

I have populated one dropdown menu using data from a database using the following code

Sub Page_Load (seander as Object, e as EventArgs) Year.DataSource = GetYear() Year.DataBind()End Sub Function GetYear() As System.Data.SqlClient.SqlDataReader Dim connectionString As String = "server='localhost'; user id='sa'; password='eoyson'; Database='PupilRoll'" Dim sqlConnection As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(connectionString) Dim queryString As String = "SELECT [Years].* FROM [Years]" Dim sqlCommand As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(queryString, sqlConnection) sqlConnection.Open Dim dataReader As System.Data.SqlClient.SqlDataReader = sqlCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection) Return dataReader End Function
I am now trying to populate another dropdown list depending on the information selected from the previous dropdown list.
Cheers!

Try the tutorial right on this site:

Master Detail Filtering with Two Dropdownlists