Dynamic parameter passing through hyperlink in a DataGrid in asp.net(C#)
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px"
CellPadding="4">
<Columns>
<asp:BoundField DataField="FileID" HeaderText="FileID" />
<asp:BoundField DataField="FilePath" HeaderText="FilePath" />
<asp:BoundField Da开发者_如何学GotaField="UploadedBy" HeaderText="CreatedBy" />
<asp:BoundField DataField="CreatedDate" HeaderText="CreatedDate" />
<asp:HyperLinkField HeaderText="LINK" NavigateUrl="show.aspx" Text="SHOW" />
</Columns>
conn.Open();
SqlDataReader rdr = comm.ExecuteReader();
if (NAME.Equals("admin"))
{
GridView1.DataSource = rdr;
GridView1.DataBind();
}
else
{
GridView2.DataSource = rdr;
GridView2.DataBind();
}
rdr.Close();
I want to use the hyperlink in the Gridview to pass the values dynamically according to to the row it is clicked.As I'm new to this I'm not able to do that.Please anybody help me.
Set hyperlink NavigateUrl
property like...NavigateUrl='<%# Eval("FileID", "show.aspx?ID={0}" %>'
NavigateUrl='<%# Eval("FileID", "show.aspx?ID={0}") + "&FilePath=" + Eval("FilePath") %>'
add onRowCommand Event in your grid
OnRowCommand="OnRowCommand_GridView1"
Then define link Button with the CommandName and CommandArgument
<asp:LinkButton ID="lnk1" runat="server" Text="DoClick" CommandName="Select" CommandArgument='<%#Eval("FileID") %>'></asp:LinkButton>
and than on code behind
protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
int MyFileID = e.CommandArgument;
//Now Perfrom here ur desired action
}
Hope this will help you.
Check onRowCommand of GridView
Check the following link: How to get the current row in GridView Row Command Event?
For information about row command: GridView.RowCommand Event
精彩评论