Dynamic parameter passing through a 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 DataField="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 a hyperlink in Gridview to dynamically pass values ​​according to the row it is clicked on. Since I am new to this, I cannot do this. Please help me.

+3
source share
3 answers

add onRowCommand event to your grid

OnRowCommand="OnRowCommand_GridView1"

Then define a Button link with CommandName and CommandArgument

      <asp:LinkButton ID="lnk1" runat="server" Text="DoClick"  CommandName="Select" CommandArgument='<%#Eval("FileID") %>'></asp:LinkButton>

not by code

protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
    {

       if (e.CommandName == "Select")
        {
          int MyFileID = e.CommandArgument;
       //Now Perfrom here ur desired action
       }

Hope this helps you.

+1
source

Set the property hyperlink NavigateUrlas ...NavigateUrl='<%# Eval("FileID", "show.aspx?ID={0}" %>'

NavigateUrl='<%# Eval("FileID", "show.aspx?ID={0}") + "&FilePath=" + Eval("FilePath") %>'
+2
source

onRowCommand GridView

: GridView Row?

: GridView.RowCommand

0

All Articles