Solved

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

Posted on 2007-12-03
3
5,266 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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