Solved

Call javascript function in Code Behind asp.net 3.5

Posted on 2011-02-28
10
556 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

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

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

14 Experts available now in Live!

Get 1:1 Help Now