Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Javascript confirm in a gridview on a hyperlink field

Posted on 2009-04-12
9
2,166 Views
Last Modified: 2012-05-06
I would like to ask someone, "Are you sure you want to delete this record?", from my gridview.

Right now I am using a hyperlink field so that I could use an image for the delete button.

I have tried a RowDataBound event adding attributes to the desired field, but that is generating a compile error saying, "Compiler Error Message: BC30545: Property access must assign to the property or use its value."
CODE FOR THE GRIDVIEW:
    <asp:GridView ID="GridViewJobListing" runat="server" AutoGenerateColumns="False" 
        BackColor="#000099" BorderStyle="Double" DataSourceID="JobSummary" 
        ForeColor="#FF9933" GridLines="None" HorizontalAlign="Center" 
        Width="600px" DataKeyNames="JobID" BorderColor="#999966" BorderWidth="6px" 
        AllowPaging="True" AllowSorting="True" PagerSettings-PageButtonCount="20" 
            HeaderStyle-Wrap="False" HeaderStyle-HorizontalAlign="Center" PageSize="20">
    <PagerSettings PageButtonCount="20"></PagerSettings>
 
        <RowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
        <Columns>
            <asp:BoundField DataField="JobTitle" HeaderText="Job Title" SortExpression="JobTitle" />
            <asp:BoundField DataField="JobStartDate" HeaderText="Start Date" SortExpression="JobStartDate" />
            <asp:BoundField DataField="DateJobAdded" HeaderText="Date Added" SortExpression="DateJobAdded" DataFormatString = "{0:d}" />
            <asp:BoundField DataField="DateLastModified" HeaderText="Modified" SortExpression="DateLastModified" DataFormatString = "{0:d}" />
            <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
            <asp:HyperLinkField DataNavigateUrlFormatString="~/EmployerEditJobWizard.aspx?JobID={0}" DataNavigateUrlFields="JobID" Text="<img src='graphics/edit.gif' alt='Edit Job' title='Edit Job' border='2' />" /> 
            <asp:HyperLinkField DataNavigateUrlFormatString="~/EmployerJobsEdit.aspx?JobID={0}&Action=Activate" DataNavigateUrlFields="JobID" Text="<img src='graphics/activate.gif' alt='Activate' title='Activate' border='2' />" /> 
            <asp:HyperLinkField DataNavigateUrlFormatString="~/EmployerJobsEdit.aspx?JobID={0}&Action=Deactivate" DataNavigateUrlFields="JobID" Text="<img src='graphics/deactivate.gif' alt='Deactivate' title='Deactivate' border='2' />" /> 
            <asp:HyperLinkField DataNavigateUrlFormatString="~/EmployerJobsEdit.aspx?JobID={0}&Action=Delete" DataNavigateUrlFields="JobID" Text="<img src='graphics/delete.gif' alt='Delete Job' title='Delete Job' border='2' />" /> 
            <asp:BoundField DataField="Expr1" HeaderText="Expr1" SortExpression="Expr1"  Visible="False" />
            <asp:BoundField DataField="JobID" HeaderText="JobID" InsertVisible="False" ReadOnly="True" SortExpression="JobID"  Visible="False" />
            <asp:BoundField DataField="EmpID" HeaderText="EmpID" SortExpression="EmpID"  Visible="False" />
        </Columns>
        <SelectedRowStyle BackColor="#FFCC66" ForeColor="#000066" />
        <HeaderStyle BackColor="#999966" ForeColor="#000099" HorizontalAlign="Center" 
            BorderColor="#CC6600" BorderStyle="Solid" BorderWidth="5px" />
        <AlternatingRowStyle BackColor="#003399" />
    </asp:GridView>
 
 
CODE FOR THE DATABOUND EVENT:
    Protected Sub GridViewJobListing_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewJobListing.RowDataBound
        e.Row.Cells[8].Attributes.Add("onclick", "return confirm('Are you sure you want to delete this record?');")
    End Sub

Open in new window

0
Comment
Question by:spaceneedlejumper
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 14

Assisted Solution

by:GiftsonDJohn
GiftsonDJohn earned 100 total points
ID: 24128409
Hi There,

If you have to just ask generic "Are you sure you want to delete this record?" it is not required to use any server events. Just add OnClientClick attribute on the HyperLink control.

<asp:HyperLinkField DataNavigateUrlFormatString="~/EmployerJobsEdit.aspx?JobID={0}&Action=Delete" DataNavigateUrlFields="JobID" Text="<img src='graphics/delete.gif' alt='Delete Job' title='Delete Job' border='2' />" OnClientClick="return confirm('Are you sure you want to delete this record?');" />
0
 

Author Comment

by:spaceneedlejumper
ID: 24128441
I tried that last night and again just now to be sure.  It generates the error:

Parser Error Message: Type 'System.Web.UI.WebControls.HyperLinkField' does not have a public property named 'onClientClick'.

Can't I do some sort of Gridview.Attributes.Add(OnClientClick) in the PageLoad  -  I know that syntax is wrong, but maybe you know what I'm talking about.
0
 
LVL 6

Assisted Solution

by:havj123
havj123 earned 100 total points
ID: 24128659
It works. Try again

<asp:HyperLink OnClientClick="return confirm('Are you sure?');" ID="DeleteLink" runat="server" NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "ImageId","ViewProperty.aspx?PropertyId="+Request.QueryString("PropertyId")+"&ImageId={0}") %>'  >Delete</asp:HyperLink>
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 6

Expert Comment

by:havj123
ID: 24128663
take hyperlink control instead of HyperLinkField
0
 
LVL 22

Accepted Solution

by:
prairiedog earned 300 total points
ID: 24131413
You are using C# style in VB code. Change it to:
e.Row.Cells(8).Controls(0).Attributes.Add("onclick", "return confirm('Are you sure you want to delete this record?');")
 
 
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 24131668
Wait a second, is it a duplicate question? If yes, then it is against the member agreemnet to post duplicate question.
0
 

Author Comment

by:spaceneedlejumper
ID: 24131807
Sorry prairiedog,

It does look like both of my questions kind of merged into the same thing.  The way the deleting operation works right now is by using a hyperlink field in the gridview that passes a querystring with the job ID and an action=delete.

I'd just like to get a warning in there somewhere.  I would be happy if I could get the warning either on the page showing the gridview when the user clicks delete, or during the pageload event of the page processing the querystring.

0
 
LVL 22

Expert Comment

by:prairiedog
ID: 24131849
Try the comment I post on the other question.
0
 

Author Closing Comment

by:spaceneedlejumper
ID: 31569344
I didn't get this working with any of the solutions offered.  I posted a new question about how to get the JobID into a URL querystring so that I can confirm and then delete based on the incoming querystring.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question