ASP.NET - Adding an UpdatePanel trigger to a LinkButton inside a gridview

独自空忆成欢 提交于 2019-11-27 02:58:04

问题


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 add a trigger of the gridview

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



回答4:


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


来源:https://stackoverflow.com/questions/6276544/asp-net-adding-an-updatepanel-trigger-to-a-linkbutton-inside-a-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!