PostBack Asp.Net上的动态UserControls

时间:2011-09-12 14:11:38

标签: asp.net vb.net web-applications

我创建了一个项目user_control,它有一个文本框,按钮等,它会有意收集用户想要的项目总数。

我在page_load上动态创建了一些user_control的内容。如果单击项目数量的添加按钮,它将添加到会话变量。但是,当用户在文本框中输入不同的数量并单击添加按钮时,总数就是文本框的原始值。

如何获取用户输入到文本框中的值以添加到总计???

我在下面创建了一个精简示例:[UserControl] [Page] [CodeBehind]

...用户控件

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="BomItem.ascx.vb" Inherits="DynamicUserControl.BomItem" %>
<style type="text/css">
.panel
{
    width: 700px;
    padding: 5px;
}

.button
{
    margin-right: 10px;
}
.label
{
    float: left;
    margin-right: 10px;
}
</style>
<asp:Panel ID="Panel1" CssClass="panel" runat="server">
    <asp:Label ID="PartNumberLabel" CssClass="label" Width="100px" runat="server" Text="123456"></asp:Label>
    <asp:Label ID="DescriptionLabel" CssClass="label" Width="350" runat="server" Text="This is a sample description."></asp:Label>
    <asp:Button ID="AddButton" CssClass="button" runat="server" Text="Add" Width="100px" />
    <asp:TextBox ID="QuantityTextBox" runat="server" Width="100" Text="1" AutoPostBack="false"></asp:TextBox>
</asp:Panel>

页面...

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
    <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" Text="Get Total Quantity" />
    <br />
    <br />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />
    <br />
</div>
</form>
</body>
</html>

...的CodeBehind

Public Class _Default
Inherits System.Web.UI.Page

Protected Overrides Sub OnInit(e As System.EventArgs)
    MyBase.OnInit(e)
    'Dynamically load the user controls on the page
    For i = 0 To 2
        Dim BomItem1 As System.Web.UI.UserControl = LoadControl("BomItem.ascx")
        BomItem1.ID = "BomItem" & i

        PlaceHolder1.Controls.Add(BomItem1)
    Next
End Sub


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    ''***This questioned answered by James and Edited 
    ''to reflect the answer.
    ''Put the commented code below in the OnInit Method above.
    ''Dynamically load the user controls on the page
    'For i = 0 To 2
    '    Dim BomItem1 As System.Web.UI.UserControl = LoadControl("BomItem.ascx")
    '    BomItem1.ID = "BomItem" & i
    '              
    '    PlaceHolder1.Controls.Add(BomItem1)
    'Next

    'Collect the quantities that the user has entered
    If IsPostBack Then
        Dim ctrl As Control = GetPostBackControl(Me.Page)
        If ctrl IsNot Nothing Then
            If ctrl.ID.EndsWith("AddButton") Then
                CollectQuantity(ctrl)
            End If
        End If
    End If

End Sub

Public Function GetPostBackControl(page As Page) As Control
    Dim control As Control = Nothing

    Dim ctrlname As String = page.Request.Params.[Get]("__EVENTTARGET")
    If ctrlname IsNot Nothing AndAlso ctrlname <> String.Empty Then
        control = page.FindControl(ctrlname)
    Else
        For Each ctl As String In page.Request.Form
            Dim c As Control = page.FindControl(ctl)
            If TypeOf c Is System.Web.UI.WebControls.Button Then
                control = c
                Exit For
            End If

        Next
    End If
    Return control
End Function

Public Sub CollectQuantity(btn As Button)
    Dim uc As UserControl = btn.Parent.Parent
    Dim QuantityTextBox As TextBox = uc.FindControl("QuantityTextBox")
    Session("TotalQuantity") += CDbl(QuantityTextBox.Text)
End Sub

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TextBox1.Text = Session("TotalQuantity")
End Sub

End Class

1 个答案:

答案 0 :(得分:0)

查看示例代码,看起来您可能在页面生命周期中过晚地重新创建控件。尝试在OnInit期间重新创建控件,看看是否有帮助。