Using a grid view Select or template link as a trigger for activity in a separate Update panel

.net 2, C#, AJAX

I have two update panels in my page. The first one has a gridview in it and i want a link in each of the rows of the gridview to act as an asynch postback trigger for the second update panel. However the id of the link in the first grid is never found, which i kind of expected to happen and so i get an error. Is there anyway to do this. I have tried the link as being the select command field and also the template field as shown below,

thanks in advance

Basic page code:

<asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">
 <ContentTemplate>

 <asp:GridView ID="grdSpecifics" runat="server" DataKeyNames="specificFieldID" AutoGenerateColumns="False" OnRowDeleting="deleteSpecificField" OnRowEditing="editSpecificField" OnRowUpdating="updateSpecificField" OnRowCancelingEdit="cancelSpecificField" OnRowDataBound="populateGridRows" OnSelectedIndexChanged="grdSpecifics_SelectedIndexChanged" BorderWidth="1px">
                           <Columns>
                               <asp:TemplateField HeaderText="Field Title" SortExpression="title">
                                   <EditItemTemplate>
                                        <asp:TextBox ID="txtSpecificEditTitle" runat="server" Text='<%# Bind("title") %>'></asp:TextBox>
                                   </EditItemTemplate>
                                   <ItemTemplate>
                                       <asp:Label ID="Label1" runat="server" Text='<%# Bind("title") %>'></asp:Label>
                                   </ItemTemplate>
                               </asp:TemplateField>
                               <asp:BoundField DataField="fieldType" HeaderText="Field Type" SortExpression="fieldType" ReadOnly="True" />
                               <asp:TemplateField HeaderText="Graded" >
                                <ItemTemplate>
                                    <asp:CheckBox ID="chkGraded" Checked='<%# Eval("graded") %>' runat="server" Enabled="false" />
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:CheckBox ID="chkGraded"  Checked='<%# Eval("graded") %>' runat="server" Enabled="true" />
                                </EditItemTemplate>
                               </asp:TemplateField>
                               <asp:TemplateField ShowHeader="False"> ----Used to be a command field
                                   <ItemTemplate>
                                       <asp:LinkButton ID="optionButton" runat="server"  CausesValidation="False" CommandName="Select"
                                           Text="Options"></asp:LinkButton>
                                   </ItemTemplate>
                               </asp:TemplateField>
                               <asp:CommandField ShowEditButton="True" />
                               <asp:CommandField ShowDeleteButton="True" />
                           </Columns>
                       </asp:GridView>


</ContentTemplate>
</asp:UpdatePanel>

<asp:UpdatePanel ID="UpdatePanel4" runat="server" UpdateMode="Conditional">
 <ContentTemplate>

Second Gridview

</ContentTemplate>
</asp:UpdatePanel>
LVL 1
MontySRAsked:
Who is Participating?
 
REA_ANDREWCommented:
OK here is how to do it.

You would specify the gridview as the ControlID of the AsyncPostBack Trigger and use the EventName of RowCommand for example here is example coide i have just tested:

    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
           
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="RowCommand" />
            </Triggers>
        </asp:UpdatePanel>
        <asp:UpdatePanel ID="UpdatePanel2" runat="server">
            <ContentTemplate>
                <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="au_id" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
                    <Columns>
                        <asp:CommandField ShowSelectButton="True" />
                        <asp:BoundField DataField="au_id" HeaderText="au_id" ReadOnly="True" SortExpression="au_id" />
                        <asp:BoundField DataField="au_lname" HeaderText="au_lname" SortExpression="au_lname" />
                        <asp:BoundField DataField="au_fname" HeaderText="au_fname" SortExpression="au_fname" />
                        <asp:BoundField DataField="phone" HeaderText="phone" SortExpression="phone" />
                        <asp:BoundField DataField="address" HeaderText="address" SortExpression="address" />
                        <asp:BoundField DataField="city" HeaderText="city" SortExpression="city" />
                        <asp:BoundField DataField="state" HeaderText="state" SortExpression="state" />
                        <asp:BoundField DataField="zip" HeaderText="zip" SortExpression="zip" />
                        <asp:CheckBoxField DataField="contract" HeaderText="contract" SortExpression="contract" />
                    </Columns>
                </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
                    SelectCommand="SELECT * FROM [authors]"></asp:SqlDataSource>
            </ContentTemplate>
        </asp:UpdatePanel>
    </form>

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        TextBox1.Text = GridView1.SelectedIndex.ToString();
    }
}
0
 
MontySRAuthor Commented:
Thanks REA_Andrew, great answer, one thing i should mention is that i need the trigger the link in the grid to not update the control panel it sits in, just the one which contains the trigger....not sure if this is possible
0
 
REA_ANDREWCommented:
Unless your logic makes something happen inside the same control panel nothing should update in it.  Your logic should refer to controls inside the other update panel and reference no controls in the current updat panel if you wish nothing be updated.

Andrew
0
 
MontySRAuthor Commented:
Thanks Andrew i will investigate further
Cheers
Duncan
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.