0
我創建了一個項目user_control,它有一個文本框,按鈕等,它將故意收集用戶想要的項目總數。回發的動態用戶控件Asp.Net
我動態地在page_load上創建了一些user_control實例。如果您單擊項目數量的添加按鈕,它將添加到會話變量。但是,當用戶在文本框中輸入不同的數量並單擊添加按鈕時,總數就是文本框的原始值。
如何獲取用戶輸入到文本框中的值以添加到總共?
我有下面創建一個精簡例如:用戶控件] [首頁] [代碼隱藏]
用戶控件...
<%@ 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>
代碼隱藏...
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
您可以嘗試在OnPreInit或OnInit事件而不是Page_Load中重新創建控件。我認爲控件在ViewState加載後正在重新創建。 –
@詹姆斯感謝億。是的,工作很棒!我將編輯代碼供其他人查看。我使用了OnInit。如果您可以創建答案,我想標記爲已回答。 – TroyS
很高興你能工作。添加了一個答案。 –