?
Solved

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

Posted on 2007-12-03
3
Medium Priority
?
5,285 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 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

800 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