problem with onclientclick and postbackurl - asp.net

I want to trigger on click of a button this:
<asp:Button runat="server" ID="submit" Text="Submit" OnClientClick="country(this.form);" PostBackUrl="http://www.google.com/" />
In javascript I m checking validation of the form.The problem is when I m clicking on the button it is not waiting for the validation but its postback to google.com...
If I do return country(this.form) then on button click it waits for validation but don't postback after I fill the form. I want something like that if javascript validation is false..then OnClientClick should be return country(this.form) if its true then only
country(this.form)

You can do this:
<asp:Button runat="server" ID="submit" Text="Submit"
OnClientClick="if(!country(this.form)) return false;"
PostBackUrl="http://www.google.com/" />
Since postbacks use a "onclick" overall, your script is perpending to the ASP.Net script, returning means none of that postback script runs. If you use an if and only return out if needed, it'll work.
It makes more sense when you look at the rendered result, something like this:
<input type="submit" name="submit" value="Submit" id="submit" onclick="if(!country(this.form)) return false; WebForm_DoPostBackWithOptions(.....)" />

Write it with return:
<asp:Button runat="server" ID="submit" Text="Submit" OnClientClick="return country(this.form);" PostBackUrl="http://www.google.com/" />
And your country function will end with return true or false,
function country(form) {
// Validations goes here
return true;// or return false;
}

Related

asp OnClientClick not executing

This button:
<asp:Button ID="btnUpload" runat="server" Height="26px" Text="Upload" Width="86px" OnClick="btnUpload_Click" />
With added attribute in Page_Load:
btnUpload.Attributes.Add("OnClientClick", "javascript:alert('Test');")
Shows in browser Inspector:
<input type="submit" name="ctl00$MainContent$btnUpload" value="Upload" id="btnUpload" class="aspNetDisabled" onclientclick="javascript:alert('Test');" style="height:26px;width:86px;">
but never fires onClientClick. Tried calling function, preceding with 'javascript', many things.... but it never executes what's in onClientClick.
Note: The button's regular OnClick="btnUpload_Click" executes fine.
Any ideas?
This just adds a raw attribute to the resulting HTML:
btnUpload.Attributes.Add("OnClientClick", "javascript:alert('Test');")
And there is no OnClientClick in HTML, so the browser has no reason to make any use of it. That's something the ASP.NET server-side controls use and translate into client-side code. Add it directly to the server-side control instead:
<asp:Button
ID="btnUpload"
runat="server"
Height="26px"
Text="Upload"
Width="86px"
OnClick="btnUpload_Click"
OnClientClick="alert('Test');"
/>
Alternatively (and I don't have a way to test this at the moment), if it's a property on the control then you may be able to add it as such:
btnUpload.OnClientClick = "alert('Test');"
As an aside... If OnClientClick is continuing to cause problems then I'd recommend abandoning it altogether. (I honestly don't know why it even exists, other than as a vestige from a time when ASP.NET was trying to take the web out of web development.) If you just want to attach a click handler in JavaScript, you don't need C#'s permission to do that. Just attach a click handler in JavaScript:
const btn = document.querySelector('#btnUpload');
if (btn) {
btn.addEventListener('click', function () {
alert('Test');
});
}
Adding that to a <script> element at the end of the page would add that function as a click handler to the #btnUpload element.

alert if the user didn't fill the required fields

I have a field in asp and he has RequiredFieldValidator.
I have also a button that on click I send mail to the user.
my code:
<asp:TextBox class="form-control" ID="mail" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="ShemPrati"
ErrorMessage="you must enter your mail"
Display="Dynamic"
ForeColor="Red"> </asp:RequiredFieldValidator>
<asp:Button ID="Button4" class="btn btn-default" runat="server" Text="send mail" OnClick="Button4_Click" />
When I click on the button it's give me the ErrorMessage (if the field is empty).
I want also to alert a massage that he will be notify that there are a required fields in the form.
How can I do it?
thanks!
You can achieve your requirement by adding a client-side onclick event to the button that submits the form:
<script language="javascript" type="text/javascript">
function checkIfFormIsValid() {
//this will check if the form is valid
if (Page_IsValid == false) {
alert('Your form is not valid!');
}
}
</script>
Then you can call the function when someone clicks your button:
<asp:Button ID="Button4" OnClientClick="JAVASCRIPT:checkIfFormIsValid();" class="btn btn-default" runat="server" Text="send mail" OnClick="Button4_Click" />
BONUS:
You can add an onblur client-side event to a specific text box and do some custom stuff that way:
txtMail.Attributes.Add("onblur","javascript:checkIfFormIsValid();");

