动态用户控件回发Asp.Net [英] Dynamic UserControls On PostBack Asp.Net
问题描述
我创建一个具有文本框,按钮项user_control等,这将有意收集用户希望。
项目的总数量我对动态创建的Page_Load的user_control几intances。如果您单击添加按钮的项目数量将添加到会话变量。然而,当用户在文本框中输入一个不同的数量和点击添加按钮总量的文本框的原始价值。
如何让用户在到文本框中输入了要添加到???总价值
我已经在下面创建一个精简的例子:用户控件] [首页] [codeBehind]
用户控件...
<%@控制语言=VBAutoEventWireup =false的codeBehind =BomItem.ascx.vb继承=DynamicUserControl.BomItem%GT;
<风格类型=文/ CSS>
。面板
{
宽度:700像素;
填充:5像素;
}.button
{
保证金权:10px的;
}
。标签
{
浮动:左;
保证金权:10px的;
}
< /风格>
< ASP:面板ID =面板1的CssClass =面板=服务器>
< ASP:标签ID =PartNumberLabel的CssClass =标签WIDTH =100px的=服务器文本=123456>< / ASP:标签>
< ASP:标签ID =DescriptionLabel的CssClass =标签WIDTH =350=服务器文本=这是一个简单的描述。>< / ASP:标签>
< ASP:按钮的ID =Add按钮的CssClass =按钮=服务器文本=添加WIDTH =100像素/>
< ASP:文本框ID =QuantityTextBox=服务器WIDTH =100文本=1的AutoPostBack =假>< / ASP:文本框>
< / ASP:面板>
页...
<%@页面语言=VBAutoEventWireup =false的codeBehind =Default.aspx.vb继承=DynamicUserControl._Default%GT;!< DOCTYPE HTML PUBLIC - // W3C // DTD XHTML 1.0过渡// ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
< HTML的xmlns =http://www.w3.org/1999/xhtml>
<头=服务器>
<标题>< /标题>
< /头>
<身体GT;
<表ID =form1的=服务器>
< DIV>
< ASP:占位符ID =PLACEHOLDER1=服务器>< / ASP:占位符>
< BR />
< BR />
< ASP:按钮的ID =Button1的=服务器文本=获取总量/>
< BR />
< BR />
< ASP:文本框ID =TextBox1的=服务器>< / ASP:文本框>
< BR />
< BR />
< / DIV>
< /表及GT;
< /身体GT;
< / HTML>
codeBehind ...
公共类_Default
继承System.Web.UI.Page受保护的覆盖子的OnInit(五作为System.EventArgs)
MyBase.OnInit(五)
动态加载页面上的用户控件
对于i = 0至2
昏暗BomItem1作为System.Web.UI.UserControl = LoadControl(BomItem.ascx)
BomItem1.ID =BomItem与&一世 PlaceHolder1.Controls.Add(BomItem1)
下一个
结束小组
保护小组的Page_Load(BYVAL发件人为对象,BYVAL E上System.EventArgs)把手Me.Load ''***此质疑詹姆斯和编辑回答
''反映了答案。
把下面的评论code在OnInit方法上面。
''动态加载页面上的用户控件
对于i = 0到2
昏暗BomItem1作为System.Web.UI.UserControl = LoadControl(BomItem.ascx)
'BomItem1.ID =BomItem与&一世
PlaceHolder1.Controls.Add(BomItem1)
'下一个 收集量用户已输入
如果随后的IsPostBack
昏暗的Ctrl键控制= GetPostBackControl(Me.Page)
如果CTRL状态并没有任何再
如果ctrl.ID.EndsWith(Add按钮),然后
CollectQuantity(CTRL)
万一
万一
万一结束小组公共职能GetPostBackControl(页面页)作为控制
昏暗控制,控制=无 昏暗ctrlname作为字符串= page.Request.Params。[获取](__ EVENTTARGET)
如果ctrlname状态并没有任何AndAlso运算ctrlname<>随后的String.Empty
控制= page.FindControl(ctrlname)
其他
对于每个CTL作为字符串在page.Request.Form
昏暗c以控制= page.FindControl(CTL)
如果TypeOf运算C是System.Web.UI.WebControls.Button然后
控制= C
对于出口
万一 下一个
万一
返回控制
结束功能公用Sub CollectQuantity(BTN为按钮)
昏暗的统一通信用户控件= btn.Parent.Parent
昏暗QuantityTextBox作为文本框= uc.FindControl(QuantityTextBox)
会议(TotalQuantity)+ = CDbl(QuantityTextBox.Text)
结束小组保护小组的button1_Click(发送者为对象,E作为EventArgs的)把手Button1.Click
TextBox1.Text =会议(TotalQuantity)
结束小组末级
看着你的榜样code,它看起来像你可能会重新创建控件的页面生命周期为时已晚。尝试在的OnInit
重现控制和看看是否有帮助。
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.
这篇关于动态用户控件回发Asp.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!