简体   繁体   中英

ASP.net (VB.Net) Dynamic controls on Pageload and reading their values on button click

I am creating a page with Dynamic controls(Setting ID of controls too) in a table based on a loop in Page Load event.

I need to access the values of those dynamic controls to save in database in button click event. Now, when I loop on the table, it is giving me 0 rows of the table. Secondly, I also try to use the Find_Control Property to find the controls on the page and it is giving me NOTHING for each Control.

Is there something, I am missing, like runat=server? Or there is another approach to access those control on button click event.

ASP.net Code is below

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Table ID="Table1" runat="server">
                </asp:Table>
            </div>
            <asp:Button ID="Button1" runat="server" Text="Button" />
        </form>
    </body>
</html>

VB.net is Below (just looping 10 times, Odd rows with text boxes, even rows with Drop down list just as an example)

Option Strict On
Option Explicit On

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If IsPostBack = False Then
            Dim li As Integer = 0, lj As Integer = 0
            For li = 0 To 9
                Dim loR As New TableRow
                '-------------------------------
                Dim loC1 As New TableCell
                loC1.Text = "Caption " & CStr(li + 1).PadLeft(2, CChar("0")) & " :"
                loR.Cells.Add(loC1)
                '-------------------------------
                Dim loC2 As New TableCell
                Select Case (li + 1) Mod 2
                    Case 0
                        Dim loDD As New DropDownList
                        loDD.ID = "ctl" & CStr(li + 1).PadLeft(2, CChar("0"))
                        For lj = 0 To li + 3
                            loDD.Items.Add("List " & CStr(li + 1).PadLeft(2, CChar("0")) & " : Value " & CStr(lj + 1).PadLeft(2, CChar("0")))
                        Next
                        loC2.Controls.Add(loDD)
                    Case 1
                        Dim loT As New TextBox
                        loT.ID = "ctl" & CStr(li + 1).PadLeft(2, CChar("0"))
                        loC2.Controls.Add(loT)
                End Select
                loR.Cells.Add(loC2)
                '-------------------------------
                Table1.Rows.Add(loR)
            Next
        End If
    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim li As Integer = 0
        For li = 0 To Table1.Rows.Count - 1
            Response.Write("")
        Next
    End Sub

End Class

Hi for storing and retrieving values from the dynamically created control we need to follow 3 steps,

1) create dynamic controls as per our need 2) override viewstate's two methods that is LoadViewState and SaveViewState as per below my code snippet

protected void CreateDynamicTextBox()
{
    Label lableVendorName = new Label();
    lableVendorName.Text = "Vendor Name";

    TextBox VendorName = new TextBox();
    VendorName.ID = "VendorName";

    PanelVendor.Controls.Add(lableVendorName);
    PanelVendor.Controls.Add(VendorName);
} 


protected override void LoadViewState(object savedState)
        {
            //if we can identify the custom view state as defined in the override for SaveViewState
            if (savedState is object[] && ((object[])savedState).Length == 2 && ((object[])savedState)[0] is string[])
            {
                object[] newViewState = (object[])savedState;
                string[] txtValues = (string[])(newViewState[0]);
                if (txtValues.Length > 0)
                {
                    CreateDynamicTextBox();
                    (PanelVendor.FindControl("VendorName") as TextBox).Text = txtValues[0];
                }
                //load the ViewState normally
                base.LoadViewState(newViewState[1]);
            }
            else
            {
                base.LoadViewState(savedState);
            }
        }

protected override object SaveViewState()
{
    object[] newViewState = new object[2];
    List<string> txtValues = new List<string>();
    if (PanelVendor.Controls.Count > 1)
    {
        txtValues.Add((PanelVendor.FindControl("VendorName") as TextBox).Text);
    }

    newViewState[0] = txtValues.ToArray();
    newViewState[1] = base.SaveViewState();
    return newViewState;
}

and now retrieve the value from that control as below

protected void btnItem_Click(object sender, EventArgs e)
        {
string VendorName = (PanelVendor.FindControl("VendorName") as TextBox).Text;
}

hope this would help. happy coding :)

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