asp.net button use javascript return function - asp.net

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.

Related

Can you place an if statement in .ascx layout?

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

Data binding simple variable not working

This is the page code for the control:
<asp:TextBox id="someID" maxlength="10" columns="10" runat="server" Text="<%# work %>" />
This is how I set up and populate the variable before page is rendered, it is in the code behind, I have tried I think all the variations available for the variable declaration, e.g. public, shared, protected, etc.:
Public work As String
work = "987654321"
The textbox always comes up blank. referred to this ms kb page for how this work and it has a specific example.
However, it didn't explain anywhere if there is some special way of declaring the variables used in the binding or some special way to set the value of the variable, or is there something needed to allow the <%# syntax to work?
Binding doesn't happen automatically. You have to call DataBind() either from Page control or the text control. Try put it in Page_Load() method.

ASP.NET tags don't expand in OnClientClick

I have the following two buttons:
<asp:Button ID="btnVote" runat="server" Text="Vote!" PostBackUrl="<%$RouteUrl:id=2, routename=Results%>"/><br />
<asp:Button ID="btnResults" runat="server" Text="Results ->" OnClientClick="location.href='<%$RouteUrl:id=2, routename=Results%>'"/>
The first <%$ %> expands as intended, while the second (identical!) one gets used as typed ( = not expanded). I am very new to ASP.NET, coming from PHP, and this is from my learning/test site.
What am I doing wrong and how can I fix it?
The ASP.NET expression syntax (<%$ ... %>) can only be used to directly assign values to properties of server controls. I think the problem you're having is that the expression syntax is embedded within a string, and not directly bound to the "OnClientClick" attribute.
Trying changing your second button to
<asp:Button ID="btnResults" runat="server" Text="Results ->" OnClientClick="<%$RouteUrl:id=2, routename=Results%>" />
If that works, you may need to modify your expression to return that extra text you need. Or, create another route that returns said info (the current value wrapped in the "location.href" attribute).
You can find more information about these expressions here: ASP.NET Expressions Overview.

how to assign a radwindow property value from a public variable in page code?

I have a radwindow and I'm trying to do this
<telerik:RadWindow ID="winQuote" runat="server" NavigateUrl="popupQuote.aspx?serie=<%= strSerie %>" Behaviors="Resize, Close, Maximize, Move">
When I get the value of the property serie using codebehind or javascript, it returns: serie=<%= strSerie %>.
It is not parsing the <%= serie%> tag, it is using it as if it were text, any ideas my friends?
Thanks
Just set it in the code-behind.
If you need such functionality on the client use JavaScript - see the RadWinddow client-side API: http://www.telerik.com/help/aspnet-ajax/window-programming-radwindow-methods.html and the setUrl() method that can be called after showing it.

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" />

Resources