?
Solved

Call javascript function in Code Behind asp.net 3.5

Posted on 2011-02-28
10
Medium Priority
?
570 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
[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
  • 4
  • 3
  • 3
10 Comments
 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 2000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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/…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

741 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