Solved

javascript and vb.net (button click)

Posted on 2011-09-17
6
311 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
[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
  • 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
Industry Leaders: 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!

 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

688 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