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
Solved

javascript and vb.net (button click)

Posted on 2011-09-17
6
309 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
ID: 36554682
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
ID: 36554686
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
ID: 36555901
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:runnerjp2005
ID: 36556633
leakim971: i dont understand the 2nd post?
0
 

Author Comment

by:runnerjp2005
ID: 36556642
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
ID: 36556735
>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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

837 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