469,898 Members | 1,945 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

Client Side Confirmation on DataGrid "Delete" LinkButton

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
May 2 '06 #1
7 5061
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

May 2 '06 #2
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 column:(I 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.)

May 2 '06 #3
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 column:(I 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.)

May 2 '06 #4
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 column:(I 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.)

May 2 '06 #5
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.)




May 3 '06 #6
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.)




May 3 '06 #7
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.)

May 4 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Stefan Strasser | last post: by
reply views Thread by Nate Harel | last post: by
4 posts views Thread by Calvin Lai | last post: by
3 posts views Thread by Shawn | last post: by
19 posts views Thread by Daniel Pitts | last post: by
30 posts views Thread by Medvedev | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.