locked
Gridview _RowDeleting is firing my RowCommand? RRS feed

  • Question

  • User240536124 posted

    This is probably something simple but the Internet isn't helping on this on. I have a gridview with onrowdeleting, onrowediting, onrowupdating. All events do exactly as they should. If I add OnRowCommand all above events fire my OnRowCommand (which throws an error).

    I just added a template column to the table (linkbutton) with the event of "Attch" and this in the code behind: if (e.CommandName == "Attch").

    Everything works fine with this OnRowCommand. Only when it is in the table, now deletes and updates throw errors. Take it out and they work fine.

    It is 100% my OnRowCommand firing when it shouldn't. I can make it set a label name and it will set the label name on a rowupdating event.

    What did I get out of order. I know I used to do this with a ListView and never had any problems.

    Thanks,

    Tuesday, May 29, 2018 4:39 PM

Answers

  • User240536124 posted

    oned_gk

    Actualy i still not understand with command name is not "delete" because "Attach" is not delete

    I'm 100% with you on this. I've done this 800 times with a ListView and never had to post a question on the forums here.

    Some comments:

    1) The reason I had a trip to the database was to pull a value not in the Gridview. I needed more than the row id on the next .aspx page.

    I did figure out a way to get this value in the GridView datasource by putting it in a column and making the column visible = false.

    2) I'm with finding out the real solution to the problem. Possibly some weird binding issue? However, if I HAD to make that trip to the database AND I had to do it on the _RowCommand,, this gets me my trip to the database and the other commands in the GridView function with no Yellow Screens of Death.

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
    
            if (e.CommandName != "Edit" && e.CommandName != "Delete" && e.CommandName != "Cancel")
            {

    NOTE: And I do see your point about just sending the row id to the next page and doing the database trip there. It just seemed logical to go the database when I did the "Attch" command. Good point.

    Thanks for the help!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 1, 2018 4:35 PM

All replies

  • User753101303 posted

    Hi,

    This command system is already used by the GridView to handle its default buttons so technically speaking OnRowCommand happens for all of them and then the GridView handles that by calling the corresponding actions ultimately causing those OnRowDeleting etc... events.

    It's a bit unclear if you are already testing e.CommandName from within RowCommand ? This is what you should always do to ensure you are really processing just the command you are interested in.

    If doing that already knowing which error happens and on which line of code could perhaps help to better understand the exact problem you are running into.

    Tuesday, May 29, 2018 5:19 PM
  • User240536124 posted

    I actually read some documents on msdn and I kind of figured that was the case. However, I wasn't sure.

    Let me ask you a different way. I have everything working the way I want, but I want an image button or link button in a column that fires an event (open another .aspx page and "attach" some images to this row of data).

    Note: I have this working. I just can't do it all at the same time. It's one or the other.

    If I can't use a rowcommand, is there an alternative event I can fire to do this?

    Like I said, I used to do this in a ListView all the time. It's just been a few years and it isn't working in a GridView for me.

    This is all I'm doing:

    LinkButton is firing an event called "Attch"

    In codebehind, I'm catching it like this.

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
    if (e.CommandName == "Attch")

    Thanks!!

    Tuesday, May 29, 2018 7:07 PM
  • User-1716253493 posted

    What do you mean open another page and attach?

    What do you mean attach?

    Can you give codes related to the error?

    Tuesday, May 29, 2018 8:41 PM
  • User753101303 posted

    Or could you rather please tell which error you have and where it happens? It would be best to first see why it doesn't work rather than spending time on trying to find a solution to workaround some unknown error on something that should work.

    Edit: for example you have similar samples at https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand(v=vs.110).aspx . I believe the problem is not what you think but depends on what you are actually doing for your "Attach" command.

    Tuesday, May 29, 2018 9:06 PM
  • User240536124 posted

    I appreciate the help. I have this as a Gridview:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    ShowFooter="True" onrowcancelingedit="GridView1_RowCancelingEdit"
    onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
    onrowupdating="GridView1_RowUpdating"
    OnRowCommand="GridView1_RowCommand" DataKeyNames="id"><Columns>blah blah blah

    The OnRowCommand is wired like this:

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
    if (e.CommandName == "Attch")
    // pull a value from the database based on the row id number.

    All I need is to be able to pull a value from the database AND for the delete and edit buttons on the Gridview to keep working. As stated, my RowCommand works. My Delete and Edit Buttons work. But they don't all work at the same time. And it is because my RowCommand fires if I use the Edit or Delete buttons. If I take my RowCommand out, the Delete and Edit Buttons work fine.

    Also, all of the commands are done from code behind:

    Actually, my GridView was created from this aspsnippets code. It's pretty clean and works well. My code is only very slightly modified and It all worked well until I added the RowCommand.

    Tuesday, May 29, 2018 10:01 PM
  • User240536124 posted

    And this is how I call the command:

    <asp:BoundField DataField="col10" HeaderText="col10"/>
    <asp:TemplateField>
    <ItemTemplate>
    <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Attch">Attch</asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateField>

    Basically, by "attach," all I'm doing is on that LinkButton Click, pull a value from the database based on that row's id number. Then I do a response.redirect to another .aspx page and pass a variable in the URL. All seems to be working exactly as needed...except the one problem, as stated above.

    Thanks again.

    Tuesday, May 29, 2018 10:09 PM
  • User753101303 posted

    When posting about an error please always include in your first post the error message (or the best English translation you can) as well as the line of code on which it happens so that we don't have first to spend time about guessing which problem you SEE.

    I gave this a quick try, it works fine and from what you provided I see nothing that should obviously fail.

    Not directly related but if you just redirect to another page with a query string value that depends on the db row, you could likely generate a link that would go directly to the other page when clicked without posting back to the server (even if it does fit your need I would still suggest to solve your current issue for learning).

    Tuesday, May 29, 2018 10:26 PM
  • User-1716253493 posted

    Hi jay, all codes you have post are working

    But i don't know which line is not working

    Wednesday, May 30, 2018 1:09 AM
  • User240536124 posted

    So I'm back to this question. Is there an alternative way to wire up my link button than the RowCommand?

    I must have done something similar to this 800 times in a ListView. There has to be some little thing.

    Thanks,

    Wednesday, May 30, 2018 1:53 AM
  • User-1716253493 posted

    click event alternative

            LinkButton lb1 = (LinkButton)sender;
            GridViewRow row = (GridViewRow)lb1.NamingContainer;

    you can get cell text, datakey, control from the row

    Wednesday, May 30, 2018 4:04 AM
  • User753101303 posted

    If the only thing you are doing is redirecting to another page from the server side, you could use https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hyperlinkfield(v=vs.110).aspx to directly generate a link to this other page with the correct query string parameter and without doing first a roundtrip to the server.

    IMHO it is always best to solve an issue you have rather than just switch to something else (even if here the alternate option could be better). At least you'll know why it failed and you'll perhaps benefit from it if you need one day to handle again a RowCommand.

    On my side I'll stop here unless you are telling finally what is your EXACT problem.

    Wednesday, May 30, 2018 6:08 AM
  • User240536124 posted

    I mean, the exact problem is that updating or deleting a row in the Gridview is firing the RowCommand when it shouldn't. And the RowCommand is making a trip to the db, and throws an error when it gets fired. I can use the RowCommand if I put something in it that doesn't throw an error. For example, if I put Label2.Text = "No error"; in the Rowcommand and then delete or update a row, the row updates or deletes (AND the label gets set to "No error", because the RowCommand fired when it shouldn't have). The deal is, I don't need to set a label, I need to get a variable from the database in order to set up a page load on the response redirect.

    I have everything working the way I want except for this one weird issue.

    Thanks for your help.

    Wednesday, May 30, 2018 3:58 PM
  • User753101303 posted

    the Gridview is firing the RowCommand when it shouldn't.

    This is NOT the problem. GridView default buttons are handled using this RowCommand system. Actually ALL commands  will fire this event. In this event you should handle just the commands you are interested in and that's it (why should it fail if you handle just your command and ignore others ?????). It should work, I tried and it does work perfectly fine here. As you keep saying that it "doesn't work" rather than telling at last WHICH ERROR and at WHICH LINE OF CODE it happen, it's impossible to help.

    For the alternate approach, if the variable comes from the same row rendered by the Gridview, it could be immediately used in the HyperLinkField column (which would render a /otherpage.aspx?data=<value>).  So to speak rather than rendering the value in a GridView colum it is used as a query string parameter in the link.

    Wednesday, May 30, 2018 4:17 PM
  • User240536124 posted

    I stumbled upon the fix if you could help me with the proper syntax.

    This works (to Delete a row...but Edit still throws an error):

    if (e.CommandName != "Delete")
    {

    if (e.CommandName == "Attch")

    }

    I can delete with this. What is the proper way to add "Edit" "Update" commands to the "if !=" statement?

    I just swapped over from VB and I'm still learning some of this.

    Thanks!

    Wednesday, May 30, 2018 4:40 PM
  • User-1716253493 posted

    Can you post the whole codes, instead of explanation?

    Wednesday, May 30, 2018 9:23 PM
  • User240536124 posted

    OK, this works, but only on delete and only if I use the "if != "Delete"" statement. How to do multiple != variables in C#  for Edit, Update, etc., with the least amount of code?

     protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
    
            if (e.CommandName != "Delete")
            {
    
                if (e.CommandName == "Attch")
                {
                    Control ctl = e.CommandSource as Control;
                    GridViewRow CurrentRow = ctl.NamingContainer as GridViewRow;
                    object objTemp = GridView1.DataKeys[CurrentRow.RowIndex].Value as object;
                    if (objTemp != null)
                    {
                        string id = objTemp.ToString();
                        ViewState["rowid"] = id;
                    }
                }
    
                // trip to database to grab a value, create a string URL and redirect.
    
                Response.Redirect(strURL);
            }
    
        }

    Thank you.

    Thursday, May 31, 2018 5:32 PM
  • User-1716253493 posted

    Actualy i still not understand with command name is not "delete" because "Attach" is not delete

    Usualy we do

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Attch")
            {
                Control ctl = e.CommandSource as Control;
                GridViewRow CurrentRow = ctl.NamingContainer as GridViewRow;
                object objTemp = GridView1.DataKeys[CurrentRow.RowIndex].Value as object;
                if (objTemp != null)
                {
                    string id = objTemp.ToString();
                    ViewState["rowid"] = id;
                }
                // trip to database to grab a value, create a string URL and redirect.
                Response.Redirect(strURL);
            }
        }

    And other commands in editing, deleting, updating event

    or you can do update / delete in row command

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Attch")
            {
                Control ctl = e.CommandSource as Control;
                GridViewRow CurrentRow = ctl.NamingContainer as GridViewRow;
                object objTemp = GridView1.DataKeys[CurrentRow.RowIndex].Value as object;
                if (objTemp != null)
                {
                    string id = objTemp.ToString();
                    ViewState["rowid"] = id;
                }
                // trip to database to grab a value, create a string URL and redirect.
                Response.Redirect(strURL);
            }
            if(e.CommandName=="delete")
            {
                //do delete
            }
            if (e.CommandName == "update")
            {
                //do  update
            }
        }
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "delete":
                    //do delete
                case "attach":
                    //do attacth
                default:
                    //do somthing
            }
        }

    Thursday, May 31, 2018 7:20 PM
  • User753101303 posted

    Beforre you had response redirect AFTER the test on the attach block ? To me the following should work :

     protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
                if (e.CommandName == "Attch")
                {
                    Control ctl = e.CommandSource as Control;
                    GridViewRow CurrentRow = ctl.NamingContainer as GridViewRow;
                    object objTemp = GridView1.DataKeys[CurrentRow.RowIndex].Value as object;
                    if (objTemp != null)
                    {
                        string id = objTemp.ToString();
                        ViewState["rowid"] = id; // doesn't reallt maye sense as you'll redirect
                    }
                    // HERE does the redirect only for this Attch command
                                  // trip to database to grab a value, create a string URL and redirect.
      
    Response.Redirect(strURL); }
                           // HERE IT WOULD BE TRIGGERRED FOR ALL COMMANDS // Is this the issue you still didn't described so far ?
    // trip to database to grab a value, create a string URL and redirect. //Response.Redirect(strURL); } }

    For now it seems you want to go to a next page and pass the id for the current row ? Using an HyperLinkField would be likely easier :

                        <asp:HyperLinkField
                            DataNavigateUrlFields="Id"
                            DataTextField="Name"
                            DataNavigateUrlFormatString="~/test.aspx?id={0}"
                         />
    

    create a link column using the "Name" column for the test and creating a link to another page with the id as querystring parameter

    Friday, June 1, 2018 7:08 AM
  • User240536124 posted

    oned_gk

    Actualy i still not understand with command name is not "delete" because "Attach" is not delete

    I'm 100% with you on this. I've done this 800 times with a ListView and never had to post a question on the forums here.

    Some comments:

    1) The reason I had a trip to the database was to pull a value not in the Gridview. I needed more than the row id on the next .aspx page.

    I did figure out a way to get this value in the GridView datasource by putting it in a column and making the column visible = false.

    2) I'm with finding out the real solution to the problem. Possibly some weird binding issue? However, if I HAD to make that trip to the database AND I had to do it on the _RowCommand,, this gets me my trip to the database and the other commands in the GridView function with no Yellow Screens of Death.

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
    
            if (e.CommandName != "Edit" && e.CommandName != "Delete" && e.CommandName != "Cancel")
            {

    NOTE: And I do see your point about just sending the row id to the next page and doing the database trip there. It just seemed logical to go the database when I did the "Attch" command. Good point.

    Thanks for the help!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 1, 2018 4:35 PM