Solved

How to Call javascript function from field in asp:GridView?

Posted on 2007-12-03
3
5,273 Views
Last Modified: 2013-11-07
I'm trying to call the attached javascript function from a column inside an asp:gridview.
I tried every combination of tag that I could find :boundfield, templatefield, itemtemplate, hyperlink, hyperlinkfield, etc.
But, Im still getting an error in the compile.
I would like to display the field from the database along with a column header and pass the other four values from that row to a javascript function defined on the form.

Heres the javascript function:
----------------------------
<script language="javascript">
function selectMycontact(n,e,p,i) {
var ifree = 0;
var target = self.opener.document.resform;
try {
target.elements['Gen:General/Client'].value = n;
target.elements['Gen:General/Email'].value =e;
target.elements['Gen:General/Phone #'].value = p;
target.elements['Gen:General/ClientID'].value = i;
} catch (e) { alert(e.message); }}
</script>
------------------------------
And heres the bound field that I am displaying that I would like to attach the following hyperlink field to:
 <asp:BoundField
             DataField="ContactLastName"
             SortExpression="ContactLastName"
             HeaderText="Last Name"
             ItemStyle-Width="200px" />
----------------------------------
<asp:HyperLink
                NavigateUrl="javaScript: selectMycontact('<%# Eval("ContactName") %>', '<%#Eval("email")%>', '<%#Eval("Phone")%>', '<%#Eval("ClientID")%>');">
                        <%#Eval("ContactLastName")%>  />
---------------------------------------
Please Help.
:
---------------------------------------

0
Comment
Question by:jtrapat1
[X]
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
  • 2
3 Comments
 
LVL 16

Expert Comment

by:TSmooth
ID: 20396597
Instead of doing it declaratively, you could use a programmatic approach to give some more flexibility. Replace the hyperlink field with a template field then in the GridView's RowDataBound event, add a literal control to the template column that contains a standard html anchor tag composed of the javascript you want.  Replacing "Cells(1)" with the correct index of your template column, your code could look similar to that below:
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim drv As DataRowView = DirectCast(e.Row.DataItem, DataRowView)
            Dim mylink As New HtmlAnchor
            mylink.HRef = String.Format("JavaScript: selectMycontact('{0}', '{1}', '{2}', '{3}');", drv("ContactName"), drv("email"), drv("Phone"), drv("ClientID").ToString())
            mylink.InnerText = drv("ContactLastName")
 
            e.Row.Cells(1).Controls.Add(mylink)
        End If
    End Sub

Open in new window

0
 

Author Comment

by:jtrapat1
ID: 20397299
Thanks for the response-
If I do this as you suggest, will this affect my column headers as "sortexpression" fields?

Thanks.
John



Thanks
John
0
 
LVL 16

Accepted Solution

by:
TSmooth earned 125 total points
ID: 20397393
You would still want to set your sortexpression attribute to the name of the SqlDataSource column that you want the column to be sorted by when you click on it.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

717 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