user3581461
user3581461

Reputation: 55

Open new gridview through Hyperlink

How can I open a new gridview through hyperlink on same page, I don't want to close existing gridview but want to show another gridview adjacent to this one, when user clicks on any hyperlink on this table. I have some data like below and I want to open a new gridview on same page when I click on any of these hyperlinks. New gridview will be having data from different table.

I cannot insert image as I do not have enough reputation but can share my HTML Code:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" DataKeyNames="UN_AT_Group">
    <Columns>
           <asp:HyperLinkField DataTextField="Group_Description" DataNavigateUrlFields="UN_AT_Group" DataNavigateUrlFormatString="~/Details.aspx?Id={0}"
    </Columns>
</asp:GridView>

Upvotes: 2

Views: 1750

Answers (1)

DatRid
DatRid

Reputation: 1169

Use a LinkButton instead:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" DataKeyNames="UN_AT_Group" OnRowCommand="GridView1_RowCommand" Visible="True">
    <Columns>    
        <asp:TemplateField>
            <ItemTemplate>
                 <asp:LinkButton ID="GotoNextGrid" runat="server" CommandArgument="NextGrid" CommandName="NextGrid" Text="Show Rights">
                 </asp:LinkButton>  
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>  
</asp:GridView>

Do the same for you second GridView but set Visibile="false".

and then catch it in CodeBehind: (Take care, make sure that what I have as Label here can also be something else... Whatever your DataTextField is.

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "NextGrid")
    {
        LinkButton lb = (LinkButton)e.CommandSource;
        GridViewRow gvr = (GridViewRow)lb.NamingContainer;
        Label lbl = gvr.FindControl("GroupDescription") as Label;
        string description = lbl.Text;
        GridView1.Visible = false;
        GridView2.Visible = true;
        FillDataForGridView2(description) //Fill the Data for GridView2 here and pass description as parameter
    }
}

Take care if you use UpdatePanel, then you need to add an Trigger:

<Triggers>
    <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="RowCommand" />
</Triggers>

I hope this helps.

If you have any questions just ask.

Upvotes: 1

Related Questions