Monday, April 25, 2011

Pass values in querystring with the hyperlink field inside the gridview

Hi,
I got the requirement to pass the two values in the form of query string of the hyperlink field in the grid view.One parameter is fron the database and another one is server side value.
Here is the solution for the above requirement.
FirstPage:
<%@ Page Language="C#"  %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
 
    public string Name = "Prabhakara";
 
    public string GetURL(object productName)
    {
        string url = string.Empty;
        url = string.Format("Testing.aspx?ProductName={0}&Name={1}", productName.ToString(), Name);
        return url;
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="Gridview1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="ProductID"
            AllowPaging="True" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField HeaderText="ProductID" DataField="ProductID" ReadOnly="True" SortExpression="ProductID" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:HyperLink ID="hlMenuItem" runat="server" NavigateUrl='<%# GetURL(DataBinder.Eval(Container.DataItem, "ProductName")) %>'
                            Text='<%# DataBinder.Eval(Container.DataItem, "ProductName")%>'>
                        </asp:HyperLink>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [ProductID], [ProductName] FROM [Alphabetical list of products]">
        </asp:SqlDataSource>
        <asp:Label ID="Label1" runat="server" Text="<%= Name %>"></asp:Label>
    </div>
    </form>
</body>
</html>
TestingPage:

<%@ Page Language="C#" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
 
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("HyperLinkExample.aspx");
    }
 
    protected void Page_Load(object sender, EventArgs e)
    {
         Label1.Text = Request.QueryString["ProductName"].ToString() + " and " + Request.QueryString["Name"].ToString();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <br />
        <asp:Button ID="btnBack" runat="server" Text="Back" OnClick="btnBack_Click" />
    </div>
    </form>
</body>
</html>


HappyCoding....

No comments:

Post a Comment