Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using linkbutton in gridview and handling it with rowcommand property.
but its commandargument shows the no. of row but I want some entry from my database(id value)
here is my code:

Html code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                        DataSourceID="SqlDataSource1" AllowPaging="True" 
                        DataKeyNames="Mail_id" onrowcommand="GridView1_RowCommand">
                        <columns>
                          <asp:ButtonField DataTextField="subject" HeaderText="Subject" 
                                SortExpression="subject" Text="Subject" />
                            <asp:ButtonField DataTextField="body" HeaderText="Content" Text="body" />
                            <asp:ButtonField DataTextField="sender" HeaderText="sender" Text="sender" />
                            <asp:ButtonField DataTextField="date" HeaderText="Date" Text="date" />
                        </columns>



c# code:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        report.Text = e.CommandArgument.ToString();
    }



please suggest something....
Posted
Updated 6-Sep-11 10:49am
v4

You can Bind the command argument with the LinkButton and pass into RowCommand Event. Let say you have below Template Field.
<asp:templatefield xmlns:asp="#unknown">
           <headertemplate>
             Status
           </headertemplate>
           <itemtemplate>
               <asp:imagebutton commandname="DataCommand" commandargument="<%# Eval("ID" )%>" text="<%# Eval("WebSite" )%>" imageurl="/test.pmg">
                     ID="lnk" runat="server" />
           </asp:imagebutton></itemtemplate>
       </asp:templatefield>

And here is the code you need to write in order to get the image button click,

Protected Void Grid_RowCommand(object sender, GridViewEventArgs e)
{
 if(e.CommandName=="DataCommand") 
    {
      // Get the value of command argument
        var value= e.CommandArgument;
      // Do whatever operation you want.  
    }
}


Hope this will help.
 
Share this answer
 
Comments
Sachin gulati 6-Sep-11 14:18pm    
i m using the default button field of gridview and there it is not allowed to define commandargument. pls suggest something else...
finally i found the solution, If someone has a better one, please send it.

we can use the Click event of the linkbutton to get the row information from inside the gridview

For example:

protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
Response.Write("Row Index of Link button: " + row.RowIndex +
"DataKey value:" + GridView1.DataKeys[row.RowIndex].Value.ToString());
}
 
Share this answer
 
Comments
TheKarateKid 18-Feb-15 14:07pm    
eitherway Solution 1 or Solution 2 works. Solution 1 is bubbling the event upto the gridview. Solution 2 handles the event where it occurs.
But only care you have to take is whichever first column/property you are using in DataKeyName attribute of the gridview, should not be with null value at any given point, else the code will not be able to read rest of the DataKeys.
gabrielnascimento 24-Oct-15 19:30pm    
Gambiarra da braba essa...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900