I am trying to use my if else statement inside my Function Save() but I cannot make it works? After click on the img button I want to verify if mycheckBox_A is checked. By the way I am using classic ASP
<img src="blah" onclick="<%Save()%>"/>
<%
Function Save()
If mycheckBox_A.checked Then
'mycheckBox_A is checked
Else
'mycheckBox_B is checked
End If
End Function
%>
The onclick attribute is not for calling server-side functions. You can assign JavaScript to this one, but not VB function. Think if you really need server-side code here that's possible that all you need in this exact place could be done with JavaScript.
EDIT:
If you need to do something on the server, you have to use AJAX to send only data you want to the server or submit the form with submit button or JavaScript call to submit() method. After submitting date you have to process it in your server-side function. Check this short tutorial on processing submitted with both POST and GET methods.
According to how to call a ASP function in the onclick event:
ASP functions execute only on the server side, while the onclick event
executes only on the client side. You are trying to mix the two in an
impossible way.
The problem is that the function calculate() does not exist on the
client-side. The browser cannot see it.
You will need to use JavaScript (or another client-side technology) if
you want to have this type of functionality.
This also applies to your Save() function.
You can't use onclick like this. Your only option is to have the button be either a submit button, or a link, then handle it on postback (if you need to do it server-side) or to write it in javascript if it can be done client-side.
Using postback to handle server logic is quite simple and basically how it works in modern ASP. You could have the submit button be in a form which has an integer, showing what you want to do, then handle this at the top of the page. That way, you can postback to the same page, handling different kinds of logic depending on what button was pressed, etc.
Should be noted that this makes for pretty ugly code, but then again, it's classic ASP.NET, it will look ugly no matter what.
No need to use any client script here at all
<%
'Have you POST to this page?
If Len(Request.Form) > 0 Then Call Save()
Sub Save()
Dim a_check, i_item, i_items
a_check = Split(Request.Form("checkbox") & "", ",")
If IsArray(a_check) Then
i_items = UBound(a_check, 1)
For i_item = 0 To i_items
'Could store this and output it later on but is just a quick example
Response.Write a_check(i_item) & " is checked"
Next
End If
End Sub
%>
<html>
<head>
</head>
<body>
<!-- make the page POST to itself -->
<form method="POST" action="">
<! -- your other fields etc
<input type="checkbox" name="checkbox" value="A" />
<input type="checkbox" name="checkbox" value="B" />
<input type="submit" value="Save" />
</form>
</body>
</html>
Quick example nothing special untested.
You can use JQuery post method and pass your variables to classic asp page.
Related
In my project I want to run 2 forms in one webpage which is master page one is for contact us and one is for signup user. but multiple server side forms are not allowed. is there any solution for fix it?
You can use postback ability of Asp.net Webforms.
You should create one button for singup and another one for contact. You can catch event in server side and you do what want to do.
You can create regular client side forms created using HTML in the master page, and use the Request.Form to read the posted data. For this to work, all the input elements must have a name, and you can read them using Request.Form["ElementName"].
Take into account that forms cannot be nested.
So, your master page needs to llok like this
...
<body>
<form id="form1" runat="server">
</form>
<form id="contact" action="Contact.aspx" method="post">
<input type="text" name="Message"/>
<input type="submit" value="Contact us!"/>
</form>
</body>
The first <form> is the regular ASP.NET server side form, which supports server side components
The second <form> is a custom HTML form. It doesn't have the runat="server" attribute, and it's outside the server side <form>.
The second form action points to a .aspx page. In its page load event you can use the Request.Form["Name"] to acces the name of the contact form. Note that you also need to include a submit button to send the data to the server, and a method="post" to specify the method to send the page.
This is resorting to "basic" HTML controls, so there is no View State, and the posted values will be lost if they're not reset in the server. I.e. if the form is posted, and there's an error, the previously posted values will be lost when rendering the page again.
If you want to make it more sophisticated you can use Javascript and, optionally AJAX. But that's a more complex solution. See this for an example.
ASP.NET Forms only supports one form. You could use an iFrame and render your second page (second form) inside of the iFrame.
Here is an interesting post about placing some page content after the tag. This may be helpful to you.
http://forums.asp.net/t/1611822.aspx?Dynamically+adding+content+to+page+outside+of+the+aspnet+form+from+within+a+UC
I have the following code in my aspx file:
<button type="button" id="btnAskQuestion" runat="server" onserverclick="btnAskQuestion_Click">Ask Question</button>
I've tried every combination of onclick="return false;" and onclick="preventDefault()" I can think of, including putting them in the javascript function that gets called. Everything I try has one of two results: either I get a postback, or the server side code (btnAskQuestion_Click) never executes.
Any idea what I might be doing wrong?
You cannot execute server-side code this way, using onserverclick causes postback.
If you wish to prevent full page refresh and still execute server-side code, you have to call a client-side JS function via onclick and execute an AJAX call from there.
Another alternative is to use your button as a trigger for UpdatePanel - this way only partial postback will be performed.
Try using the property UseSubmitBehavior="false" in the button markup.
or you can use a "trick" :
Markup
<button onclick="myFunction()">Click Me!</button>
<div style="display:none">
<asp:Button runat="server" id="btnButton" .../>
</div>
js
function myFunction()
{
if (true statement)
$("[id$=btnButton]").click();
else
alert("false");
}
What this does is that you handle stuff with normal markup and do the logic using js. And you can trigger a click of the button that do something in the server.
There're OnClick, that fires on server and OnClientClick that fires on client browser. You should do this:
<asp:Button ID="btnAskQuestion" runat="server"
OnClick="btnAskQuestion_Click"
OnClientClick="return myfunction();">Ask Question</asp:button>
If myFunction returns true, then you will have a postback to the server.
My answer is appropriate only for ASP:Button, not the button control you are working with. Given the choice, I'd switch to ASP:Button.
You're looking for OnClientClick. If you put your JavaScript code there, it will kill the PostBack before it can hit the server.
On the other hand, if you're looking to execute server code without a PostBack, that's impossible. The PostBack is what triggers the server to act.
I was exploring the search box on the Apple website and noticed it doesn't have a input type="submit" to post the form, even when Javascript is disabled.
<form action="/search/" method="post" class="search" id="g-search">
<div class="sp-label">
<label for="sp-searchtext">Search</label>
<input type="text" name="q" id="sp-searchtext" accesskey="s">
</div>
</form>
Having never really explored it, I take it from this it means you can post a form without needing a submit button, it just relies on the user pressing the return key.
Two questions: 1) Is this compatible across all browsers? So in IE 7 will pressing return still work?; 2) Is there a way to do this in ASP.NET without using an asp:button? I will probably have it inside a placeholder (where I would conventionally use defaultButton to allow for multiple forms on the page) but if I can get rid of the button altogether then that's a plus.
yes of course it is possible to do it in anyway you want.
The simpler thing is to have an onclick event that calls a function that does the submit like this:
JQuery:
$('#id_of_form').submit()
javascript:
document.name_of_my_form.submit();
or
document.getElementById('id_of_my_form').submit();
so simple :)
on the page products.aspx i created a form:
<form id="send_info_form" method="post" action="send_email.aspx">
<input type="text" name="the_name />
<input type="submit" />
</form>
when i click on submit it's forwarded to the same page (products.aspx) and not to the page i set in action attribute of the form.
It looks like you have a misunderstanding about how ASP.NET's logic works- ASP.NET has a much different paradigm than PHP or ASP does.
It seems like you're taking more of an ASP classic or PHP approach of directly handling the landing pages of forms and POST values, which you no longer have to do. You shouldn't need a separate page to handle the logic of the form submission either; this is all handled by event handlers in the submitting page's codebehind.
Instead of handling input elements directly, you should use ASP.NET's server controls to handle all the inputs for you.
What you should be doing is:
In the Products.aspx page:
E-mail Address: <asp:TextBox runat="server" ID="txtEmail" />
<asp:Button runat="server" ID="btnSubmit" OnClick="btnSubmit_Click" Text="Submit" />
Note that there's no form tag required (besides the one already provided to you when you first make the ASPX page.
Since you're working with an object-oriented language with a business objects representing all of your HTML elements with ASP.NET, you don't have to handle reading from the POST values of the form directly.
In the codebehind for Products.aspx (I'm assuming C#, so Products.aspx.cs), add a method for btnSubmit_Click:
protected void btnSubmit_Click(object sender, EventArgs e) {
string sendEmailTo = txtEmail.Text;
// insert mail sending logic here
}
In ASP.NET, the tag will by default always post itself to the same page. This allows you to handle any events in the codebehind in the ASPX page. It's not clear from your question what exactly you're trying to do. I can think of three possible scenarios:
1) You want to post back to the same page, and toggle visibility of UI elements (other panels, etc.) based on the result of the processing, or redirect the user to a second destination page once processing is complete. This is the most common scenario and the approach I recommend be taken, because it keeps all the logic regarding the processing of the form in one place.
2) You can specify a PostBackUrl to specify another (ASP.NET) page for button controls and whatnot. From there you can do the processing from elements on the first page on the second page using the PreviousPage property. See http://msdn.microsoft.com/en-us/library/ms178139.aspx for more information.
3) If you have a separate page you wish to post to that you don't control that's not ASP.NET-based (i.e., another site's form, or a PHP/ASP3.0 site you run), things get significantly more difficult. ASP.NET puts everything in one giant elements. Since tags cannot reliably be embedded within each other in HTML, you will either have to do a silent POST manually from your codebehind, or use Javascript to quietly submit an ajax request upon submission.
I have an issue that's been bugging me this morning. I'm building an ASP.NET webforms app that has many input forms and I'm trying to standardise how I manage validation. I would like to use the built-in validators (RequiredFieldValidator, Regex etc). My html requirements are:
Before validation:
<div class="formLine">
<label for="fieldID">Form Label</label>
<input type="text" id="fieldID" />
</div>
After validation (in case of error):
<div class="formLine formError">
<label for="fieldID">Form Label</label>
<input type="text" id="fieldID" />
<span class="errorMessage">Please enter some value</span>
</div>
The additional span is fine, this is achieved with the ASP.NET validation controls. My problem is adding the formError class to the containing <div>.
I'm comfortable using jQuery/DHTML to add this class, but just don't know where to hook it in. Is it possible to monitor DOM changes with jQuery - for example fire an event handler whenever a span is added as a child of <div class="formLine">?
Does anyone else have any good suggestions for dealing with this?
Thanks for reading.
EDIT: After clarification
You could write a custom function that is called by the onClick client side event. Because you are using ASP.Net validation controls you should be able to call the function ValidatorValidate(val) where val is the validator. You could then check the contents of the validators SPAN tag for your validation error and update your div tag accordingly.
Note: I'm no expert on client side validation but this should work in principal.
Further info on MSDN.
You need to declare your div as runat="server" with an id tag so you can reference it in server side code then in page load do a test for Page.IsValid which will return false if one of your validators fails to vaildate.
You can then set the class property on your div programatically. Something like:
<div class="formLine" id="divForm" runat="server">
<label for="fieldID">Form Label</label>
<input type="text" id="fieldID" />
</div>
Code-Behind (VB)
If Not Page.IsValid Then
divForm.Attributes.Remove("class")
divForm.Attributes.Add("class", "formLine formError")
End If
i'm assuming you're looking for a way to do this on the client side, without postback.
when you use asp.net validation controls on your webform you'll notice that a script reference to WebUIValidation.js is rendered on your page. peeking into that file offers some useful info. there's a global variable in that file named Page_IsValid. whenever validation fails, that variable is set to false.
EDIT: the WebUIValidation.js script reference was prior to .net 2.0, so you can't look at that file anymore. however, the Page_IsValid variable is still there and can be used by hooking into a form submit handler. something like this
// verified that this works
$(document).ready(function() {
$('#form1').bind('submit', function(e) {
if (!Page_IsValid)
$('#yourDiv').addClass('error');
});
});