ASP.NET - 将UpdatePanel触发器添加到gridview内的LinkBut​​ton

时间:2011-06-08 09:11:36

标签: asp.net gridview updatepanel linkbutton

我试图更新模态对话框的内容,这段代码对我有用:

<asp:LinkButton ID="updateSomething" runat="server" Text="Update" CausesValidation="false" OnClientClick="openDialog();" onclick="UpdateButton_Click" />

<asp:UpdatePanel ID="upNewUpdatePanel" runat="server">
    <ContentTemplate>
        <asp:Label ID="updateLabel" runat="server"></asp:Label>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="updateSomething" EventName="Click" />
    </Triggers>
</asp:UpdatePanel>

但是,当我尝试将LinkBut​​ton放在gridview中时,如下所示:

<asp:GridView ID="grdListUsers" runat="server" AutoGenerateColumns="false" AllowPaging="false" OnRowDataBound="grdRowDefListUsers" CssClass="mGrid" EmptyDataText="No users.">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Nome" HeaderStyle-Width="300" />
        <asp:BoundField DataField="Login" HeaderText="Login" HeaderStyle-Width="300" />
        <asp:TemplateField HeaderText="Options" HeaderStyle-Width="75" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
            <ItemTemplate>
                <asp:LinkButton ID="updateSomething" runat="server" Text="Update" CausesValidation="false" OnClientClick="openDialog();" onclick="UpdateButton_Click" />
        </asp:TemplateField>
    </Columns>
</asp:GridView>

这不起作用,我收到一条错误说:在UpdatePanel'updNewUpdatePanel'中找不到ID为'updateSomething'的控件。

如何在gridview中使用ImageButton?

4 个答案:

答案 0 :(得分:9)

尝试将asp:AsyncPostBackTrigger添加到asp:GridView的{​​{1}}事件中,并处理该事件中的链接按钮点击

OnRowCommand

并在cs中创建像这样的事件

<asp:GridView ID="grdListUsers" runat="server" onRowCommand="grdListUsers_RowCommand">
     <asp:TemplateField>
           <asp:LinkButton ID="updateSomething" CommandName="update-something" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'/>
     </asp:TemplateField>
</asp:GridView>

答案 1 :(得分:3)

在链接按钮周围添加另一个更新面板,如下所示。

<asp:GridView ID="grdListUsers" runat="server" AutoGenerateColumns="false" AllowPaging="false" OnRowDataBound="grdRowDefListUsers" CssClass="mGrid" EmptyDataText="No users.">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Nome" HeaderStyle-Width="300" />
        <asp:BoundField DataField="Login" HeaderText="Login" HeaderStyle-Width="300" />
        <asp:TemplateField HeaderText="Options" HeaderStyle-Width="75" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
            <ItemTemplate>
                <asp:UpdatePanel ID="aa" runat="server">
                    <ContentTemplate>
                        <asp:LinkButton ID="updateSomething" runat="server" Text="Update" CausesValidation="false" OnClientClick="openDialog();" onclick="UpdateButton_Click" />
                    </ContentTemplate>
                    <Triggers>
                        <asp:PostBackTrigger  ControlID="updateSomething"/>
                    </Triggers>
              </asp:UpdatePanel>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

答案 2 :(得分:0)

您可以从UpdateButton_Click-Handler手动将UpdatePanel的UpdateMode设置为Conditionalupdate

<asp:UpdatePanel ID="UdpFormPanel" runat="server" UpdateMode="conditional" ChildrenAsTriggers="false"  >

LinkBut​​ton的Click-Event处理程序:

Protected Sub UpdateButton_Click(ByVal sender As Object, ByVal e As EventArgs)
    'blah....
    upNewUpdatePanel.Update()
End Sub

答案 3 :(得分:0)

您可以添加gridview的触发器

<Triggers>
      <asp:PostBackTrigger ControlID="gridview1" />
</Triggers>