Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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

19 Experts available now in Live!

Get 1:1 Help Now