.NET中的asp.ImageButton OnClick函数在updatePanel中

时间:2011-11-21 23:27:15

标签: asp.net vb.net gridview onclick updatepanel

我有一个GridView,它使用计时器不断反弹,并且在updatePanel内,因此页面不会持续刷新(每行都有一个倒计时序列,因此gridview需要不断更新)

在gridview中,我有一个带有OnClick方法的图像按钮。以前要启动OnClick方法,我会确保gridView不在UpdatePanel中,并且gridview的pageload数据绑定位于“If Not IsPostBack”中。

使用Timer虽然我不能将gridview绑定到“If Not IsPostBack”并且它需要在UpdatePanel中。

有没有办法使用UpdatePanel和“If Not IsPostBack”仍然可以调用OnClick方法?

由于

以下是一些代码,如果我的解释没有完全明白:

的UpdatePanel /定时/ GridView的

<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Timer ID="timerCountDown" runat="server" Interval="1000" OnTick="timerCountDown_Tick"></asp:Timer>
<asp:GridView ID="gridBuildQueue" runat="server" AutoGenerateColumns="False"
                    GridLines="none" ShowFooter="false" ShowHeader="false" Width="100%">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:ImageButton runat="server" ID="cmdCancelBuild" ImageUrl="~/images/cancel.jpg" OnClick="ImageButton_Click"/>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
        </asp:GridView>
        </ContentTemplate>
</asp:UpdatePanel>

OnTick计时器方法:

Protected Sub timerCountdown_Tick(ByVal sender As Object, ByVal e As EventArgs)
    Me.gridBuildQueue.DataBind()
End Sub

ImageButton_Click方法(当前从未调用过):

Protected Sub ImageButton_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
    Dim imageButton As ImageButton = CType(sender, ImageButton)
    Dim row As GridViewRow = CType(imageButton.NamingContainer, GridViewRow)
    Dim button As ImageButton = DirectCast(row.FindControl("cmdCancelBuild"), ImageButton)
etc...
End Sub

1 个答案:

答案 0 :(得分:3)

您需要使用GridView控制事件,特别是 - RowCommand事件。

<asp:UpdatePanel runat="server">
   <ContentTemplate>
    <asp:GridView 
             ID="gridBuildQueue" 
             runat="server" 
             AutoGenerateColumns="False"
             GridLines="none" 
             ShowFooter="false" 
             ShowHeader="false" 
             Width="100%"
             onrowcommand="gridBuildQueue_RowCommand"
             >
               <Columns>
                <asp:TemplateField>
                 <ItemTemplate>
                   <asp:ImageButton 
                             runat="server" 
                             ID="cmdCancelBuild" 
                             ImageUrl="~/images/cancel.jpg" 
                             CommandName="cmd"/>
                  </ItemTemplate>
                 </asp:TemplateField>
               </Columns>
    </asp:GridView>
   </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="gridBuildQueue" EventName="RowCommand" />
        </Triggers>
    </asp:UpdatePanel>

代码背后:

protected Sub gridBuildQueue_RowCommand(ByVal sender As Object,ByVal e as GridViewCommandEventArgs)
  if e.CommandName="cmd"  Then

   ....
  End If
End sub

除了RowCommand事件之外,您还必须为RowCommand事件添加AsyncPostBackTrigger条目。 (设置UpdatePanel.Triggers集合)。

样本:

标记

<div>
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:ImageButton ID="ImageButton1" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:Timer ID="Timer1" runat="server" Interval="1000">
        </asp:Timer>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="RowCommand" />
        <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
    </Triggers>
</asp:UpdatePanel>

代码隐藏

Dim lst As New List(Of String)
    Protected Sub GridView1_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        Label1.Text = DateTime.Now
    End Sub

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If IsNothing(Session("lst")) Then
                Session("lst") = lst
            End If
            GridView1.DataSource = lst
            GridView1.DataBind()
        End If
    End Sub

    Protected Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
        lst = Session("lst")
        lst.Add(DateTime.Now.ToString())
        GridView1.DataSource = lst
        GridView1.DataBind()
    End Sub