简体   繁体   中英

Get values of Dynamically Created Controls inside Asp.Net Gridview C#

I am using Asp.Net Gridview and adding a text box dynamically in third column. The third column has a PlaceHolder and i am adding Textbox inside that placeHolder dynamically. This part is working fine. Now if i entered any text inside that textBox, how can i be able to get the value entered by the user.?

Below is my code :

 protected void ddlChangeSubType_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddlChangeSubType = (DropDownList)sender;
            GridViewRow currentRow = (GridViewRow)ddlChangeSubType.NamingContainer;
            DropDownList ddlChangeType = currentRow.FindControl("ddlChangeType") as DropDownList;
            //TextBox txt = currentRow.FindControl("txt") as TextBox;

            PlaceHolder placehldr = currentRow.FindControl("placehldrDynamicCnrtl") as PlaceHolder;

            object objControl;

            rowIndex = currentRow.RowIndex;
            if (Session["DynamicControls"] != null)
            {
                for (int y = 0; y < CRFormGridView.Rows.Count; y++)
                {
                    if (((Dictionary<int, object>)Session["DynamicControls"]).TryGetValue(y, out objControl))
                    {
                        objControlsDict.Add(y, objControl);
                    }
                }
            }

            if (ddlChangeSubType != null && currentRow != null && ddlChangeSubType != null)
            {
                switch (ddlChangeType.SelectedItem.Text.ToUpper())
                {
                    case "UPDATE OFFER":
                        TextBox txtBox = new TextBox();
                        txtBox.Text = "Text Box Added";
                        txtBox.ID = "txt";
                        txtBox.ClientIDMode = ClientIDMode.Static;
                        txtBox.EnableViewState = true;
                        placehldr.Controls.Add(txtBox);
                        if (objControlsDict.ContainsKey(rowIndex))
                            objControlsDict.Remove(rowIndex);
                        objControlsDict.Add(rowIndex, txtBox);
                        break;

                    case "ADD COMPONENT":
                        Label lbl = new Label();
                        lbl.Text = "Label Added";
                        lbl.ID = "lbl";
                        lbl.ClientIDMode = ClientIDMode.Static;
                        lbl.EnableViewState = true;
                        placehldr.Controls.Add(lbl);
                        if (objControlsDict.ContainsKey(rowIndex))
                            objControlsDict.Remove(rowIndex);
                        objControlsDict.Add(rowIndex, lbl);
                        break;

                    case "UPDATE REQUEST":
                        break;

                    default:
                        break;
                }

                Session.Add("DynamicControls", objControlsDict);
            }
        }


  protected void placehldrDynamicCnrtl_PreRender(object sender, EventArgs e)
        {
            try
            {
                if (Page.IsPostBack)
                {
                    PlaceHolder placeHldr = (PlaceHolder)sender;
                    GridViewRow currentRow = (GridViewRow)placeHldr.NamingContainer;

                    objControlsDict = (Dictionary<int, object>)Session["DynamicControls"];
                    if (objControlsDict != null)
                    {
                        if (objControlsDict.ContainsKey(count) && objControlsDict[count] is TextBox)
                        {
                            TextBox txtBox = (TextBox)objControlsDict[count];
                            txtBox.Text = "Text Box Added";
                            txtBox.ID = "txt";
                            txtBox.ClientIDMode = ClientIDMode.Static;
                            txtBox.EnableViewState = true;
                            ((PlaceHolder)this.CRFormGridView.Rows[count].Cells[3].FindControl(
                                "placehldrDynamicCnrtl")).Controls.Add(txtBox);
                        }

                        if (objControlsDict.ContainsKey(count) && objControlsDict[count] is Label)
                        {
                            Label lbl = (Label)objControlsDict[count];
                            lbl.Text = "Label Added";
                            lbl.ID = "lbl";
                            lbl.ClientIDMode = ClientIDMode.Static;
                            lbl.EnableViewState = true;
                            ((PlaceHolder)this.CRFormGridView.Rows[count].Cells[3].FindControl(
                                "placehldrDynamicCnrtl")).Controls.Add(lbl);
                        }
                        count++;
                    }
                }
            }
            catch (Exception es)
            {
                throw;
            }
        }

You can access your Textbox by `FindControl("ControlID"), for example:

   TextBox txtbox = (TextBox)FindControl("txt");

and in order to get it's value, you'll write:

   String txt_value = txtbox.Text;

You can access any property of your textbox after making the TextBox object and finding it by it's ID with the help if FindControl()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM