坚持动态控制,视图状态和回发。简单的例子 [英] Stuck on Dynamic Controls, Viewstate, and Postback. Simplified example

查看:150
本文介绍了坚持动态控制,视图状态和回发。简单的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读有关视图状态,和StateBag的,以及回传和页面创建的帖子和链接和文章。

I've been reading posts and links, and articles about viewstate, and statebag, and postback and page creation.

我是pretty体面的业余爱好者,但这个只是不下沉,可能有人告诉我怎么可能这样做的例子吗?

I'm a pretty decent amateur, but this one just isn't sinking in, could someone show me an example of how this might be done?

确定,这里就是我想用这个例子做

OK, here is what I am trying to do with this example

创建一个表,其中左边的列有LinkBut​​tons

Create a table where the left column has LinkButtons

在单击各个的LinkBut​​ton它打开了一堆Imagebuttons在右列

When each linkButton is clicked it opens a bunch of Imagebuttons in the right column

每个的ImageButton然后弹出一个消息

Each Imagebutton then pops up a message

问题是的ImageButton不会触发消息

The problem is the imagebutton does not trigger the message

这里是前端的:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="simple.aspx.vb" Inherits="simple" %>

<!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">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>simple</title>
</head>
<body>
<form id="form1" runat="server">

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

<asp:Table id="MainTable" runat="server">
<asp:TableRow>

<asp:TableCell ID ="cell1" BackColor="LightBlue" />

<asp:TableCell ID ="cell2" BackColor="LightBlue" />

</asp:TableRow>
</asp:Table>

</form>
</body>
</html>

和这里是code:

Partial Class simple
Inherits System.Web.UI.Page

Protected Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init

    Dim NewTableButton As LinkButton = New LinkButton
    NewTableButton.Text = "Text Created from 1st SQL Query"
    AddHandler NewTableButton.Click, AddressOf LoadContacts

    cell1.Controls.Add(NewTableButton)

End Sub

Protected Sub LoadContacts(sender As Object, e As System.EventArgs)

    Dim TextPassedfromLinkButton As String = sender.text.ToString

    Dim imgGear As New ImageButton
    imgGear.ImageUrl = "../graphics/gear.jpg"
    imgGear.AlternateText = "Text Created from 2nd SQL Query using the " _
 & TextPassedfromLinkButton & " as a query parameter"
    AddHandler imgGear.Click, AddressOf message

    cell2.Controls.Add(imgGear)

End Sub

Protected Sub message(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim strText As String = sender.text.ToString
    Dim alertString As String = "alert('" & strText & "');"
    ScriptManager.RegisterStartupScript(Me, [GetType](), "showalert", alertString, True)

End Sub

End Class

下面是查看源代码,你点击将会弹出消息的按钮后,

Here is the view source after you click the button that should popup the message

 <table id="MainTable">
 <tr>
 <td id="cell1" style="background-color:LightBlue;"><a href="javascript:__doPostBack(&#39;ctl03&#39;,&#39;&#39;)">Text Created from 1st SQL Query</a></td>
 <td id="cell2" style="background-color:LightBlue;"><input type="image" name="ctl04" src="../graphics/gear.jpg" alt="Text Created from 2nd SQL Query using the Text Created from 1st SQL Query as a query parameter" /></td>
 </tr>
 </table>

因此​​,在此期间我得到了这个工作(更新10:49 EST)

So in the meantime I got this to work (updated 10:49 est)

Protected Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init

    Dim NewTableButton As LinkButton = New LinkButton
    NewTableButton.Text = "Text Created from 1st SQL Query"
    'AddHandler NewTableButton.Click, AddressOf LoadContacts
    AddHandler NewTableButton.Click, Function() LoadContacts(NewTableButton.Text)

    cell1.Controls.Add(NewTableButton)

    If IsPostBack Then
        LoadContacts(NewTableButton.Text)
    End If

End Sub

Private Function LoadContacts(strText As String) As Integer

    Dim TextPassedfromLinkButton As String = strText

    Dim imgGear As New ImageButton
    imgGear.ID = "uniquename"
    EnableViewState = False
    imgGear.ImageUrl = "../graphics/gear.jpg"
    imgGear.AlternateText = "Text Created from 2nd SQL Query using the " & TextPassedfromLinkButton & " as a query parameter"
    AddHandler imgGear.Click, AddressOf message

    cell2.Controls.Add(imgGear)

    Return 0

End Function

唯一的问题是我得到2个相同imgGears

The only problem is I get 2 Identical imgGears


推荐答案

这里的问题是您另一个按钮的事件处理过程中实例化imgGear按钮的

The problem here is you are instantiating the imgGear button during the event handler of another button.

似乎完全逻辑这样做,但对于imgGear事件处理程序将永远不会被如果创建imgGear并触发页面的生命周期,例如添加到控制树太晚在事件处理程序或在preRender中。

It seems totally logical to do so, but the event handler for imgGear will never be triggered if imgGear is created and added to the control tree too late in the page lifecycle, e.g. in an event handler or during OnPreRender.

这是很难达到你正在尝试使用标准的.NET的做法去做......我从来没有管理它。因为它在页面生命周期中添加过晚你还可能会注意到一个事件处理过程中添加通常不会保留其在回传值的文本框中,再次。

It is very hard to achieve what you are trying to do using standard .Net practices...I have never managed it. You may also notice a text box added during an event handler doesn't usually retain its value on postback, again because it has been added too late in the page lifecycle.

您真的需要检测的OnInit或期间的CreateChildControls的背景下,因为一个按钮实例,并在该阶段添加到控制树的将会的有其处理程序触发。

You really need to detect the context during OnInit or CreateChildControls, because a button instantiated and added to the control tree during that phase will have its handler triggered.

我已经开发了一个变通,但...和最喜欢的变通它不是pretty ...但它可以让你出一个尴尬的境地:

I have developed a work around though...and like most work arounds it's not pretty...but it can get you out of a tight spot:

在OnInit中你可以告诉您的按钮引起回传,如果它是一个成员变量。 Intialise的NewTableButton在构造器......那么你就可以发现,如果它在OnInit的...

During OnInit you can tell if your button caused the postback, if it's a member variable. Intialise the NewTableButton in the contructor...then you can detect if it was clicked during OnInit...

   Private NewTableButton as Button

   Public Sub New()
        MyBase.New()
        NewTableButton = New LinkButton
        NewTableButton.Text = "Text Created from 1st SQL Query"
    End Sub

    Protected Overrides Sub OnInit(e As EventArgs)
        MyBase.OnInit(e)
        cell1.Controls.Add(NewTableButton)
        If Not IsNothing(Page.Request(NewTableButton.UniqueID)) Then
           ' Create and add imgGear Button
           LoadContacts
        End If
    End Sub

然而,文本框/下拉值使用的标准做法的OnInit期间不可用......所以,你可能有一个)推迟到在preRender了他们的阅读,或者b)使用页面。请求(Control.UniqueID)的OnInit中读取输入控件的值。

However, the text box / drop down values are not available during OnInit using standard practices...so, you may have to a) Defer the reading of them until OnPreRender, or b) Use the Page.Request(Control.UniqueID) to read the value of input controls during OnInit.

这是我与的.NET Web发展的最大问题之一,它会导致头痛和我不喜欢变通...

This is one of the biggest issues I have with .Net web development, it causes headaches and I don't like work arounds...

我很想知道,如果有这种解决方法的替代,但它是我发现来实现你想实现什么的唯一方式。

I love to know if there was an alternative to this workaround, but It's the only way I've found to achieve what you're trying to achieve.

这篇关于坚持动态控制,视图状态和回发。简单的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