modalpopupextender cancel button working as if it is doing postback

I have implemented my owm functionality for the cancel button(I'm using javascript for that) of the ModalPopUpExtender instead of writing "cancelcontrolid" property for the ModalPopUpExtender.
It is working fine but there is one issue with that this cancel button is behaving as if it is doing postback.
It is necessary to have this functionality because when i'm writing the "cancelcontrolid" property it is not resetting the ModalPopUpExtender with the default values.
Please help..
Since you're using the button to only execute client side code why are you using <asp:Button you should use simple HTML<input type="button" instead:
<form id="form1" runat="server">
<asp:Button ID="btnShow" runat="server" Text="Show" />
<asp:ToolkitScriptManager ID="scripManager" runat="server" />
<asp:ModalPopupExtender ID="modal" BackgroundCssClass="modalStyle" PopupControlID="popup"
TargetControlID="btnShow" runat="server" BehaviorID="modalBehavior" />
<asp:Panel runat="server" ID="popup" CssClass="panelStyle">
<input type="button" id="btnCancel" onclick="Hide()" value="Cancel" />
</asp:Panel>
</form>
<script type="text/javascript">
function Hide() {
$find("modalBehavior").hide();
}
</script>
Try this......
<asp:button runat="server".... OnClientClick="myfunction(); return false;" />

How to swallow the postback of a asp.net linkbutton control?

I have a page with a form containing an element. I have the click event being handled client side by a Jscript function, however, the page is still reloading whenever I click the LinkButton, can this be avoided?
aspx
<body>
<form>
<asp:LinkButton runat="server" ID="thing" OnClientClick="return SomeFunction()" Text="Some Operation" />
</form>
</body>
JScript
function SomeFunction() {
document.getElementById('someText').innerText = 'SomeMessage';
return false;
}
SomeButton.Attributes.Add("onclick", "javascript:SomeFunction(); return false;");
Update: since your updated code is assigned function in ASPX page, you can use this method -
<asp:LinkButton runat="server" ID="thing"
OnClientClick="javascript:SomeFunction(); return false;"
Text="Some Operation" />
OR
<asp:LinkButton runat="server" ID="thing"
OnClientClick="return SomeFunction()" Text="Some Operation" />
function SomeFunction() {
document.getElementById('someText').innerText = 'SomeMessage';
return false; /**** Required *****/
}
To have the return false; working, you will have to add return before the Method Name.
ex. <asp:LinkButtun ID="IDHere" runat="server" OnClientClick="return SomeFunction()" />

ASP.Net Javascript integration

I am trying to use the following script in asp.net:
<script language="javascript" type="text/javascript">
function checktext() {
var txt = document.getElementById('tbComments');
if (txt.Text.Length > 0) {
alert('Thank you for submitting feedback.');
return true;
}
else {
alert('Sorry, you must enter text before submitting.')
return false;
}
}
</script>
<asp:Button ID="btnSave" runat="server" Text="Submit" onclick="btnSave_Click" OnClientClick="checktext();" />
I have tried using it on the onclick event.. the script will just not work at all.
Any Ideas?
you can use the ClientID property for getting the name of a Control on the client side. I suggest you to try jQuery for all these though
var txt = document.getElementById('<%=tbComments.ClientID%>');
besides, the OnClientClick has to receive a true or false value, in order to "know" whether to send the request to the server; so you have to change it with something like OnClientClick="return checktext();"
Try calling it like this:
<asp:Button
ID="btnSave"
runat="server"
Text="Submit"
onclick="btnSave_Click"
OnClientClick="return checktext();" />
Also this line looks suspicious in a web forms application:
document.getElementById('tbComments');
Make sure that the generated id of your control is not prefixed with something else.
Replace:
<asp:Button ID="btnSave" runat="server" Text="Submit" onclick="btnSave_Click" OnClientClick="checktext();" />
with:
<asp:Button ID="btnSave" runat="server" Text="Submit" onclick="btnSave_Click" OnClientClick="return checktext();" />
Replace:
var txt = document.getElementById('tbComments');
With:
var txt = document.getElementById('<%= tbComments.ClientId %>');
HTH.
everone else mentioned OnClientClick so I won't address that.
assuming tbComments is a textbox of some kind, this line
if (txt.Text.Length > 0) {
is going to fail because Text is not a property of html inputs or textareas, which is how asp.net textboxes are rendered. what you want is
if (txt.value.length > 0) {
also, is there some reason you're not using a regular asp.net RequiredFieldValidator control? you're doing more work than you need to. If you absolutely have to have alert boxes, you can use a CustomValidator control to call your function (you'll have to tweak it to fit the model).

Resources