Add attribute to GridView to call javascript function

Posted on 2006-04-17
Last Modified: 2012-06-27
I am trying to launch a modal dialog from a grid view.  I have it working now by having a "View" command column and calling the GridView_RowCommand method on click.  From here I use the following code to launch a modal dialog:

Response.Write("<script>"); Response.Write("window.showModalDialog(encodeURI('ViewResourceComment.aspx?ID=");
Response.Write("'),'','center: yes; resizable: no; help: no; status: no; dialogWidth: 750px; dialogHeight: 675px;')");

The problem I am having with this is that the click in the grid causes a post back to the server.  With other modal dialogs I have (open from links) I add an attribute to the link on the page load so that is automatically calls javascript code and not C#.

this.lnkNewComment1.Attributes.Add("OnClick", "Javascript:LoadModalDialog('"
            + dataToSend + "','EditComment.aspx', '710', '700', 'comment')");

Is there a way to do this with a gridView?  I'm not sure how to determine the row that was selected.  I will also need to access the data key names attribute to get the ID of the item.

Overview of my grid:
View -  Link- loads a modal dialog
delete -  Link - redirects to a confirm screen
Archive -  Link - redirects to a confirm screen
comment - just text
user - just text.

Thanks in advance!

Question by:sarahjackel
    LVL 17

    Accepted Solution

    in the rowdatacommand event:

    e.Row.Cells(cellIndex where is your link).Attributes.Add("onclick", "window.showModalDialog...")
    LVL 17

    Expert Comment

    sorry rowdataBound :)

    Author Comment

    ooo...that's good.  Thanks for the quick responce!  I'll try it out.

    Author Comment


    I had to change it a bit.  Here is what I wrote:

     string dataKey = "";
            for (int i = 0; i < BaseGridView.Rows.Count; i++)
                dataKey = BaseGridView.DataKeys[i].Values[0].ToString();
    + dataKey + "','ViewComment.aspx?ID=" + dataKey + "', '700', '600', 'viewcomment')");

    Thanks you, lem2802

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now