2011-12-28 52 views
0

(动态控件),但无法找到控件在页面Pre_Init我可以使用下面的代码获取控件的名称,但它无法在面板中找到控件,即使它存在。那为什么呢?我需要做的是在回收控制处理之前获取它的价值。虽然存在Asp.Net

注意:这只是一个示例。

下面是HTML

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm2.aspx.vb" Inherits="DynamicControls_GetControlUnloaded.WebForm2" %> 

<!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"> 
<asp:ScriptManager ID="ScriptManager1" runat="server"> 
</asp:ScriptManager> 
<div style="width: 200px;"> 
    <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
     <ContentTemplate> 
      <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder> 
     </ContentTemplate> 
    </asp:UpdatePanel> 
</div> 
</form> 
</body> 
</html> 

这里是后面的代码

Public Class WebForm2 
Inherits System.Web.UI.Page 
Dim current_val As Object 

Private Sub WebForm2_PreInit(sender As Object, e As System.EventArgs) Handles Me.PreInit 
    Dim postback_control As Control = GetPostBackControl(Me.Page) 

    If postback_control IsNot Nothing Then 
     Select Case postback_control.GetType 
      Case GetType(DropDownList) 
       current_val = CType(postback_control, DropDownList).Text 
      Case GetType(TextBox) 
       current_val = CType(postback_control, TextBox).Text 
      Case GetType(CheckBox) 
       current_val = CType(postback_control, CheckBox).Checked 
      Case GetType(RadioButton) 
       current_val = CType(postback_control, RadioButton).Checked 
     End Select 
    End If 

End Sub 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    'Create Dynamic controls 
    Call BuildControls() 
End Sub 

Private Sub BuildControls() 
    For i As Integer = 0 To 2 
     Dim ddl As New DropDownList 
     ddl.Items.Add("Item 1") 
     ddl.Items.Add("Item 2") 
     ddl.Items.Add("Item 3") 
     ddl.Style.Add("margin", "3px") 
     ddl.ID = "Ctrl" & i.ToString 
     ddl.AutoPostBack = True 
     ddl.Width = 150 
     PlaceHolder1.Controls.Add(ddl) 
    Next 
End Sub 

Public Shared Function GetPostBackControl(ByVal thePage As Page) As Control 
    Dim myControl As Control = Nothing 
    Dim ctrlName As String = thePage.Request.Params.Get("__EVENTTARGET") 
    If ((ctrlName IsNot Nothing) And (ctrlName <> String.Empty)) Then 
     myControl = thePage.FindControl(ctrlName) 
    Else 
     For Each Item As String In thePage.Request.Form 
      Dim c As Control = thePage.FindControl(Item) 
      If (TypeOf (c) Is System.Web.UI.WebControls.Button) Then 
       myControl = c 
      End If 
     Next 
    End If 
    Return myControl 
End Function 

End Class 
+0

你为什么要这样做而不是正常的方式呢? – ivowiblo

+0

@ivowiblo这个项目是动态的。我试图在重新创建之前获取控件的值(dropdownlist)。 – TroyS

+0

哦,我看到你改变了整个例子,让我现在检查:) – ivowiblo

回答

0

您可能是在与Pre_Init的处理步骤月初。你的代码真的必须在那里运行吗?请参阅MSDN中有关生命周期事件的this article

+0

它是一个动态控件,我需要从它处理文本。 – TroyS

+0

我甚至把它放在Page_Load事件中,它获取控件名称,但在面板中找不到它。 – TroyS

+0

你有没有试过把它放在PreRender中?这会在Page_Load – nycdan

-1

FlowLayoutPanel2在Page Pre_init上不可用,因为它只在客户端可用。你为什么不使用<asp:Dropdownlist>

如果你必须使用一个选择选项,您可以通过添加runat="server"访问<select和/或将其添加到<div id="FlowLayoutPanel2"从服务器端访问他们两个。

<div id="FlowLayoutPanel2" style="width:300px;padding-bottom:10px;padding-left:10px;padding-right:10px;" runat="server"> 
     <span>Select type of item:</span> 
     <select name="ctrl1" runat="server" ...> 
      <option selected="selected" value=""></option> 
      <option value="Item 1">Item 1</option> 
      <option value="Item 2">Item 2</option> 
      <option value="Item 3">Item 3</option> 
      <option value="Item 4">Item 4</option> 
     </select> 
    </div> 
+0

我正在使用下拉列表。我发布的html是在浏览器中调试的源代码。 – TroyS

+0

我试图在加载事件中重新创建之前获取动态控件的值。不能完全弄清楚如何做到这一点。 – TroyS

+0

你可以在源文件中发布你的HTML吗? – Robert

0

初始事件是你最好的选择。 Pre-Init用于主页面,我猜。视图状态在Init事件之前加载。

0

我的建议是在Init中创建控件,而不是在Load中创建控件,并创建所有控件。这将使这些对象的所有事件都被触发(因为在分析ViewState之前您正在创建它们)。然后,您想要的值将从控件本身提供。如果您需要根据值显示不同的值,请创建所有对象,然后隐藏不想显示的对象(.Visible = false)。除了它们不会被渲染外,它们将在ASP.NET雷达上运行。