Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Unable to cast object of type 'System.Web.UI.WebControls.Button' to type 'System.Web.UI.WebControls.GridView'.

this button is in page
C#
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:
C#
Unable to cast object of type 'System.Web.UI.WebControls.Button' to type 'System.Web.UI.WebControls.GridView'.

This button is in gridview
C#
protected void btnRewrk_OnClick(object sender, EventArgs e)
 {
   GridViewRow row = (GridViewRow)(((Control)sender).NamingContainer);
 }
Posted
v2
Comments
Can you please elaborate what exactly you want to do ?

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:

XML
<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.

C#
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
 
Share this answer
 
v2
Instead of this use this

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


C#
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.
 
Share this answer
 
v2

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