Solved

Call javascript function in Code Behind asp.net 3.5

Posted on 2011-02-28
10
567 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 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
Independent Software Vendors: 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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/…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 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