从下拉列表事件处理程序加载动态控件,如何在按钮事件后保留控件 [英] load dynamic control from a dropdownlist event handler, how to preserve the control after button event
问题描述
我知道我需要在每次回发时加载我的动态控件,最好是在 Page_Init() 中.但是我的动态控件是从下拉列表 selectedindexchanged 事件处理程序中加载的.现在在任何回发之后,我的动态控件都消失了.我将如何强制它在每次回发时加载?
I understand I need to load my dynmaic control on every postback and preferrably in Page_Init(). But my dyanmic controls are loaded from a dropdownlist selectedindexchanged event handler. Now after any postback, my dynamic controls are gone. How would I force it to load on every postback ?
非常感谢!
推荐答案
您必须将创建的控件数量存储在某处.为此,我推荐 ViewState.然后,您必须使用与以前相同的 id 在每个回发上重新创建动态创建的控件.因为 ViewState
在 Page_Load
中可用,所以我会使用这个事件.在 DropdownList 的 SelectedIndexChanged
-Eventhandler 中,添加控件并相应地增加 ViewState
-Variable.
You have to store the number of created controls somewhere. I recommend ViewState for this purpose. Then you have to recreate the dynamically created controls on every postback with the same id as before. Because ViewState
is available in Page_Load
i would use this event. In your DropdownList's SelectedIndexChanged
-Eventhandler you add the controls and increase the ViewState
-Variable accordingly.
这是一个例子:
aspx:
<asp:placeholder ID="Container" runat="server"></asp:placeholder><br/>
<asp:Label ID="LblInfo" runat="server"></asp:Label><br />
<asp:DropDownList ID="DdlAddControls" OnSelectedIndexChanged="DdlAddControls_SelectedIndexChanged" AutoPostBack="true" runat="server">
<asp:ListItem Text="" Value="0"></asp:ListItem>
<asp:ListItem Text="add 1 button" Value="1"></asp:ListItem>
<asp:ListItem Text="add 2 buttons" Value="2"></asp:ListItem>
<asp:ListItem Text="add 3 buttons" Value="3"></asp:ListItem>
</asp:DropDownList>
VB.Net
Private Property ControlCount As Int32
Get
If ViewState("ControlCount") Is Nothing Then
ViewState("ControlCount") = 0
End If
Return DirectCast(ViewState("ControlCount"), Int32)
End Get
Set(ByVal value As Int32)
ViewState("ControlCount") = value
End Set
End Property
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If ControlCount <> 0 Then
RecreateControls()
End If
End Sub
Private Sub RecreateControls()
addControls(ControlCount)
End Sub
Private Sub addControls(ByVal count As Int32)
For i As Int32 = 1 To count
Dim btn As New Button() ' i use a Button as example'
btn.ID = "Button_" & Me.Container.Controls.Count
btn.Text = "Button " & Me.Container.Controls.Count
AddHandler btn.Click, AddressOf Click
Me.Container.Controls.Add(btn)
Next
End Sub
Private Sub Click(ByVal sender As Object, ByVal e As EventArgs)
LblInfo.Text = DirectCast(sender, Button).Id & "clicked"
End Sub
Protected Sub DdlAddControls_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DdlAddControls.SelectedIndexChanged
Dim newControlCount As Int32 = Int32.Parse(DdlAddControls.SelectedValue)
addControls(newControlCount)
ControlCount += newControlCount
End Sub
C#
private Int32 ControlCount {
get {
if (ViewState("ControlCount") == null) {
ViewState("ControlCount") = 0;
}
return (Int32)ViewState("ControlCount");
}
set { ViewState("ControlCount") = value; }
}
private void Page_Load(object sender, System.EventArgs e)
{
if (ControlCount != 0) {
RecreateControls();
}
}
private void RecreateControls()
{
addControls(ControlCount);
}
private void addControls(Int32 count)
{
for (Int32 i = 1; i <= count; i++) {
Button btn = new Button();
// i use a Button as example'
btn.ID = "Button_" + this.Container.Controls.Count;
btn.Text = "Button " + this.Container.Controls.Count;
btn.Click += Click;
this.Container.Controls.Add(btn);
}
}
private void Click(object sender, EventArgs e)
{
LblInfo.Text = ((Button)sender).Id + "clicked";
}
Protected void DdlAddControls_SelectedIndexChanged(object sender, System.EventArgs e)
{
Int32 newControlCount = Int32.Parse(DdlAddControls.SelectedValue);
addControls(newControlCount);
ControlCount += newControlCount;
}
这篇关于从下拉列表事件处理程序加载动态控件,如何在按钮事件后保留控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!