[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2645
  • Last Modified:

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

 
0
sarahjackel
Asked:
sarahjackel
  • 2
  • 2
1 Solution
 
Elvio Lujan.Net Senior DeveloperCommented:
in the rowdatacommand event:

e.Row.Cells(cellIndex where is your link).Attributes.Add("onclick", "window.showModalDialog...")
0
 
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now