1

I want to ask how to get a text or value of a button inside the gridview? but i want to get the text value from this onrowcommand which use GridViewCommandEventArgs as its parameter.

as if im using onrowdatabound is (GridViewRowEventArgs), which makes it easy for me to get the button.text inside the gridview

string example = ((Button)e.Row.FindControl("btnStop")).Text;

I want to get the button.text to do an if else loop inside the onrowcommand. Anyone know how?

 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" CellPadding="2" CellSpacing="2"    HorizontalAlign="Center"   PageSize="5"  Width="133%" DataKeyNames="SurveyID" DataSourceID="SqlDataSource1" 
  AutoGenerateColumns="False" onrowcommand="stop_survey" 
       onrowdatabound="filter_select" onselectedindexchanging="selected" 
                                   >

code behind

 public void filter_select(object sender, GridViewRowEventArgs e)
 {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {....
     ((Button)e.Row.FindControl("btnStop")).Text = "Start"; 
    }
 }

 public void stop_survey(object sender, GridViewCommandEventArgs e)
 {
      //i want to get the "btnStop" button text which is nested on the gridview.
 }

i want to get the btnStop text, as I want to have different sqlstatement depending on its text (eg. Start or Stop) the problem is i cant do e.Row inside stop_survey. Please guide me.

3
  • do you want to get button text in onrowcommand event Commented Dec 11, 2011 at 14:44
  • I have added more details @pratapchandra . help me please Commented Dec 11, 2011 at 15:23
  • take a look at my answer i am hoping that it will helps you Commented Dec 11, 2011 at 15:26

1 Answer 1

4

Create a object of GridViewRow from refrence of command source row.

GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;

But keep in mind that which type of object control is you are using to call event in above code is LinkButton. But if You Calling Rowcommand event from Simple Button then You need to Write this:

GridViewRow row = (GridViewRow)((Button)e.CommandSource).NamingContainer;

After Creating Row Object You can simply find any control from Gridview using rowindex.

Label LabelEmail = (Label)GridView1.Rows[row.RowIndex].FindControl("LabelEmail");

Finally You Can Access that Properties of that Control.

LabelResult.Text = LabelEmail.Text; 

Same as Above You can also find Datakeys of gridview using GridViewRow rowindex.

int code = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values[0].ToString()); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you very much.. i research about this and only got the 2nd code. looks like im missing the 3rd one. Thank you pratapchandra!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.