A.B.Cade
A.B.Cade

Reputation: 16905

jquery dialog and asp.net dynamic linkbutton

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 ?

Upvotes: 1

Views: 2197

Answers (2)

A.B.Cade
A.B.Cade

Reputation: 16905

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.

Upvotes: 2

Icarus
Icarus

Reputation: 63956

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.

Upvotes: 0

Related Questions