Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Client Side Confirmation on DataGrid "Delete" LinkButton

Reply
Thread Tools

Client Side Confirmation on DataGrid "Delete" LinkButton

 
 
=?Utf-8?B?QWxleCBNYWdoZW4=?=
Guest
Posts: n/a
 
      05-02-2006
I have a DataGrid control with a LinkButton command column that deletes the
row. What I want to do is set it up so that there's a client-side Confirm
alert BEFORE the actual Delete command gets called on the server-side. That's
easy to do with normal buttons, etc. as follows...

<asp:Button ID="ConfirmBtn" Text="ConfimMe!"
OnClientClick="if(!confirm('Are you sure?'))return false;"
OnClick="ConfirmBtnClickHandler" Runat="server" />

But OnClientClick, etc. are not supported for the DataGrid LinkButton. So
how could I do this?

Alex
 
Reply With Quote
 
 
 
 
=?Utf-8?B?U3RldmUgR29vZHllYXI=?=
Guest
Posts: n/a
 
      05-02-2006
You can always add it by code:
ConfirmBtn.Attributes.Add ("onclick", "if (! confirm('Are you sure?'))
return false;");

- Steve

"Alex Maghen" wrote:

> I have a DataGrid control with a LinkButton command column that deletes the
> row. What I want to do is set it up so that there's a client-side Confirm
> alert BEFORE the actual Delete command gets called on the server-side. That's
> easy to do with normal buttons, etc. as follows...
>
> <asp:Button ID="ConfirmBtn" Text="ConfimMe!"
> OnClientClick="if(!confirm('Are you sure?'))return false;"
> OnClick="ConfirmBtnClickHandler" Runat="server" />
>
> But OnClientClick, etc. are not supported for the DataGrid LinkButton. So
> how could I do this?
>
> Alex

 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      05-02-2006
Hi Alex,

For such scenario, as Steve has mentioned, we may need to use code to
programmatically add the client script for the button. Also, are you using
the built-in button column in DataGrid as below?

<asp:ButtonColumn ButtonType="linkButton" ></asp:ButtonColumn>

If so, it'll be very hard to reference the actual button object at runtime
(so as to custimze it) because the button id is autogenerated and it's
unsafe to locate sub controls through control index. I think you can
consider use template column instead of built-in column for such scenario.
And in VS IDE's designview , it provide the wizard for us to convert a
button column to a template column. For example, the above link button
column can be converted to the below template columnI add the id for the
link button so that we can referece it later):

=========
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton ID="lbDelete" runat="server"
CausesValidation="false" CommandName="" Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
==============

Then, in DataGrid's ItemdataBound event, we can use the following code to
locate the linkbutton and add client-sdie script behavior for it:

protected void DataGrid1_ItemCreated(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
LinkButton lb = e.Item.FindControl("lbDelete") as LinkButton;

lb.OnClientClick = "if(!confirm('are you sure to delete this
item?')){return false;}";

}
}


Hope this helps. If there is anything I missed, please feel free to post
here.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

 
Reply With Quote
 
=?Utf-8?B?QWxleCBNYWdoZW4=?=
Guest
Posts: n/a
 
      05-02-2006
G-d, you're good!!! Now just one question remains about this... In the sample
you gave, everything works but I don't know how to actually create a
server-side function that reacts to this TemplateColumn LinkButton. In your
example below, do I just add an OnClick handler to the <asp:LinkButton /> ?

Alex


"Steven Cheng[MSFT]" wrote:

> Hi Alex,
>
> For such scenario, as Steve has mentioned, we may need to use code to
> programmatically add the client script for the button. Also, are you using
> the built-in button column in DataGrid as below?
>
> <asp:ButtonColumn ButtonType="linkButton" ></asp:ButtonColumn>
>
> If so, it'll be very hard to reference the actual button object at runtime
> (so as to custimze it) because the button id is autogenerated and it's
> unsafe to locate sub controls through control index. I think you can
> consider use template column instead of built-in column for such scenario.
> And in VS IDE's designview , it provide the wizard for us to convert a
> button column to a template column. For example, the above link button
> column can be converted to the below template columnI add the id for the
> link button so that we can referece it later):
>
> =========
> <asp:TemplateColumn>
> <ItemTemplate>
> <asp:LinkButton ID="lbDelete" runat="server"
> CausesValidation="false" CommandName="" Text="Delete"></asp:LinkButton>
> </ItemTemplate>
> </asp:TemplateColumn>
> ==============
>
> Then, in DataGrid's ItemdataBound event, we can use the following code to
> locate the linkbutton and add client-sdie script behavior for it:
>
> protected void DataGrid1_ItemCreated(object sender, DataGridItemEventArgs e)
> {
> if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
> ListItemType.AlternatingItem)
> {
> LinkButton lb = e.Item.FindControl("lbDelete") as LinkButton;
>
> lb.OnClientClick = "if(!confirm('are you sure to delete this
> item?')){return false;}";
>
> }
> }
>
>
> Hope this helps. If there is anything I missed, please feel free to post
> here.
>
> Regards,
>
> Steven Cheng
> Microsoft Online Community Support
>
>
> ==================================================
>
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
>
> ==================================================
>
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
>
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>
>

 
Reply With Quote
 
=?Utf-8?B?QWxleCBNYWdoZW4=?=
Guest
Posts: n/a
 
      05-02-2006
So I tried adding an OnClick to the LinkButton in your example below and I
*do* get the event back. But here's the problem - I want to be able to
extract the data from the row of the LinkButton that was actually called but
the prototype for a LinkButton seems to deliver an EventArgs parameter as
opposed to a DataGridCommandEventArgs parameter. So how to I get the data out
of the row that was actually clicked?

