2014-04-08 72 views
0

在PreInit事件期間,我已經向我的內容頁面添加了一些動態控件,但Viewstate在回發後未被自動維護,儘管聲明瞭所聲明的內容有關在PreInit事件期間添加控件的信息。我的下拉列表和文本框重置。我究竟做錯了什麼?preinit事件期間添加到我的內容頁面的動態控件不保持視圖狀態

標記

<%@ Page Title="" Language="VB" MasterPageFile="~/ContentPages/MasterPage.master" AutoEventWireup="false" CodeFile="ElktonOEE.aspx.vb" Inherits="ContentPages_OEE_ElktonOEE" %> 
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder_Head" Runat="Server"> 
<link rel="stylesheet" href="http://flexweb/MES/css/UserControls/FlexGridView.css" type="text/css"/> 
<link rel="stylesheet" href="http://flexweb/MES/css/LabelTexbox.css" type="text/css"/> 
<link rel="stylesheet" href="http://flexweb/MES/css/OEE.css" type="text/css"/> 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder_PageContentTitle" Runat="Server"> 
<div class="PageContentTitle">OEE Report</div> 
</asp:Content> 

<asp:Content ID="Content4" ContentPlaceHolderID="ContentPlaceHolder_Content" Runat="Server"> 
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"> 
</asp:ToolkitScriptManager> 
<script type="text/javascript"> 
</script> 

<div class="MESContentDiv" id="SqlQueryContentDiv"> 
<div id="ContentLabelDiv"> 
<asp:Label ID="PanelLabel" runat="server" CssClass="ContentLabel"></asp:Label> 
</div> 
<br /> 
<asp:Panel ID="Panel1" runat="server"> 
<!-- Controls dynamically generated in the code-behind and inserted here --> 
</asp:Panel> 
<br /> 
<br /> 
<asp:Panel ID="Panel2" runat="server"> 
<asp:Table ID="Table1" runat="server"> 
</asp:Table> 
</asp:Panel> 
</div> 

</asp:Content> 

代碼隱藏

Imports ASP      'Allows User Control to be dynamically loaded onto page 
Imports System.Data    'Allows namespace access to the DataSet class 
Imports System.Web.UI.UserControl 
Imports MES_Class_Library 

Partial Class ContentPages_OEE_ElktonOEE 
Inherits System.Web.UI.Page 

Protected Sub Page_PreInit(sender As Object, e As System.EventArgs) Handles Me.PreInit 
'Call this to avoid null result when searching for controls on a content page 
CommonFlexwebFunctions.PrepareChildControlsDuringPreInit(Page) 

'Add Dynamic Controls, Dyanimc Controls Must be given ID in order for the viewstate to work. 
LoadSearchPrompt() 
End Sub 

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load 
Label_Debug.Visible = False 

'Prompt for parameters 
PanelLabel.Text = "Select OEE Parameters" 

'Check for empty Query String 
If (Request.QueryString("Type") IsNot Nothing) And (Request.QueryString("Date") IsNot Nothing) Then 
Select Case Request.QueryString("Type") 
Case "Daily" 
LoadDailySummary() 
Case "Weekly" 
'LoadWeeklySummary() 
Case "Monthly" 
'LoadMonthlySummary() 
End Select 
Else 
Panel2.Visible = False 
End If 

End Sub 

Private Sub LoadSearchPrompt() 

Dim lb1, lb2 As New Label 
Dim ddl As New DropDownList 
Dim tb As New TextBox 
Dim ce As New AjaxControlToolkit.CalendarExtender 
Dim validation_groupname As String = "ValidDate" 

'Report Label 
lb1.CssClass = "LabelName125 Twelve" 
lb1.ID = "lbType" 
lb1.Text = "Report Type:" 

'Report DDL 
ddl.CssClass = "ML5" 
ddl.ID = "ddlReportType" 
ddl.Items.Add("--") 
ddl.Items.Add("Daily") 
ddl.Items.Add("Weekly") 
ddl.Items.Add("Monthly") 

