views:

1166

answers:

2

hello

I created dropdownlist at runtime when a button is clicked.and i palced another button to get the selected text from dynamic dropdownlist.When i try to retrieve the selected text from dropdownlist it gives me the error called object reference not set, following is my code.

TableRow tr;
    TableCell tc;
    DropDownList dp;
    TextBox txt;
    protected void Button1_Click(object sender, EventArgs e)
    {

        int no = int.Parse(TextBox1.Text);
        for (int i = 0; i < no; i++)
        {
            tr = new TableRow();
            tr.BorderStyle = BorderStyle.Groove;
            for (int j = 0; j < 1; j++)
            {
                tc = new TableCell();
                tc.BorderStyle = BorderStyle.Groove;
                dp = new DropDownList();
                //form1.Controls.Add(dp);
                txt = new TextBox();
                dp.Items.Add("hello");
                tc.Controls.Add(dp);
                tc.Controls.Add(txt);
                tr.Cells.Add(tc);
            }

            Table1.Rows.Add(tr);

        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {

        TextBox1.Text =((DropDownList)this.FindControl("dp")).SelectedItem.Text;


    }
+4  A: 

You can't do it this way. Remember that on every request, you get a new page object, and new copies of all the controls in it. Any control you add dynamically must be added the same way every single time, otherwise it won't exist.

In this case, you add it once, when the button is clicked. When you click button2, a request is generated and a new page object is created that no longer has your dropdownlist, because it is only ever added in the button1 handler.

The easiest thing to do would be add your dropdownlist to the page normally but just set Visible to false. Then when they click button 1, set Visible to true. This will ensure that your dropdownlist will always be present.

Dynamic controls are tricky, and should be avoided when possible, especially if you're new to ASP.Net.

womp
A: 

I am agree with @womp. Every time page is posted back, ASP.NET loads the web page in its original state and tweaks the page according to this new information. So, try to write code into page_load event especially you are adding controls to the page dynamically.

 protected void Page_Load(object sender, EventArgs e)
    {
        TableRow tr;    
        TableCell tc;    
        DropDownList dp;    
        TextBox txt;
        if (TextBox1.Text == "")
            TextBox1.Text = "0";

        int no = int.Parse(TextBox1.Text);

        for (int i = 0; i < no; i++)       
        {            
            tr = new TableRow();
            tr.BorderStyle = BorderStyle.Groove;            
            for (int j = 0; j < 1; j++)
            {                
                tc = new TableCell();
                tc.BorderStyle = BorderStyle.Groove;
                dp = new DropDownList();
                dp.ID = "DP" + i + "" + j;
                //form1.Controls.Add(dp);                
                txt = new TextBox();
                txt.ID = "TX" + i + "" + j;
                dp.Items.Add("** Select **");
                dp.Items.Add("hello");
                dp.SelectedIndexChanged += new EventHandler(dp_SelectedIndexChanged);
                tc.Controls.Add(dp);                
                tc.Controls.Add(txt);                
                tr.Cells.Add(tc);            
            }            
            Table1.Rows.Add(tr);        
        } 

    }

    void dp_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList d=(DropDownList)sender;
        TextBox tx = (TextBox)FindControl("TX" + d.ID.Substring(2, d.ID.Length -2));

        Label1.Text = d.SelectedValue + " " + d.ID + " " + tx.Text;
    }
adatapost
Unfortunately, this isn't that advisable either. If you're going to use dynamic controls, you should add them in Page_Init. While your example will work for basic cases with static data, ViewState will not be tracking controls that are added after Init. If you need to databind your controls, you'll run into all kinds of problems wondering where your data is after the postback.
womp