我正在尝试动态创建一个页面,但是我第一次进入了这个页面。我在阅读后可以创建以下博客/帖子/文章。我遇到的问题是,当我点击第一个按钮时,我没有发生任何事情,如果我再点击它就可以了。
任何人都可以告诉我哪里出错了吗?
由于
ASP页面:
<%@ Page Title="Create / Amend Quotes" Language="vb" AutoEventWireup="false" CodeBehind="Quote.aspx.vb" Inherits="BIS.Quote" %>
<!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 id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
</form>
</body>
</html>
VB
Imports System.Data
Imports System.Data.SqlClient
Public Class Quote
Inherits System.Web.UI.Page
Const CREATE_SELECTION As String = "create"
Const AMEND_SELECTION As String = "amend"
Const VIEWSTATEKEY_DYNCONTROL As String = "VS_Controls"
Protected Property VS_Controls() As String
Get
Dim result As String = ViewState.Item(VIEWSTATEKEY_DYNCONTROL)
If result Is Nothing Then
Return String.Empty
Else
Return result
End If
End Get
Set(ByVal value As String)
ViewState.Item(VIEWSTATEKEY_DYNCONTROL) = value
End Set
End Property
Private Sub Quote_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
PlaceHolder1.Controls.Clear()
End Sub
Private Sub Quote_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Select Case Me.VS_Controls
Case CREATE_SELECTION
Dim Lab As New Label
Lab.ID = "Tester"
Lab.Text = "Create"
Me.PlaceHolder1.Controls.Add(Lab)
Case AMEND_SELECTION
Dim Lab As New Label
Lab.ID = "Tester_2"
Lab.Text = "Amend"
Me.PlaceHolder1.Controls.Add(Lab)
Case Else
Dim btn_create As New Button
Dim btn_Amend As New Button
Dim Label1 As New Label
btn_create.ID = "but_Create"
btn_create.Text = "Create"
btn_create.CommandName = "Create"
AddHandler btn_create.Click, AddressOf onClick
btn_Amend.ID = "but_Amend"
btn_Amend.Text = "Amend"
btn_Amend.CommandName = "Amend"
AddHandler btn_Amend.Click, AddressOf onClick
Label1.id = "lab1"
Label1.Text = VS_Controls
Me.PlaceHolder1.Controls.Add(btn_create)
Me.PlaceHolder1.Controls.Add(New LiteralControl(" ")) 'space them out
Me.PlaceHolder1.Controls.Add(btn_Amend)
Me.PlaceHolder1.Controls.Add(New LiteralControl(" ")) 'space them out
Me.PlaceHolder1.Controls.Add(Label1)
End Select
End Sub
Private Sub onClick(ByVal sender As Object, ByVal e As EventArgs)
Dim From As Button = DirectCast(sender, Button)
Dim But_String As String = From.ID
If But_String = "but_Create" Then
Me.VS_Controls = CREATE_SELECTION
' Me.VS_Controls = "create"
' ViewState("create_amend") = "create"
ElseIf But_String = "but_Amend" Then
Me.VS_Controls = AMEND_SELECTION
'ViewState("create_amend") = "amend"
End If
End Sub
Private Sub Quote_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
End Sub
End Class
答案 0 :(得分:0)
在ASP.NET页面生命周期(http://msdn.microsoft.com/en-us/library/ms178472.aspx)中,Page_Load
在事件处理之前发生。因此,在创建标签之前,您的视图状态不会被修改。你应该用你的逻辑来创建Page_PreRender
中的标签(或者可以从点击事件中调用的单独方法,或者如果Page_Load
为假则从IsPostback
调用。)
答案 1 :(得分:0)
你需要检查页面是否处于回发模式.....
if(!Page.IsPostBack){}
答案 2 :(得分:0)
创建按钮并在OnInit
期间分配事件处理程序。在您点击Page_Load
时已经加载了ViewState。