开发者

C# and ASP - Setting a command argument, then command argument disappearing

I'm trying to use a gridviewcommand to delete an entry in the gridview (as well as from the datacontext), but I want to ask the user to confirm or not first. I have a seperate linkbutton to execute the delete code using the ID of the item in the row that the user clicked the delete button in.

When the user clicks delete, my codebehind sets the CommandArgument property of the other linkbutton to basically be the same command argument that it passed. But when the user goes to confirm the deletion, all of a su开发者_高级运维dden the commandargument of the confirmation link is an empty string? Is the property being flushed after the first postback?

Here is my code:

ASPX

<asp:LinkButton ID="lbDelete" runat="server" Text="Delete" CommandName="delete" CommandArgument='<%# Eval("TheId") %>' /> //Delete LinkButton

<asp:LinkButton ID="confirmDelete" runat="server" OnClick="confirmDelete_Click" Text="Delete" /> //Confirmation Button

C#

rowCommandFunction(object sender, GridViewCommandEventArgs e)
{
     // blah blah blah
     ...
     confirmDelete.CommandArgument = e.CommandArgument.ToString();
     ...// I've put a breakpoint here, and it is setting the value properly...
}

confirmDelete_Click(object sender, EventArgs e)
{
    //...but when this line tries to run, comfirmDelete.CommandArgument is set to an empty string?
    int selectedId = int.Parse(confirmDelete.CommandArgument);
    ...
}


Yes, the second post back causes the issue..

I think you should remove the confirmDelete button and instead call a javascript function from lbDelete and show a confirm box from there. Then perform your delete operation inside the command event.

see the code below,

Aspx section,

     <asp:LinkButton ID="lbDelete" runat="server" Text="Delete" CommandName="delete" CommandArgument='<%# Eval("TheId") %>' OnClientClick="return ConfirmDelete()"/>

JavaScript section,

      function ConfirmDelete()
      {
         if(confirm("Are you sure to delete?"))
         {
             return true;
          }
           else
          {
            return false;
          }

      }


You have to enable the view state with EnableViewState="True" in the buttons.

By default buttons don't have the view state enabled, so you get the command argument disappearing after the postback, as you supposed.

Edit:

why don't set a CommandName to the second button and handle it in the RowCommand function?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