在Asp.Net Gridview C#中获取动态创建的控件的值

时间:2015-10-15 10:33:12

标签: c# asp.net .net updatepanel

我正在使用Asp.Net Gridview并在第三列中动态添加文本框。 第三列有一个PlaceHolder,我在那个placeHolder中动态添加Textbox。这部分工作正常。现在,如果我在该文本框中输入任何文本,我怎样才能获得用户输入的值。?

2 个答案:

答案 0 :(得分:0)

以下是我的代码:

 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;
            }
        }

答案 1 :(得分:0)

您可以通过`FindControl(“ControlID”)访问文本框,例如:

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

为了得到它的价值,你会写:

   String txt_value = txtbox.Text;

您可以在创建TextBox对象后访问文本框的任何属性,并在FindControl()

的帮助下通过它的ID找到它