Codebehind
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGrid();
}
}
public void BindGrid()
{
SqlConnection conn = new SqlConnection("Data
Source='localhost';Initial Catalog='Northwind';Integrated Security=SSPI;Persist
Security Info=False ");
SqlDataAdapter da = new SqlDataAdapter("",
conn);
da.SelectCommand = new SqlCommand("select
CategoryName,CategoryID from Categories", conn);
DataSet ds = new DataSet();
da.Fill(ds, "data");
gdview.DataSource = ds.Tables[0].DefaultView;
gdview.DataBind();
}
Multiple Querystring Parameters
If you want to send mutliple items to the target
page,
<asp:GridView ID="gdview" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:HyperLinkField HeaderText="Category" DataTextField="CategoryName" DataNavigateUrlFields="CategoryID,CategoryName"
DataNavigateUrlFormatString="~/page.aspx?id={0}&name={1}"
/>
</Columns>
</asp:GridView>
Using Hyperlink Control in Repeater and DataList
Control
<asp:Repeater ID="rp1"
runat="server">
<ItemTemplate>
<table>
<tr>
<td>
<asp:HyperLink ID="hyp1"
runat="server" Text='<%#Eval("CategoryName")%>' NavigateUrl='<%#"page.aspx?id=" +
DataBinder.Eval(Container.DataItem,"CategoryID") + "&name=" +
DataBinder.Eval(Container.DataItem,"CategoryName")%>' ></asp:HyperLink>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
CodeBehind
protected void Page_Load(object sender, EventArgs e)
{
if
(!Page.IsPostBack)
{
BindRep();
}
}
public void BindRep()
{
SqlConnection conn
= new SqlConnection("Data Source='localhost';Initial
Catalog='Northwind';Integrated Security=SSPI;Persist Security Info=False ");
SqlDataAdapter da
= new SqlDataAdapter("", conn);
da.SelectCommand = new SqlCommand("select CategoryName,CategoryID from
Categories", conn);
DataSet ds = new DataSet();
da.Fill(ds, "data");
rp1.DataSource = ds.Tables[0].DefaultView;
rp1.DataBind();
}
|