5 February 2014

HyperLink and redirect in gridview

  In general we use hyperlink to redirect other page or open new page to view specific details based on some conditions, below example redirecting to other page if user is eligible to view.


 <asp:GridView ID="gv1" runat="server" AutoGenerateColumns="false" OnRowDataBound="gv1_RowDataBound">
            <Columns>
                <asp:TemplateField HeaderText="Userprofile">
                    <ItemTemplate>
                        <asp:HyperLink ID="hyprlnkdetails" runat="server" />
                        <asp:Label ID="lblText" runat="server" Text=""></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

 protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            DataRowView drview = e.Row.DataItem as DataRowView;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                HyperLink HLdetails = e.Row.FindControl("hyprlnkdetails") as HyperLink;
                Label lblText = e.Row.FindControl("lblText") as Label;
                if (drview[5].ToString() == "True")
                {
                    HLdetails.Text = "Click here to view";
                    HLdetails.NavigateUrl = String.Format("~/details.aspx?UserID={0}", drview[8].ToString());
                }
                else
                {
                    HLdetails.Visible = false;
                    lblText.Text = "View Restricted";
                }
            }
        }

No comments:

Post a Comment