Click here to Skip to main content
11,641,387 members (60,620 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
Unable to cast object of type 'System.Web.UI.WebControls.Button' to type 'System.Web.UI.WebControls.GridView'.
this button is in page
protected void BtnSave_OnClick(object sender, EventArgs e)
{
     btnRewrk_OnClick(sender,e); 
}
here i want to call the bellow event method in which button is in gridview, so here i am getting error:
Unable to cast object of type 'System.Web.UI.WebControls.Button' to type 'System.Web.UI.WebControls.GridView'.
This button is in gridview
protected void btnRewrk_OnClick(object sender, EventArgs e)
 {
   GridViewRow row = (GridViewRow)(((Control)sender).NamingContainer);
 }
Posted 6-Dec-12 23:43pm
v2
Comments
Tadit Dash at 7-Dec-12 6:01am
   
Can you please elaborate what exactly you want to do ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

HI anbujeremiah,

This is a link that can help you in finding your solution please have a look into it.
Access buttons in Code behind

First of all add the button to your grid as following code snippet:

<asp:TemplateField>
                  <ItemTemplate>
                        <asp:LinkButton ID = "lnkSil" runat="server" CommandName="bla"
CommandArgument='<%# ((GridViewRow) Container).RowIndex %>' Text= "Sil" ></asp:LinkButton>
                  </ItemTemplate>
                  </asp:TemplateField>

Then add a row data bound function that can able to access the control. Follow the below code snippet.

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
 {
     int currentRowIndex = Int32.Parse(e.CommandArgument.ToString());
     LinkButton bf = (LinkButton)gv.Rows[currentRowIndex].Cells[1].Controls[0];
     ...
 }


Thanks & Regards
sisir
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Instead of this use this

<asp:button runat="server" id="BtnSave"  rowindex="<%# Container.DisplayIndex %>" >
OnClick="BtnSave_OnClick"/>
</asp:button>

protected void BtnSave_OnClick(object sender, EventArgs e){
Button ibtn1 = sender as Button;
int rowIndex = Convert.ToInt32(ibtn1.Attributes["RowIndex"]);
 
//Use this rowIndex in your code to get which button is clicked
  GridViewRow row = Gridview.rows[rowIndex];
}

You dont nedd to call event of another button here.Instead you can use common method for both event.
  Permalink  
v2

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

  Print Answers RSS
0 Mika Wendelius 400
1 DamithSL 213
2 CPallini 159
3 OriginalGriff 145
4 Afzaal Ahmad Zeeshan 129
0 Mika Wendelius 460
1 OriginalGriff 215
2 DamithSL 213
3 CPallini 169
4 Afzaal Ahmad Zeeshan 129


Advertise | Privacy | Mobile
Web02 | 2.8.150731.1 | Last Updated 7 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100