"Steven Cheng[MSFT]" wrote:

> Hi Alex,
>
> For such scenario, as Steve has mentioned, we may need to use code to
> programmatically add the client script for the button. Also, are you using
> the built-in button column in DataGrid as below?
>
> <asp:ButtonColumn ButtonType="linkButton" ></asp:ButtonColumn>
>
> If so, it'll be very hard to reference the actual button object at runtime
> (so as to custimze it) because the button id is autogenerated and it's
> unsafe to locate sub controls through control index. I think you can
> consider use template column instead of built-in column for such scenario.
> And in VS IDE's designview , it provide the wizard for us to convert a
> button column to a template column. For example, the above link button
> column can be converted to the below template columnI add the id for the
> link button so that we can referece it later):
>
> =========
> <asp:TemplateColumn>
> <ItemTemplate>
> <asp:LinkButton ID="lbDelete" runat="server"
> CausesValidation="false" CommandName="" Text="Delete"></asp:LinkButton>
> </ItemTemplate>
> </asp:TemplateColumn>
> ==============
>
> Then, in DataGrid's ItemdataBound event, we can use the following code to
> locate the linkbutton and add client-sdie script behavior for it:
>
> protected void DataGrid1_ItemCreated(object sender, DataGridItemEventArgs e)
> {
> if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
> ListItemType.AlternatingItem)
> {
> LinkButton lb = e.Item.FindControl("lbDelete") as LinkButton;
>
> lb.OnClientClick = "if(!confirm('are you sure to delete this
> item?')){return false;}";
>
> }
> }
>
>
> Hope this helps. If there is anything I missed, please feel free to post
> here.
>
> Regards,
>
> Steven Cheng
> Microsoft Online Community Support
>
>
> ==================================================
>
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
>
> ==================================================
>
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
>
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>
>

 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      05-03-2006
Thanks for your response Alex,

Actually, for push button or linkbutton in DataGrid or Gridview, their
postback event will be mapped to the ItemCommand event (or RowCommand for
GridView) by the "CommandName" attribute. For example, for our link button
in the DataGrid template column, we can add the "Delete" value for the
"CommandName" attribute as below:

=======================
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton ID="lbDelete" runat="server"
CausesValidation="false" CommandName="Delete"
Text="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
===========================

Then, in the DataGrid's "ItemCommand" event, we can can handle the
linkbutton postback as below:

=======================
protected void DataGrid1_ItemCommand(object source,
DataGridCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
Response.Write("<br/>" + e.CommandName + ": " +
e.Item.ItemIndex);
}
}
==========================

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)










 
Reply With Quote
 
=?Utf-8?B?QWxleCBNYWdoZW4=?=
Guest
Posts: n/a
 
      05-03-2006
Is there ANYTHING you don't know?
Thanks!

Alex

"Steven Cheng[MSFT]" wrote:

> Thanks for your response Alex,
>
> Actually, for push button or linkbutton in DataGrid or Gridview, their
> postback event will be mapped to the ItemCommand event (or RowCommand for
> GridView) by the "CommandName" attribute. For example, for our link button
> in the DataGrid template column, we can add the "Delete" value for the
> "CommandName" attribute as below:
>
> =======================
> <asp:TemplateColumn>
> <ItemTemplate>
> <asp:LinkButton ID="lbDelete" runat="server"
> CausesValidation="false" CommandName="Delete"
> Text="Delete"></asp:LinkButton>
> </ItemTemplate>
> </asp:TemplateColumn>
> ===========================
>
> Then, in the DataGrid's "ItemCommand" event, we can can handle the
> linkbutton postback as below:
>
> =======================
> protected void DataGrid1_ItemCommand(object source,
> DataGridCommandEventArgs e)
> {
> if (e.CommandName == "Delete")
> {
> Response.Write("<br/>" + e.CommandName + ": " +
> e.Item.ItemIndex);
> }
> }
> ==========================
>
> Hope this helps.
>
> Regards,
>
> Steven Cheng
> Microsoft Online Community Support
>
>
> ==================================================
>
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
>
> ==================================================
>
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
>
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>
>
>
>
>
>
>
>
>
>
>

 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      05-04-2006
Thanks for your response Alex,

Sure, the more we've learnt, the more we'll find that we don't know.
Anyway, if there is anything we can help on ASP.NET developing , please
feel free to post here.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

 
Reply With Quote
 
AcesHidden AcesHidden is offline
Junior Member
Join Date: Nov 2010
Posts: 1
 
      11-23-2010
While Steven's solution will work, I found this to be easier.

Code:
<asp:TemplateColumn>
    <ItemTemplate>
        <asp:Button ID="Delete" CommandName="Delete" Text="Delete" runat="server" OnClientClick="return confirm('Are you sure you want to permanently delete this item?')" />
    </ItemTemplate>
</asp:TemplateColumn>
There is no server side code involved and you can get an actual button instead of a link.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LinkButton Client side code firing before server side code alexmac262@hotmail.com ASP .Net 1 03-22-2007 06:13 PM
Client-Side User Confirmation Within DataGrid simon_eyer@hotmail.com ASP .Net 2 06-29-2006 01:13 PM
Linkbutton does not look like a linkbutton Sathyaish ASP .Net 3 09-08-2005 09:41 AM
Linkbutton does not look like a linkbutton Sathyaish ASP .Net Datagrid Control 1 09-08-2005 08:44 AM
linkbutton: confirmation with javascript Gönen EREN ASP .Net 1 08-13-2003 05:03 AM



Advertisments