-->

ASP.NET - Adding an UpdatePanel trigger to a LinkB

2019-01-18 07:07发布

问题:

I was trying to update the content of a modal dialog, and this code works for me:

<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>

However, when I try to place the LinkButton inside a gridview, like so:

<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>

This does not work, I get an error saying: A control with ID 'updateSomething' could not be found for the trigger in UpdatePanel 'upNewUpdatePanel'.

How can I use the ImageButton inside the gridview?

回答1:

Try and add the asp:AsyncPostBackTrigger to the asp:GridView's OnRowCommand event and handle the link button click in that event

<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>

and in the cs create the event like this

protected void grdListUsers_RowCommand(object sender, GridViewCommandEventArgs e)
{
   if (e.CommandName == "update-something")
   {
      grdListUsers.SelectedIndex = Convert.ToInt32(e.CommandArgument);
   }
}


回答2:

Add another Update Panel surrounding your link button just like below.

<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>


回答3:

You could set the UpdatePanel's UpdateMode to Conditional and update it manually from the UpdateButton_Click-Handler:

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

LinkButton's Click-Event handler:

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


回答4:

You could add a trigger of the gridview

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