1

I Have a gridview which has a row that contains LinkButtons that were added dynamically. when these LinkButtons are clicked I need to show a confirmation dialog. I tried to work as suggested in this post: JQuery DIalog and ASP.NET Repeater but it doesn't work, the postBackReference doesn't contain the right ID (it ignores the placeholder) this is my code:

GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
{
   //some code here

   LinkButton lb = new LinkButton();
   lb.Text = "something";
   lb.ID = "someId";
   string postBackReference = ClientScript.GetPostBackEventReference(lb, string.Empty);
   lb.OnClientClick = "javascript: showConf(function(){"+ postBackReference  +"});return false;";

   TableCell cell = new TableCell();
   cell.Controls.Add(lb);
   e.Row.Cells.Add(cell);
}

Does anyone has an idea ?

2
  • 1
    Found It! Should have used the RowDataBound event instead (for registering my JS function with the asp.net __doPostback) Commented Dec 6, 2011 at 16:02
  • I see what you mean. Regardless of whether you do it on OnRowDataBound or OnRowCreated I would add an extra parameter to the showConfirmation function with your own id so that you don't have to parse the server-side id at all to know on which record you are operating. I put some details on the comments section of my answer. Commented Dec 6, 2011 at 21:49

2 Answers 2

2

So here is a solution that worked for me: Basically I worked according to the solution in this post: JQuery DIalog and ASP.NET Repeater The only difference was that I had to use the RowCreated Event for adding my dynamic LinkButtons and RowDataBound Event for registering my client function (otherwise the original __doPostBack wouldn't get the ID param correctly (as if it ignores the fact that it is in a place holder)). So my code behind looks like this now:

GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
{
   //some code here

   LinkButton lb = new LinkButton();
   lb.Text = "something";
   lb.ID = "someId";

   TableCell cell = new TableCell();
   cell.Controls.Add(lb);
   e.Row.Cells.Add(cell);
}

and:

GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
   //some code here

   LinkButton lb = e.Row.FindControl("someId") as LinkButton;
   string postBackReference = ClientScript.GetPostBackEventReference(lb, string.Empty);
   lb.OnClientClick = "javascript: showConf(function(){"+ postBackReference  +"});return false;";

}

client function- showConf and markup stay as they were.

Sign up to request clarification or add additional context in comments.

1 Comment

Welcome to Stack Overflow! As soon as you are able, be sure and mark your answer as accepted so others will know that there is a solution.
0

I don't know exactly what's the role of JQuery for your scenario. I imagine you want to show some sort of fancy confirmation box, if you provide the details I'll provide a more concrete answer, but for now, this is how is done with pure javascript:

GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
{

   LinkButton lb = new LinkButton();
   lb.Text = "something";
   lb.ID = "someId";
   lb.OnClientClick = "javascript: return confirm('Are you sure that you want to do this and that?'); ";

   TableCell cell = new TableCell();
   cell.Controls.Add(lb);
   e.Row.Cells.Add(cell);
}

UPDATE - Try something like this for the JQuery UI approach

  1. have a div with id="dialog-confirm" in your markup as so:

     <div id="dialog-confirm" title="" ></div>
    
  2. Define a javascript function called showConfirmation as so:

     function showConfirmation(confirmationMessage)
     {
          $("#dialog-confirm").dialog("destroy");
          $( "#dialog-confirm" ).dialog({
            resizable: false,
            height:140,
            title: confirmationMessage,
            modal: true,
            buttons: {
                "Yes": function() {
                    $( this ).dialog( "close" );
                    __doPostBack(linkItemID, '');//will cause postback
                },
                Cancel: function() {
                    $( this ).dialog( "close" );
                }
            }
        });
     return false; //it will never postback
     }
    
  3. Now your code behind should look like this:

     GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
     {
       LinkButton lb = new LinkButton();
        lb.Text = "something";
        lb.ID = "someId";
        lb.OnClientClick = "return showConfirmation('Are you sure you want to do this and that?','"+lb.ID+"'); ";
        TableCell cell = new TableCell();
        cell.Controls.Add(lb);
        e.Row.Cells.Add(cell);
     }
    

Note: Code above hasn't been tested but that should be very very close to what you need.

11 Comments

Sorry, forgot to say, I want to use jquery-ui dialog
just replace the return confirm with a function that shows jquery ui dialog.
Thanks lcarus and thiagoleite, but I don't think it will work since it won't stop the original __doPostBack generated by the ASP.Net from firing. So before the user will be able to choose a button, the postback will occur
@A.B.Cade: Yes, it will stop it because the javascript function returns false when the user clicks on the Cancel button on the dialog. Notice how OnClientClick has return showConfirmation... That's exactly the same way plain vanilla confirm works. If you don't want the postback at all under any circumstance, simply change return true for return false on the Yes button also, or remove both returns altogether and let the function reach the last line, which always returns false.
Thanks again lcarus, that was my first thought but it never worked for me. Apparently, the jquery dialog doesn't suspend the process. Does your code work for you ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.