1 Replies - 4594 Views - Last Post: 01 November 2011 - 04:15 AM Rate Topic: -----

#1 anantmtr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-October 11

The problem is cannot get the value from dropdownlist

Posted 29 October 2011 - 03:21 AM

I have declared
 DropDownList d;
    DropDownList dr;
as globally
My main problem is that i can not get the seleted value of dropdownlist .I want to insert it in database .I am creating dynamically dropdownlist

IN first button i have successfully created dynamic number of dropdownlist with help textbox1 and button1.
But while insertion through button 2 i do not get the value dropdownlsit
 dr = new DropDownList();
                dr = (DropDownList)(Panel3.FindControl(str));
                //Label la = new Label();
                //la.ID = "la" + i;
                //LiteralControl lit = new LiteralControl("<br></br>");
                value = dr.SelectedValue.ToString();


protected void Button1_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(TextBox1.Text);

try
{

for (int i = 0; i < a; i++)
{
d = new DropDownList();
d.ID = "Text" + i;
LiteralControl l1 = new LiteralControl("<br></br>");

d.Items.Add("Rahul");
d.Items.Add("Gautum");
d.Items.Add("anant");

//Session[""+i]=i;
//Session["current"] = i;
//Session["Product" + i] = d;
//Session["id" + i] = d.ID;
//Session["seleted" + i] = d.SelectedValue.ToString();
//d.SelectedIndexChanged += new EventHandler(d_SelectedIndexChanged);

//string st = "select doe_name from Doe_detail ";
//SqlCommand cmd = new SqlCommand(st, con1);
//SqlDataReader dr = cmd.ExecuteReader();
//while (dr.Read())
//{
// d.Items.Add(dr.GetString(0).ToString());
//}
Panel3.Controls.Add(d);
Panel3.Controls.Add(l1);


}

}
catch (Exception e1)
{
Label4.Text = "" + e1.Message.ToString();
}
}
void d_SelectedIndexChanged(object sender, EventArgs e)
{

int i;
i = (int)Session["current"];
dr = (DropDownList)Session["Product" + i];
dr.ID = (String)Session["id" + i];
Session["seleted" + i] = d.SelectedValue.ToString(); ;


}

protected void Button2_Click(object sender, EventArgs e)
{



int a = Convert.ToInt32(TextBox1.Text);
string value = "";
for (int i = 0; i < a; i++)
{
string str ="Text" + i;
dr = new DropDownList();
dr = (DropDownList)(Panel3.FindControl(str));
//Label la = new Label();
//la.ID = "la" + i;
//LiteralControl lit = new LiteralControl("<br></br>");
value = dr.SelectedValue.ToString();
//value = d.SelectedValue.ToString();
//DropDownList d = new DropDownList();
//dr = new DropDownList();
//dr = (DropDownList)Session["Productid" + i];
//dr.ID = "Text" + i;
//dr = (DropDownList)Session["Product" + i];
//dr.ID = (String)Session["id" + i];
//dr = (DropDownList)this.Page.FindControl("Text" + i);
//value = dr.SelectedValue.ToString();

//value = dr.SelectedValue.ToString(); ;
//value = dr.SelectedValue();
SqlConnection con = new SqlConnection("Data Source=ABC-0D78787890A;Initial Catalog=Amit;Integrated Security=True");

con.Open();

SqlCommand cmd1 = new SqlCommand("insert into niitstud values('" + value + "')", con);
cmd1.ExecuteNonQuery();
con.Close();
//Panel3.Controls.Add(la);
//Panel3.Controls.Add(lit);

}

}

Is This A Good Question/Topic? 0
  • +

Replies To: The problem is cannot get the value from dropdownlist

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: The problem is cannot get the value from dropdownlist

Posted 01 November 2011 - 04:15 AM

The thing to remember when you are creating controls dynamically in asp.net is that they have to be rebuilt in the same order and with the same ID's everytime that the page loads. Since dynamically created controls do not exist in ViewState you have to have a way to rebuild each of the dynamically added controls on any postback that involves them. The best way I've found to do this is to maintain a count of the controls that get added by using either a ViewState backed page property or by storing the count in a hidden control on the aspx page. Either way amounts to the same thing since ViewState is stored in a hidden control on the page also. The reason I choose not to use a Session variable for this is that these controls only apply to this one specific page so I do not need the state of the controls anywhere else in the asp.net application (and the point of ViewState is to store the state of the controls on the current page). A ViewState backed property looks something like the following:

    public partial class _Default : System.Web.UI.Page
    {
        public int DDLCount
        {
            get
            {
                // attempt to create an object from the ViewState with name DDLCount
                object temp = ViewState["DDLCount"];
                // If the object is null then return 0
                // else we cast it to int and return the value
                return temp == null ? 0 : (int)temp;
            }
            // Sets/Creates a ViewState element named "DDLCount"
            set { ViewState["DDLCount"] = value; }
        }



Notice that this property is placed within the page class. Now that we have a way to store how many dynamically added controls there are we also need a way to create the controls each time that the page is loaded (postback occurs). To simplify this we will create a simple function that we can then call from any other function or event for the page.

        private void CreateDropDownLists()
        {
            // I am just defaulting to 5, you should be
            // creating the count however is appropriate  
            // for you application
            if (DDLCount == 0)
                DDLCount = 5;

            // foreach number in the count we create/recreate a 
            // dropdownlist and add it to the panel
            for (int i = 0; i < DDLCount; i++)
            {
                DropDownList ddl = new DropDownList();
                ddl.ID = "Text_" + i;
                ddl.Items.Add(new ListItem("dropdownlist_" + i, "TestValue_" + i));

                Panel1.Controls.Add(ddl);
            }
        }



Now we can call this function in both the button click event that initially loads the dropdownlists as well as in the page_load event.

        protected void Page_Load(object sender, EventArgs e)
        {
            // we don't call the function on the
            // initial page load or if the DDLCount is 0
            if (IsPostBack && DDLCount > 0)
            {
                CreateDropDownLists();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            CreateDropDownLists();
        }



Now the only thing left is to get the values from these dropdownlists during a separate event such as another button click event.

        protected void Button2_Click(object sender, EventArgs e)
        {
            // For each dropdownlist here I am getting the
            // selected value from the control and adding it
            // to the text in a label control
            for (int i = 0; i < DDLCount; i++)
            {
                DropDownList ddl = Panel1.FindControl("Text_" + i) as DropDownList;

                Label1.Text += ddl.SelectedValue + " : ";
            }
        }



This is only an example and you should not try to simply copy and paste it since it is not designed specifically for your application but is rather meant to be an in depth example to get you going in the right direction.

This post has been edited by Nakor: 01 November 2011 - 04:15 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1