2011-12-28 51 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雷達上運行。