Add attribute to GridView to call javascript function

Hello,
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(CommentID);
Response.Write("'),'','center: yes; resizable: no; help: no; status: no; dialogWidth: 750px; dialogHeight: 675px;')");
Response.Write("</script>");

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:
Columns:
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!
Sarah

 
sarahjackelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Elvio Lujan.Net Senior DeveloperCommented:
in the rowdatacommand event:

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Elvio Lujan.Net Senior DeveloperCommented:
sorry rowdataBound :)
0
sarahjackelAuthor Commented:
ooo...that's good.  Thanks for the quick responce!  I'll try it out.
0
sarahjackelAuthor Commented:
Worked!

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();
            BaseGridView.Rows[i].Cells[0].Attributes.Add("OnClick","Javascript:LoadModalDialog('"
+ dataKey + "','ViewComment.aspx?ID=" + dataKey + "', '700', '600', 'viewcomment')");
        }

Thanks you, lem2802
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.