Cannot start object of type System.Web.UI.WebControls.GridView in ASP.NET

I wrote a method that deletes rows from my asp.net gridview when the delete button is pressed, and another method when the edit button is pressed. The Edit and Delete buttons are part of the gridview's built-in controls.

However, when I click these buttons (edit / delete) and an exception is thrown. Cannot pass an object of type "System.Web.UI.WebControls.GridView" to enter "System.Web.UI.WebControls.Button". that points to a string

Button btn = (Button)sender; 

The problem is that this line is not associated with any of the editing or deleting methods. This is due to the asp button in another column, and for that reason I get lost. How can I solve this problem? What causes OnRowDeleting and OnRowEditing conflicts with the showResponses method?

Here is aspx

 <asp:GridView runat="server" ID="gvShowQuestionnaires" HeaderStyle-CssClass="table_header" CssClass="view" AlternatingRowStyle-CssClass="alt" AlternatingRowStyle-BackColor="#f3f4f8" AutoGenerateColumns="False" DataKeyNames='QuestionnaireID' OnRowDeleting="gvShowQuestionnaires_RowDeleting" OnRowEditing="gvShowQuestionnaires_RowEdit" FooterStyle-CssClass="view_table_footer" OnRowCommand="showResponses"> <Columns> <asp:BoundField DataField="QuestionnaireID" HeaderText="ID" HeaderStyle-Width="80px" ItemStyle-CssClass="bo"></asp:BoundField> <asp:BoundField DataField="QuestionnaireName" HeaderText="Questionnaire Name" /> <asp:TemplateField HeaderText="Results" HeaderStyle-Width="150px"> <ItemTemplate> <asp:Button runat="server" ID="button1" CommandArgument='<%# Eval("QuestionnaireID") %>' OnClick="showResponses" text="Results"/> </ItemTemplate> </asp:TemplateField> <asp:CommandField HeaderText="Options" ShowDeleteButton="True" ShowEditButton="true" EditText="Edit"></asp:CommandField> </Columns> </asp:GridView> 

And here is the code behind:

 protected void gvShowQuestionnaires_RowDeleting(object sender, GridViewDeleteEventArgs e) { int questionnaireID = (int)gvShowQuestionnaires.DataKeys[Convert.ToInt32(e.RowIndex)].Value; GetData.DeleteQuestionnaire(questionnaireID); gvShowQuestionnaires.DataSource = DT; gvShowQuestionnaires.DataBind(); } protected void gvShowQuestionnaires_RowEdit(object sender, GridViewEditEventArgs e) { string id = gvShowQuestionnaires.Rows[e.NewEditIndex].Cells[0].Text; Session["qID"] = id; Response.Redirect("~/members/edit_questionnaire.aspx"); } protected void showResponses(object sender, EventArgs e) { Button btn = (Button)sender; string id = btn.CommandArgument.ToString(); Session["qID"] = id; Response.Redirect("~/members/questionnaire_responses.aspx"); } 

Any help would be greatly appreciated.

+7
source share
3 answers

It seems to me that this is understandable. Here:

 <asp:GridView runat="server" ... OnRowCommand="showResponses"> 

you bind the RowCommand event to showResponses . And here, in showResponses , you assume the sender is a button:

 protected void showResponses(object sender, EventArgs e) { Button btn = (Button)sender; string id = btn.CommandArgument.ToString(); Session["qID"] = id; Response.Redirect("~/members/questionnaire_responses.aspx"); } 

The sender is not a button - it is a view of the grid. If you need a command argument, you should use GridViewCommandEventArgs.CommandArgument .

 protected void showResponses(object sender, GridViewCommandEventArgs e) { Session["qID"] = e.CommandArgument; Response.Redirect("~/members/questionnaire_responses.aspx"); } 
+10
source

Add the CommandName attribute to your button1 in the GridView.

 <asp:Button runat="server" ID="button1" CommandName="Button1" CommandArgument='<%# Eval("QuestionnaireID") %>' OnClick="showResponses" text="Results"/> 

Then in showResponses do this ...

 protected void showResponses(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Button1") { Session["qID"] = e.CommandArgument; Response.Redirect("~/members/questionnaire_responses.aspx"); } } 
0
source
 var btnSender = (Button)e.CommandSource; 
0
source

All Articles