I created an item user_control that has a textbox, button, etc. which will intentionally collect the total quantity of items the user wants.
I dynamically create a few intances of user_control on page_load. If you click the add button for the item quantity it will add to a session variable. however when the user enters a different quantity in the textbox and clicks the add button the total is of the original value of the textbox.
How to get the value the the user has typed in to the textbox to add to total???
I've create a stripped down example below: [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...
<%@ 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
Looking at your example code, it looks like you might be recreating the controls too late in the page lifecycle. Try recreating the controls during OnInit
and see if that helps.
精彩评论