Я создал элемент user_control с текстовым полем, кнопкой и т. Д., Который намеренно собирает общее количество элементов, которое хочет пользователь.
Я динамически создаю несколько экземпляров user_control на page_load. Если вы нажмете кнопку добавления для количества товара, оно будет добавлено в переменную сеанса. однако, когда пользователь вводит другое количество в текстовое поле и нажимает кнопку добавления, итоговое значение равно исходному значению текстового поля.
Как получить значение, которое пользователь ввел в текстовое поле, чтобы добавить к итоговому значению ???
Я создал урезанный пример ниже: [UserControl] [Page] [CodeBehind]
UserControl ...
<%@ 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