Hi, I have encounter a problem when i try to insert a javascript confirm message. Can someone please help me?
I have a gridview in display.aspx where allow to me to view and delete some data. Also, when user want to delete the data, a confirmation message box will be prompt. Upon user click ok, it will pass some value and redirect user to another page, control.aspx.
With the below code, i can pass the value(promotionId) to control.aspx and delete.
In the CodeBehind attach the confirmation and event handler:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Find the HyperLink with an ID of "gridHyperLink"...
HyperLink gridHyperLink = (HyperLink)e.Row.FindControl("gridHyperLink");
haapy
Member
81 Points
36 Posts
How to prompt a confirmation box using javascript
Nov 12, 2008 11:37 AM|LINK
Hi, I have encounter a problem when i try to insert a javascript confirm message. Can someone please help me?
I have a gridview in display.aspx where allow to me to view and delete some data. Also, when user want to delete the data, a confirmation message box will be prompt. Upon user click ok, it will pass some value and redirect user to another page, control.aspx.
With the below code, i can pass the value(promotionId) to control.aspx and delete.
<asp:HyperLinkField DataNavigateUrlFields="PromotionId" DataNavigateUrlFormatString="control.aspx?PID={0}" Text="Delete" />
The problem is when i try to use javascript window.confirm() i cant link to the aspx. Can someone help me? Thanks.
<asp:HyperLinkField DataNavigateUrlFields="PromotionId" DataNavigateUrlFormatString='javascript:confirm("control.aspx?PID={0}")' Text="Delete" />
DeanHunter
Member
296 Points
68 Posts
Re: How to prompt a confirmation box using javascript
Nov 12, 2008 01:04 PM|LINK
Hi,
This should work for your confirm.
var answer = confirm("Delete record?");if (answer){window.location="control.aspx?PID={0}";}else{alert("Record not deleted")}
NC01
All-Star
82577 Points
15430 Posts
MVP
Re: How to prompt a confirmation box using javascript
Nov 12, 2008 01:23 PM|LINK
In the aspx file, remove the asp:HyperLinkField and use a TemplateField instead:
<form id="form1" runat="server">
<asp:GridView ID="GridView1" AutoGenerateColumns="False" ShowHeader="True" runat="server" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="gridHyperLink" NavigateUrl="#" Text="Delete" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
In the CodeBehind attach the confirmation and event handler:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Find the HyperLink with an ID of "gridHyperLink"...
HyperLink gridHyperLink = (HyperLink)e.Row.FindControl("gridHyperLink");
if (gridHyperLink != null)
{
DataRow dataRow = ((DataRowView)e.Row.DataItem).Row;
string dataColumnValue = dataRow["PromotionId"].ToString();
string confirmMessage = string.Format(
"Are you sure that you want to delete Promotion ID #{0}?",
dataColumnValue);
string clickHandler = string.Format(
"if ( window.confirm('{0}') ) {{window.location.href = 'default.aspx?PID={1}';}}",
confirmMessage,
dataColumnValue);
gridHyperLink.Attributes.Add("onclick", clickHandler);
}
}
}
NC...