Solved

javascript and vb.net (button click)

Posted on 2011-09-17
6
310 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
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

730 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