'Start Date Label 
lb2.CssClass = "LabelName125 Twelve" 
lb2.ID = "lbDate" 
lb2.Text = "Start Date:" 

'Start Date Textbox 
tb.CssClass = "TextboxValue125 ML5" 
tb.ID = "textboxStartDate" 
tb.ValidationGroup = validation_groupname 

'Calendar Extender 
ce.ID = "ceDate" 
ce.TargetControlID = "textboxStartDate" 

'Valiation 
Dim cv As New CompareValidator 
Dim vs As New ValidationSummary 

cv.ControlToValidate = "textboxStartDate" 
cv.ID = "cv1" 
cv.Display = ValidatorDisplay.None 
cv.ErrorMessage = "Date must be in the mm/dd/yyyy format." 
cv.Operator = ValidationCompareOperator.DataTypeCheck 
cv.Type = ValidationDataType.Date 
cv.ValidationGroup = validation_groupname 

vs.ID = "vs1" 
vs.HeaderText = "The data you entered contains an error." 
vs.ShowMessageBox = True 
vs.ShowSummary = False 
vs.ValidationGroup = validation_groupname 

'Submit 
Dim btn As New Button 
btn.CssClass = "Button100 LeftMargin25" 
btn.ID = "btnSubmit" 
btn.CausesValidation = True 
btn.ValidationGroup = validation_groupname 
btn.Text = "Submit" 

'add handler 
AddHandler btn.Click, AddressOf MyBtnClick '' this is the method to call 

'Add Controls 
Panel1.Controls.Add(lb1) 
Panel1.Controls.Add(ddl) 
Panel1.Controls.Add(lb2) 
Panel1.Controls.Add(tb) 
Panel1.Controls.Add(ce) 
Panel1.Controls.Add(cv) 
Panel1.Controls.Add(vs) 
Panel1.Controls.Add(btn) 

End Sub 

Private Sub MyBtnClick(ByVal sender As Object, ByVal e As EventArgs) 
Dim btn As Button = CType(sender, Button) ''Gets the button that fired the method 
Dim ReportType As String = "" 
Dim StartDate As String = "" 

'Access the ddl 
Dim ddl As DropDownList = CType(CommonFlexwebFunctions.RecursiveFindControl(Page, "ddlReportType"), DropDownList) 
'If the proper ddl was found, set its value 
If ddl IsNot Nothing Then 
'Set the value 
ReportType = ddl.SelectedItem.ToString() 
End If 

'Access the tb 
Dim tb As TextBox = CType(CommonFlexwebFunctions.RecursiveFindControl(Page, "textboxStartDate"), TextBox) 
'If the proper ddl was found, set its value 
If tb IsNot Nothing Then 
'Set the value 
StartDate = tb.Text 
End If 

Response.Redirect("ElktonOEE.aspx?Type=" + ReportType + "&Date=" + StartDate) 
End Sub 

Private Sub LoadDailySummary() 
Panel2.Visible = True 
End Sub 


End Class 

類功能

Public Class CommonFlexwebFunctions 
Public Shared Function RecursiveFindControl(container As Control, name As String) As Control 
If Not (container.ID Is Nothing) AndAlso (container.ID.Equals(name)) Then 
Return container 
End If 

For Each c As Control In container.Controls 
Dim ctrl As Control = RecursiveFindControl(c, name) 
If Not ctrl Is Nothing Then 
Return ctrl 
End If 
Next 
Return Nothing 
End Function 
Public Shared Sub PrepareChildControlsDuringPreInit(page As Page) 
' Walk up the master page chain and tickle the getter on each one 
' Run this so you can see the controls on content pages 
Dim master As MasterPage = page.Master 
While master IsNot Nothing 
master = master.Master 
End While 
End Sub 
End Class 

回答

相關問題