How to Access value of dynamically created control?

Posted by sharad on Stack Overflow See other posts from Stack Overflow or by sharad
Published on 2012-04-10T10:53:47Z Indexed on 2012/04/10 11:29 UTC
Read the original article Hit count: 241

Filed under:
|
|

i have asp.net web application in which i need to create form dynamically depend upon table which user selected.however,i created form dynamically with it's control like text box,drop downlist etc. but not able to access those controls at particular button click event.

My form scenario is like this:

i have dropdownlist in which i display list of tables,button control say display( on click event which i redirect page with some addition in querystring so that at page init it dynamically genrate controls) and another button control which used to access this controls value say Submit and store in database. This is my code:

At Display button Click event:

Response.Redirect("dynamic_main.aspx?mode=edit&tablename=" + CMBTableList.SelectedItem.ToString());

This is code For page.Init level

    if (Request.QueryString["mode"] != null)
                {
if (Request.QueryString["mode"].ToString() == "edit")
                        {
                            Session["tablename"] = Request.QueryString["tablename"].ToString();
                            // if (Session["tablename"].ToString() == CMBTableList.SelectedItem.ToString())
                            //{
                            createcontrol(Session["tablename"].ToString());
                            // }
                        }
                }

i have used createcontrol() which genrate controls

Code For Submit Button:

 string[] contid=controlid.Value.Split(',');
 MasterPage ctl00 = FindControl("ctl00") as MasterPage;

                ContentPlaceHolder cplacehld = ctl00.FindControl("ContentPlaceHolder2") as ContentPlaceHolder;
                Panel panel1 = cplacehld.FindControl("plnall") as Panel;
                Table tabl1 = panel1.FindControl("id1") as Table;
                foreach (string sin in contid)
                {

                    string splitcvalu = sin.Split('_')[0].ToString();
                    ControlsAccess contaccess = new ControlsAccess();
                    if (splitcvalu.ToString() == "txt")
                    {

                        TextBox txt = (TextBox)tabl1.FindControl(sin.ToString());
                        //TextBox txt = cplacehld.FindControl(sin.ToString()) as TextBox;
                        val = txt.ID;
                    }
                    else
                    {
                        DropDownList DDL = cplacehld.FindControl(sin.ToString()) as DropDownList;
                    }
                }

Here i am no able to access this textbox or drop downlist.i am getting id of particular control from hidden field controlid in which i have stored it during genrating controls. i have used findcontrol() it works in case of finding masterpage,contentplaceholder,table etc. but in case of textbox or other control it doesnt work it shows it returns null. Anyone can help me.

Thanks

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET