Can you place an if statement in .ascx layout? - asp.net

I'm new with working with ASP.net and .ascx, and now I've seen a button that calls a method by 'OnClientClick' So the code looks like this:
<asp:Button Text="Save" OnClick="BtnSave_Click" OnClientClick="isBusy();"/>
Now I want that the method only calls at certain definitions declared in the same .ascx file. And thus I thought that an if-statement inside the ascx would work. So I've already tried attempts like OnClientClick="if(Text.Length <= maxlength) { isBusy(); } but that caused the line to not respond at all.
Currently I'm wondering if an if-statement in this situation is actually possible.

it is doable. but you need to make sure your js is correct.
in your question, what is Text.Lengh ?
whatever, if you want block the server side postback, then return false in your onclick JS, that will completely mute the postback event
for example
<asp:button runat="server" onclientclick="return false;" />
this button will never post back

Related

Prevent page refresh in ASP.NET

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 want iif conditiomn in my radio button

I want to put if condition in my radio button.
I have wrote this code but it does not give me result as I want. you will get idea from my code what actual I need to do. I have loaded user control two time in a single page so I want to call Java Script in page according to control.
<asp:RadioButton runat="server" GroupName="Pricing" class="2deditable iscreatedbydealer isinprivatelabel" onClick='<%this.ID=="ucPricing_Details_Sale"? "setSalePopupRetailPrice();":"setClearancePopupRetailPrice();"%>'
ID="rbManual" />
I think you can dynamic add the onclick event for the control in page load event of server side.
e.g
if("ucPricing_Details_Sale".Equals(this.ID))
{
this.rbManual.Attributes.Add("onclick", "setSalePopupRetailPrice();");
}
else
{
this.rbManual.Attributes.Add("onclick", "setClearancePopupRetailPrice();");
}
The on OnClick event will run on server side. Try using the OnClientClick event instead.
Edit: Deleted my last statement. Mistook me about the context of the call.
Also added code sample:
<asp:RadioButton runat="server"
OnClientClick='<%this.ID=="ucPricing_Details_Sale"?[...]"%>'
ID="rbManual" />

Button not processing onClick method

I have a button on an ascx control that calls a method on the onClick event:
<asp:Button id="bUpdateText" onClick="FUpdate" ValidationGroup="Update" CausesValidation="False" Text="Update" cssclass="button" runat="server" />
Normally I use this control on it's own page and the button works. This time round however, I am loading this control into a Div that is present on the home page of my site (that way I can show the contents with a little bit of JQuery). However, when I bring the control in this way, the onClick event doesn't fire and I am not sure what could cause that.
Sorry I don't have any code sample but the nature of the site makes it difficult to provide any that would make sense.
In short, what would stop this event firing now?
p.s I have tried adding validation groups to all other buttons and validation controls on the page and there is only ONE form present on the page.
EDIT: I have only just added the validation stuff in to see if that does anything. By default it has been like this and still didn't work:
<asp:Button id="bUpdateText" onClick="FUpdate" Text="Update" cssclass="button" runat="server" />
As mentioned as well, this works when I use this control on it's own page (loaded directly into Default.aspx) so I don't think the case of onClick matters.
EDIT2: I have just noticed that when I click this button, other validation controls on my page are being triggered even though they have their own DIFFERENT validation group?! Taking these controls out doesn't help though.
Thanks.
I have found out what is causing the issue.
This control that I am now including is called on the Page_Finalize() and I am guessing that by this point the viewstate has forgotten it needs to do anything. Loading this control on the page load sorts it out.
Thanks for looking.
To start, if you set the 'causesValidation' property to false, you do not need a validation group.
Additionally, I believe that ASP cares about case when dealing with the OnClick command.
i.e. it should be OnClick not onClick
Yeah, annoying and small, but that might be your problem
You can use Firebug to see what happen in Update validationGroup. it looks like your page execute only client-side button click because of Update validationGroup.

Using jQuery with ASP.NET web forms on postback

I've read plenty of good blog posts on integrating jQuery with ASP.NET web forms to utilize the data access capabilities, but I'm curious to know how easy it would be to use some of jQuery's animation capabilities before a postback. I can think of two good examples:
1) When I click on a asp:button, animate a div, but then do a regular postback when the animation is finished (or complete the animation after the postback?).
2) When I click on a linkbutton in a gridview, fade out the gridview row, then do the regular postback in the linkbutton's onclick event.
I'm not aware of a solution to this. Everything I've tried has either run the postback before the animation even starts, or runs the animation but then you need to execute the postback in JavaScript (yuck).
Yes, of course you can do that.
Say for example we take a button to do some postback,
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
OnClick Event - cause the postback to be processed on the server side.
But if you also define a OnClientClick event and call any JavaScript function with a return value to the call. It will decide the post back after the JavaScript function is executed.
For example,
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" OnClientClick="return DoJqueryAnimations();" />
In above example - DoJQueryAnimations() is a JavaScript function. We call it with a "return:".
So when the function is called and decides what to return, the postback will wait for the client function to give the value - true(do postback) / false (no post back).
In the JavaScript function you do the animation and return true.
For example:
function DoJqueryAnimations()
{
//Do some animations here or call other JavaScript function.
return true;
}

asp.net button use javascript return function

I have an asp:button control and am using the "CommandName" and "CommandArgument" parameters in the .aspx file. The CommandName is found with <%# Eval("Name") %>, but the CommandArgument has to be found via a javascript function. However, I'm not quite sure how to do this inline. The function returns a number (integer), which is to be used as the value of the CommandArgument. Is it possible to do this?
Thanks
EDITED TO ADD CODE
I've added an example code (don't have access to the real code atm). However, basically, the CommandArgument should be the value returned by the function CalculateLength().
function CalculateLength(a,b) {
return a*b;
}
<asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName=<%# Eval("Name") %> CommandArgument= ??? //Should be result of CalculateLength(a,b).
I am not sure how you are going to get the javascript to work because the aspx code is running server side and building the output for your button. By the time the javascript runs the page code has already been built and the button's html and attached javascript as well.
Is there anyway to calculate the function server side and then just do:
CommandArgument="<%= CalculateLengthServerSide() %>"
You don't need to just use the data your binding to, you can call any server side function.
Edit: Try switching your label that stores the quantity to a textbox and making it read only so uses will not mess with it. After the button is clicked, you should be able to find the textbox control and read out the posted value.

Resources