473,385 Members | 1,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 5247
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Stefan Strasser | last post by:
why is delete an expression and not a statement? (in my draft copy of the standard). I was about to ask the same question about "throw" but found an expression case of throw("return boolvalue ? 5...
13
by: gary | last post by:
Hi, We all know the below codes are dangerous: { int *p = new int; delete p; delete p; } And we also know the compilers do not delete p if p==NULL. So why compilers do not "p = NULL"...
0
by: Nate Harel | last post by:
Hi all, I have a client that who has a Access issue. They keep a "master.mdb" file on their server, and each of the users running Access on their local machine, accesses the file over the...
8
by: John Baker | last post by:
Hi: Access 2000 W98! I have a table with numerous records in it, and am attempting to delete certain records that have been selected from it. These are selected based on the ID number in a...
4
by: Calvin Lai | last post by:
Hi all, Does anyone know how I could add a confirm (client side) feature on those Delete Linkbutton in DataGrid? Thanks,
3
by: Shawn | last post by:
I have found many cases in that after a row has been deleted from a datagrid using a linkbutton and then hit the browser refresh button the row previous to the origianal delete will as well be...
19
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is...
30
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
3
by: tvnaidu | last post by:
I compiled tinyxml files (.cpp files) and when I am linking to create final exe in Linux, I am getting lot of errors saying "undefiend reference" to "operator new" and "delete", any idea?. ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.