Friday, June 22, 2012

Passing Parameters to querystring


1. Create new Website
2.Add Gridview on default page
3.Connect sqldatasource connection to it.


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataSourceID="SqlDataSource1" onrowdatabound="GridView1_RowDataBound">
            <Columns>
                <asp:TemplateField>
                <EditItemTemplate>
               
                </EditItemTemplate>
               
                <ItemTemplate>
               
                    <asp:Label ID="Label1" runat="server"
                        Text='<%# Eval("CustomerID") %>'></asp:Label>
                    <br />
                    <asp:HyperLink ID="HyperLink1" runat="server"
                        Text='<%# Eval("CategoryName") %>'></asp:HyperLink>
               
                </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:LoginConnectionString %>"
            SelectCommand="SELECT [CustomerID], [CategoryName] FROM [Categories]">
        </asp:SqlDataSource>


4.Add this code on Default.aspx.cs



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

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

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HyperLink hl = (HyperLink)e.Row.FindControl("HyperLink1");
            if (hl != null)
            {
                DataRowView drv = (DataRowView)e.Row.DataItem;
                string id = drv["CustomerID"].ToString();
                string companyname = drv["CategoryName"].ToString();
                hl.NavigateUrl = "~/Pages/Order.aspx?customerid=" + id.ToString() + "&Categoryname=" + Server.UrlEncode(companyname.ToString());
            }
        }
    }
}



OutPut:
After Clicking Book


Order.aspx



No comments:

Post a Comment