Dynamically adding dropdownlist

asp.net , csharp , db Philadelphia, United States
  • 12 years ago

    Hi all

          I am new to asp.net. I am adding dropdownlist dynamically from codebahind. But i m not writing its code in PostBack, as i want these controls to be added even if the Page postbacks. Now the problem is that when Page PostBacks these controls are added again in form & my Dropdownlist selected index is again set to 0. Can someone tell me how to solve this problem... Plz.. thnx in advance... My code goes like this...

     cmd = new SqlCommand("select distinct attrName, attrID from ProductAttr Where ProdID='" + int.Parse(Request["ProdID"].ToString()) + "'", con);

    con.Open();

    reader = cmd.ExecuteReader();

    if (reader.HasRows)

    {

    while (reader.Read())

    {

    HtmlTableRow tr = new HtmlTableRow();

    HtmlTableCell td1 = new HtmlTableCell();

    Label lblattrname = new Label();

    lblattrname.Text = reader.GetString(0);

    td1.Controls.Add(lblattrname);

    tr.Controls.Add(td1);

    HtmlTableCell td2 = new HtmlTableCell();

    SqlConnection con1 = new SqlConnection(strcon);

    ds = new DataSet();SqlDataAdapter adap = new SqlDataAdapter("select SubAttrValue, SubAttrID from SubProductAttr Where AttrID='" + reader.GetInt32(1) + "'", con1);

    adap.Fill(ds);

    DropDownList DropAttrValue = new DropDownList();

    DropAttrValue.AutoPostBack = true;DropAttrValue.ID = "DropAttrValue" + count;

    DropAttrValue.DataSource = ds;

    DropAttrValue.DataTextField = ds.Tables[0].Columns[0].ToString();

    DropAttrValue.DataBind();

    td2.Controls.Add(DropAttrValue);

    tr.Controls.Add(td2);

    tableprdinfo.Controls.Add(tr);

  • 12 years ago
    Not sure what you are say? Are you saying you are adding the dropdownlist in the pageLoad event? And each time the page reloads it adds the same values to dropdown list again so you end with two sets of the same values? If so then use the

    EnableViewState="false"

    In C# on the .aspx page   <%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" EnableViewState="false" Inherits="test" %>

    Hope this helps.

  • 12 years ago

    Hi

    I have written this code in Page_Load event... as i want to load this Dropdownlist everytime my Page loads.

    But I also want to get the Dropdownlist selected value . But when i select an item in Dropdownlist, It Postbacks & its selected value is changed. I tried EnableViewState="false". But it is still same.

     Actually what i want is to add ProductAttr such as color, shape,... & their values in Dropdownlist like red, green,..

    I want this everytime the page loads. This value comes frm database & it may vary in no. Can u tell me if i can do it in some other way. 

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Perl - The only language that looks the same before and after RSA encryption.” - Keith Bostic