Solved

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

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now