簡體   English   中英

在Asp.Net Gridview C#中獲取動態創建的控件的值

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

我正在使用Asp.Net Gridview並在第三欄中動態添加文本框。 第三列有一個PlaceHolder,我正在動態地在該placeHolder中添加文本框。 這部分工作正常。 現在,如果我在該文本框中輸入了任何文本,那么我如何能夠獲得用戶輸入的值?

下面是我的代碼:

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

您可以通過`FindControl(“ ControlID”)訪問文本框,例如:

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

為了獲得它的價值,您將編寫:

   String txt_value = txtbox.Text;

在創建TextBox對象並通過FindControl()的幫助通過其ID查找它之后,可以訪問文本框的任何屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM