The method behind C to create the control and get the value

  • 2020-12-26 05:51:42
  • OfStack

This example shows how C# creates controls behind the scenes and gets values. Share to everybody for everybody reference. Specific implementation methods are as follows:

Foreground code:

<form id="form1" runat="server">
    <div>
        <div class="item">
            Please input a number:
            <asp:TextBox runat="server" CssClass="item" ID="txtTextCount"></asp:TextBox>
          
            <asp:Button runat="server" ID="btnCreate" Text="Create TextBox List" ValidationGroup="CreateTextBox"
                OnClick="btnCreate_Click" />&nbsp;&nbsp;
            <asp:Button runat="server" ID="btnOK" Text=" Get control values " ValidationGroup="ShowListContent"
                OnClick="btnOK_Click" />
        </div>
        <div runat="server" id="divControls" class="item">
        </div>
        <div runat="server" id="divMessage">
        </div>
    </div>
</form>

Background code:

protected void Page_Load(object sender, EventArgs e)
    {
        if (this.IsPostBack)
        {
            int txtCount = int.Parse(txtTextCount.Text);             // Note: Every time PostBack , both need to be recreated dynamically TextBox
            CreateTextBoxList(txtCount);
        }
    }
    ///<summary>      /// Create textbox list
    ///</summary>
    ///<param name="num">textbox list count</param>
    private void CreateTextBoxList(int num)
    {
        HtmlGenericControl div;
        HtmlGenericControl span;
        TextBox txt;
        //RegularExpressionValidator rev;         for (int i = 0; i < num; i++)
        {
            // create div
            div = new HtmlGenericControl();
            div.TagName = "div";
            div.ID = "divTextBox" + i.ToString();
            div.Attributes["class"] = "item2";             // create span
            span = new HtmlGenericControl();
            span.ID = "spanTextBox" + i.ToString();
            span.InnerHtml = "Url Address" + (i + 1).ToString() + ":";             // create TextBox
            txt = new TextBox();
            txt.ID = "txt" + i.ToString();
            txt.CssClass = "input";             // Create a format validation control and associate it with the corresponding TextBox
            //rev = new RegularExpressionValidator();
            //rev.ID = "rev" + i.ToString();
            //rev.ControlToValidate = txt.ID;
            //rev.Display = ValidatorDisplay.Dynamic;
            //rev.ValidationGroup = "ShowListContent";
            //rev.ValidationExpression = @"(http(s)?://)?([\w-]+\.)+[\w-]+(/[\w- ./?%&amp;=]*)?";
            //rev.ErrorMessage = "Invalid url Address!";             // Add controls to the container
            div.Controls.Add(span);
            div.Controls.Add(txt);
            //div.Controls.Add(rev);
            divControls.Controls.Add(div);
        }
    }     protected void btnCreate_Click(object sender, EventArgs e)
    {
        txtTextCount.Enabled = false;
        btnCreate.Enabled = false;
    }     protected void btnOK_Click(object sender, EventArgs e)
    {
        TextBox txt;
        HtmlGenericControl span;
        StringBuilder sbResult = new StringBuilder();
        int txtCount = int.Parse(txtTextCount.Text);         // Traversal gets dynamically created TextBox In the Text value
        for (int i = 0; i < txtCount; i++)
        {
            // Note: You must get the dynamically created ones here through the upper container TextBox , in order to get ViewState content
            txt = divControls.FindControl("txt" + i.ToString()) as TextBox;             if (txt != null && txt.Text.Trim().Length > 0)
            {
                sbResult.AppendFormat("Url Address{0}: {1}.<br />", i + 1, txt.Text.Trim());
            }
        }
        // Traversal gets dynamically created TextBox In the Text value
        for (int i = 0; i < txtCount; i++)
        {
            // Note: You must get the dynamically created ones here through the upper container TextBox , in order to get ViewState content
            span = divControls.FindControl("spanTextBox" + i.ToString()) as  HtmlGenericControl ;             if (span != null && span.InnerText.Trim().Length > 0)
            {
                sbResult.AppendFormat("Url Address{0}: {1}.<br />", i + 1, span.InnerText.Trim());
            }
        }
        divMessage.InnerHtml = sbResult.ToString();
}

Hopefully this article has helped you with your C# programming.


Related articles: