Solved

Call javascript function in Code Behind asp.net 3.5

Posted on 2011-02-28
10
565 Views
Last Modified: 2012-08-13
I been trying to call a javascript function to validate if a checkbox at least was selected on a asp.net form, problem is that by calling directly on the printbutton, it throws and error, (print button is inside a item template of a listview control)...   So this is the current workaround I am trying but I cannot get to call the script.....please help!


Protected Sub ListView1_ItemDataBound(ByVal sender As Object, ByVal e As ListViewItemEventArgs)
        If e.Item.ItemType = ListViewItemType.DataItem Then
            Dim myButtonPrintOut1 As Button = CType(e.Item.FindControl("printButton1"), Button)
            myButtonPrintOut1.Attributes.Add("onClientClick", "checkboxChecked()")
            myButtonPrintOut1.OnClientClick = "javascript:return confirm('You wish to do this?'"
            myButtonPrintOut1.OnClientClick = "checkboxChecked()"
        End If
    End Sub

Open in new window

0
Comment
Question by:TonyReba
  • 4
  • 3
  • 3
10 Comments
 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 500 total points
ID: 34997397
Try this code
Protected Sub ListView1_ItemDataBound(ByVal sender As Object, ByVal e As ListViewItemEventArgs)
        If e.Item.ItemType = ListViewItemType.DataItem Then
            Dim myButtonPrintOut1 As Button = CType(e.Item.FindControl("printButton1"), Button)
            myButtonPrintOut1.Attributes.Add("onClientClick", "checkboxChecked()")
            myButtonPrintOut1.OnClientClick = "javascript:return confirm('You wish to do this?');"
            myButtonPrintOut1.OnClientClick = "checkboxChecked()"
        End If
    End Sub

Open in new window

0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34997401
Changed
myButtonPrintOut1.OnClientClick = "javascript:return confirm('You wish to do this?'"

Open in new window

to
myButtonPrintOut1.OnClientClick = "javascript:return confirm('You wish to do this?');"

Open in new window


Raj
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34997409
Ignore my first post - http:#34997397

Raj
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 5

Expert Comment

by:sindhuxyz
ID: 34997417
Easy way

DropDownList1.Attributes.Add("onchange","CallBack();");
0
 
LVL 5

Expert Comment

by:sindhuxyz
ID: 34997443
Method 2

JavaScript

<head runat="server">
    <title>Call JavaScript From CodeBehind</title>
    <script type="text/javascript">
        function alertMe() {
            alert('Hello');
        }
    </script>
</head>



C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!ClientScript.IsStartupScriptRegistered("alert"))
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(),
            "alert", "alertMe();", true);
    }
}
0
 
LVL 9

Author Comment

by:TonyReba
ID: 34997509
sindhuxyz:


This has to be done on the Button Click event , not at the page load,  I tried using registerstartupscripts, and function was called, but, at load event... so it needs to be on button click or itemDataBound Events?

does it makes sense
0
 
LVL 9

Author Comment

by:TonyReba
ID: 34997519
this is more of the code behind code I have
' First we create the function for Listview1 

    Protected Sub ListView1_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ListView1.ItemCommand

        Dim myButtonPrint1 As Button = CType(ListView1.FindControl("printButton1"), Button)


        If e.CommandSource Is myButtonPrint1 Then


            Dim recordsId As New List(Of String)
            For Each lvi1 As ListViewItem In ListView1.Items
                Dim chb1 As CheckBox = IIf(lvi1.FindControl("CheckBoxPrintProvider1") Is Nothing, IIf(lvi1.FindControl("CheckBoxPrintProvider3") Is Nothing, lvi1.FindControl("CheckBoxPrintProvider4"), lvi1.FindControl("CheckBoxPrintProvider3")), lvi1.FindControl("CheckBoxPrintProvider1"))

                If chb1.Checked = True Then
                    Dim param1 As String
                    param1 = DirectCast(lvi1.FindControl("lblId1"), Label).Text
                    ' This is the list you will save in Session object
                    'param1 = CType(e.Item.DataItem, System.Data.DataRowView)("ID").ToString()
                    recordsId.Add(param1)

                End If
            Next

            ' Store in session to be pulled out in the Printable Page
            Session("Records") = recordsId
            Response.Redirect("PrintableProviderList.aspx")
        End If
    End Sub


    Protected Sub ListView2_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ListView2.ItemCommand

        Dim myButtonPrint2 As Button = CType(ListView2.FindControl("printButton2"), Button)


        If e.CommandSource Is myButtonPrint2 Then
            Dim recordsId As New List(Of String)
            For Each lvi2 As ListViewItem In ListView2.Items
                Dim chb2 As CheckBox = IIf(lvi2.FindControl("CheckBoxPrintProvider2") Is Nothing, IIf(lvi2.FindControl("CheckBoxPrintProvider3") Is Nothing, lvi2.FindControl("CheckBoxPrintProvider4"), lvi2.FindControl("CheckBoxPrintProvider3")), lvi2.FindControl("CheckBoxPrintProvider2"))

                If chb2.Checked = True Then
                    Dim param1 As String
                    param1 = DirectCast(lvi2.FindControl("lblId2"), Label).Text
                    ' This is the list you will save in Session object
                    'param1 = CType(e.Item.DataItem, System.Data.DataRowView)("ID").ToString()
                    recordsId.Add(param1)

                End If
            Next

            ' Store in session to be pulled out in the Printable Page
            Session("Records") = recordsId
            Response.Redirect("PrintableProviderList.aspx")
        End If
    End Sub

    Protected Sub ListView1_ItemDataBound(ByVal sender As Object, ByVal e As ListViewItemEventArgs)
        If e.Item.ItemType = ListViewItemType.DataItem Then
            Dim myButtonPrintOut1 As Button = CType(e.Item.FindControl("printButton1"), Button)
            myButtonPrintOut1.Attributes.Add("onClientClick", "checkboxChecked()")
            myButtonPrintOut1.OnClientClick = "javascript:return confirm('You wish to do this?');"

            myButtonPrintOut1.OnClientClick = "checkboxChecked()"
        End If
    End Sub


 

    Protected Sub ListView2_ItemDataBound(ByVal sender As Object, ByVal e As ListViewItemEventArgs)
        If e.Item.ItemType = ListViewItemType.DataItem Then
            Dim myButtonPrintOut2 As Button = CType(e.Item.FindControl("printButton2"), Button)
            myButtonPrintOut2.Attributes.Add("onClientClick", "checkboxChecked()")

        End If
    End Sub

Open in new window

0
 
LVL 5

Expert Comment

by:sindhuxyz
ID: 34997640
For button, you can use:

Button1.Attributes.Add("onclick","CallBack();");
0
 
LVL 9

Author Comment

by:TonyReba
ID: 34997798
what does CallBack does?
0
 
LVL 9

Author Comment

by:TonyReba
ID: 34997801
Dont I need to tell the name of the javascript? , in which event fo I set this?
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

16 Experts available now in Live!

Get 1:1 Help Now