Solved

javascript and vb.net (button click)

Posted on 2011-09-17
6
306 Views
Last Modified: 2012-08-13
i curently have a javascript code that will run a script to show contect within a div onclick...
<script type="text/javascript" >
    $(function () {
        $(".submit").click(function () {
            var comment = $("#textbox").val();
            var dataString ='&comment=' + comment;
            if (comment == '') {
                alert('Please Give Valid Details');
            }
            else {
                $("#flash").show();
                $("#flash").fadeIn(400).html('<img src="ajax-loader.gif" />Loading Comment...');
                $.ajax({
                    type: "POST",
                    url: "Default2.aspx",
                    data: dataString,
                    cache: false,
                    success: function (html) {
                        $("div#update").append(html);
                        $("div#update li:last").fadeIn("slow");
                        $("#flash").hide();
                    }
                });
            } return false;
        });
    });

    </script>

Open in new window


is there away i can chnage this javascript so i can just call it via  submit.Attributes.Add("onClick", ***run the code somehow here***")
0
Comment
Question by:runnerjp2005
  • 3
  • 2
6 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
submit.Attributes.Add("onClick", "trig1()")

with :



<script type="text/javascript" >
	$(function () {
		$(".submit").click(trig1);
	});
	function trig1() {
		var comment = $("#textbox").val();
		var dataString ='&comment=' + comment;
		if (comment == '') {
			alert('Please Give Valid Details');
		}
		else {
			$("#flash").show();
			$("#flash").fadeIn(400).html('<img src="ajax-loader.gif" />Loading Comment...');
			$.ajax({
				type: "POST",
				url: "Default2.aspx",
				data: dataString,
				cache: false,
				success: function (html) {
					$("div#update").append(html);
					$("div#update li:last").fadeIn("slow");
					$("#flash").hide();
				}
			});
		}
	}
</script>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
but I think you want this (remove/without submit.Attributes.Add("onClick", ***run the code somehow here***")) :
<script type="text/javascript" >
    $(function () {
        $(".submit,#submit").click(function () {
            var comment = $("#textbox").val();
            var dataString ='&comment=' + comment;
            if (comment == '') {
                alert('Please Give Valid Details');
            }
            else {
                $("#flash").show();
                $("#flash").fadeIn(400).html('<img src="ajax-loader.gif" />Loading Comment...');
                $.ajax({
async:false,
                    type: "POST",
                    url: "Default2.aspx",
                    data: dataString,
                    cache: false,
                    success: function (html) {
                        $("div#update").append(html);
                        $("div#update li:last").fadeIn("slow");
                        $("#flash").hide();
                    }
                });
            } return false;
        });
    });

    </script>

Open in new window

0
 
LVL 7

Expert Comment

by:Kishan Zunjare
Comment Utility
Create Javascript function of you code and provide following in asp.net code behind;

On the page load event
Button1.Attributes.Add("onclick","JS FunctionName()");

Hope this will work.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:runnerjp2005
Comment Utility
leakim971: i dont understand the 2nd post?
0
 

Author Comment

by:runnerjp2005
Comment Utility
my main issue is the javascript means i cant run
 Protected Sub Submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Submit.Click
        Label1.Text = "hello"
    End Sub

it does the javascript part and misses the onlick ...yet i need both!
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
>leakim971: i dont understand the 2nd post?

we use a synchronous ajax call (line 13) : http://arshadinfo.wordpress.com/2008/05/25/what-is-the-difference-between-asynchronous-ajax-and-synchronous-request/

>my main issue is the javascript means i cant run
>Protected Sub Submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Submit.Click
>        Label1.Text = "hello"
>    End Sub
>it does the javascript part and misses the onlick ...yet i need both!

Yes, that's why I propose the second code. First we need to wait end of the ajax call
If you want to run your server side code ( Label1.Text = "hello" ) you must remove the return false;

Note you don't need : submit.Attributes.Add("onClick", "trig1()")
and $(".submit").click(trig1); // if .submit is the class of your button
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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 …
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

17 Experts available now in Live!

Get 1:1 Help Now