Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 395
aperez Avatar asked Jun 08 '11 09:06

aperez


People also ask

How do I add a postback trigger in UpdatePanel?

Find control (no problem) link. CommandArgument = e. VisibleIndex. ToString(); PostBackTrigger trigger = new PostBackTrigger(); trigger.

What is the use of trigger in UpdatePanel in asp net?

Specifies a control and event that will cause a full page update (a full page refresh). This tag can be used to force a full refresh when a control would otherwise trigger partial rendering.


3 Answers

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);
   }
}
like image 79
Atzoya Avatar answered Oct 17 '22 23:10

Atzoya


You could add a trigger of the gridview

<Triggers>
      <asp:PostBackTrigger ControlID="gridview1" />
</Triggers>
like image 30
Marztres Avatar answered Oct 17 '22 21:10

Marztres


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>
like image 24
Telan Niranga Avatar answered Oct 17 '22 21:10

Telan Niranga