On page load RadButton Onclientclicked triggered - c#

When I use telerik radbutton
<telerik:RadButton ID="RadButton1" runat="server" Text="Stackoverflow"
Skin="Default" OnClientClicked="SaveIt()" CommandArgument="Edit">
</telerik:RadButton>
after refreshing page, always SaveIt() function starts to work.
But;
when I use button, it works only when user click it.
<button type="button" onclick="SaveIt()">Stackoverflow</button>
What is the difference between them?

Couple of things here. The difference between a RadButton and a normal HTML button is as follows:
When a RadButton is used, the client side HTML generated is as follows:
<a id="btnStandard" class="RadButton RadButton_Office2010Silver rbSkinnedButton"
href="javascript:void(0)">
<input class="rbDecorated" type="submit" name="btnStandard_input"
id="btnStandard_input" value="Standard Button" />
<input id="btnStandard_ClientState" name="btnStandard_ClientState"
type="hidden" />
</a>
As you can see, the type generated is of "Submit" - which means it will submit the form to the server. So if you refresh it will try to resend the form again.
what i am failing to understand is - have you used ajax panel. Because if you have used ajax panel, the button click would trigger an ajax posting to the server and when you refresh the page its as if its a first time and not a postback.
Where as the normal html is not meant for form submits. Its just used to trigger a click event on the client side.
Having said that - the signature for OnClientChecked is as follows:
OnClientChecked="<js function name>"
NOTE: you should be providing only the function name without parenthesis i.e. ( and ).
In your case since you provided the parenthesis - when the button gets initialized the client side functions associated with it are getting executed and you are having your Javascript code run at runtime.
So here is the right code for this:
<telerik:RadButton runat="server" Text="test" OnClientClicked="func" />
<script>
function func() {
alert("clicked");
}
</script>
Hope this answers your question.

Doesn't the RadButton have the onClick methods instead of OnClientClicked ?

Actually, I've run into this behavior also. Refreshing the page does imply a postback as the person who posted the alleged answer for this claims. The js function specified for the onclientclicked event is firing on the loading of the page. I noticed the answer simply re-stated the obvious and what the original poster had posted as the code that is not working and claimed it worked without addressing the issue of the function being fired on load and simply assumed a 'refresh' was equivalent to a postback.
I simply thought I'd address to fish for a valid analysis and to point out simply that if someone posts something and their friend promotes it as an answer, it doesn't mean it's right.

Related

Calling codebehind method from markup and is throwing javascript error

I have a div tag that has a click event and the method I'm trying to call is from the codebehind.
This is my div tag
<div class="DivA" runat="server" id="ThisDiv" onclick="<%ClickMe();%>"></div>
The method is a simple
public void ClickMe()
{
Response.Redirect("www.google.ca");
}
I'm just testing this before I add the real stuff to it. The error that it is throwing is...
JavaScript critical error at line 16, column 49 in http://localhost:24307/DIVPAGE.aspx
SCRIPT1002: Syntax error
this is the line that it is giving me
<div id="ThisDiv" class="DivA" onclick="<%ClickMe();%>"></div>
I have tried changing the
<%ClickMe();%>
to
<%=ClickMe()%>
But that throws the same error. Another thing I don't understand is when you look at the line with the error that it is missing the runat tag and has added other characters to the onclick event.
Thanks
You have a concept problem here, do this, and test it will work:
<asp:LinkButton id="lbClickMe" runat="server" OnClick="ClickMe">
<div class="DivA" id="ThisDiv">
The Click Me Button!
</div>
</asp:LinkButton>
That's it, when runat=server is specified ASP.NET page parser will process the element as server side, so for this elements/controls no server tags in markup are allowed except data binding tags inside control templates. So to call you method you have to put a runat server on a control that haves the Click event, this is the case of the LinkButton, inside of him you can put your div for some specific styling of your UI.
Also not that, if you really want to have the your div behaving like that, there is no problem in complicating what is simple, but in that case please do this instead:
<asp:LinkButton id="lbClickMe" runat="server" OnClick="ClickMe" Visible="False"></asp:LinkButton>
<div class="DivA" id="ThisDiv" onclick="<%= Page.GetPostBackEventReference(lbClickMe) %>"></div>
The GetPostBackEventReference extracts the javascript code necessary to simulate your link button click, but once more is preferable to use directly the link button if you can.
Hope it helps,
Regards.
The <%= %> syntax emits a string, it doesn't do anything, like a redirect.
You need to do your redirect client-side with this javascript:
window.location = 'http://my.url.com';
If you need to interact with server side code, you need to do so with AJAX communicating to a web service to get the URL you need, and then performing the redirect described above.
Update
Sorry lads, brain freeze.
Yes, indeed, you can inject a string that will be evaluated as a click handler, but the handler must be a javascript function, not a server-side one! Once the page is rendered, it can no longer interact with the server save for communicating with a web service (or if we want to get technical, web sockets as well).
You can't call server-side C# methods from the DOM like that. You can only call JavaScript functions in an HTMLElement's onclick handler.
It is correct that you can call server-side methods using the template language, however this will be executed at the time of rendering the page; you could, for example, render the results of that server-side method, but you can't use a server-side method as a handler for a client-side event. The onclick event on a DOM element can only call a JavaScript function.
ASP web controls also have an OnClick event attribute, which is probably what's confusing you; this is different from the onclick event attribute on DOM elements (ASP will create additional code for its web controls, e.g. in case of an asp:button). This works using ViewState and a postback to the server. The onclick event for a DOM element however won't do those things for you.
Adding runat="server" will convert your element to an ASP control, however it will only be an HtmlControl. In the case of a <div>, it will be an HtmlGenericControl which simply writes out the onclick attribute of your element as it is.

Is it possible for a server to treat a postback as not a postback?

Currently I have a site that loads everything on initial load (when it's not a postback). Then it proceeds to load more data that should be fine to load regardless if it's postback or not. I thought everytime the page is refreshed or the button is pressed there is a postback. What I thought that if the user doesn't go to another page, any action he takes will be a postback.
However I'm getting very inconsistent errors when the site is actually on a server and was curious if perhaps, when clicking a button after a bit of inactivity, will the server possibly forget about the previous activity and treat the action as the person hitting the site for the first time again?
Below is how the button is defined....
<asp:Button CssClass="btn btn-default controls" ID="btnAddAdditionalCom" runat="server" Text="Add Comment" OnClick="btnAddAdditionalCom_Click"/>
Any asp:button click will cause a postback to the server because asp is a server side language so it has to talk to the server to execute the button click. If you want to do button clicks without talking to the server use something like javascript
You cant modify it.
Because this is a essential concepts in asp.net.
Even you cant change the value of IsPostback Property (It has no setter).
If you want to treat Postback as page load then u forgot the need of IsPostback

Button which is not runat server triggers all validators on the page

Firstly before we go anywhere please don't worry about javascript in your answer. Client side validation is working as expected and I have turned off javascript in my browser to focus on testing server side validation. OK!
I have a webforms user control which has input fields and validators. But due to the specific HTML and CSS design I have been given, I can't use an <asp:Button> control it is necessary for me to use a html <button type="submit"> to submit the form. This is Not the same as <asp:Button> which renders as <input type="submit">
I have tested my html <button> tags in all the browsers I need to support and they are working. In my Page_Load I can determine which button has been clicked and trigger the correct validation group, and then if validation passes, run the right method.
Unfortunately when I click a button, all the validators in all the user controls in all the validation groups get triggered server-side! I was expecting none of the validators to be triggered so that I can use Page.Validate server-side. Has anyone seen this behaviour? Is there a known solution?
Thanks :-)

Adding javascript functions to asp control

iv'e got an asp button which performs another task besides postback
i have done this by adding javascript code to the button as follows
if( !IsPostBack )
btn1.Attributes.Add("onclick", "f();");
i'm not sure of 2 things
the first :
where in the cs code i should add the function f() , currently i'm doing it in page load
while ignoring postbacks because then the function would have already been added(i might be wrong)
the second :
is there any to make the function execute only if the page was validated ? (after postback)
thanks in advance
eran.
I would use OnClientClick instead of adding the click event throug the attributes. As for your JavaScript, I would add it in the ASPX part. Depending on what the function does, you might be able to avoid the code behind all together and do something like this:
<asp:Button ID="Button1" runat="server" Text="Hi" OnClientClick="javascript:return f();" OnClick="Button1_Click" />
From your f() function, kick off the validation, and return true if validation passes, otherwise return false. If OnClientClick returns true the page will post back, and if it returns false the page will not post back.
1st Question
You will need to add it each time.
btn1.Attributes.Add("onclick", "f();");
2nd Question
You can check the validity of a page by checking the Page.IsValid Property
Unobtrusive JavaScript
If you're serious about javascript then you don't want to write inline javascript.
You will want to create an event handler in javascript code inside the script tags or in a external file.
If you're using .NET 4 server control id:s will be good, else I would use jquery's
contains selector. Search for the given server control ID.

page postback in asp.net?

in my application i have the playvideo page where video will play, below that i have the option for sharing the video like adding to favorite ,playlist and sending mail.
when i click on any of the link the page is postbacking and video will start from the first.
i place update panel for link button even though it is not working (video is playing from the first i.e., page is postbacking. can u help me. thank you
Actually, the part of page that is within the UpdatePanel does the postback. Make sure you have only those controls(for instance, your links) inside the UpdatePanel.
Alternatively, you can use multiple UpdatePanels; for instance one for your video and one for the links. In this case note that, when one UpdatePanel gets updated other UpdatePanels also gets updated, which you may not want; so all you have to do then is to mark the UpdateMode property to Conditional and call YourDesiredUpdatePanel.Update() method manually - whenever required.
Btw, updating selected portions of the page also reduces the load on the server
Or you may want to look into using client callbacks instead of a postback. But since client callback uses XMLHTTP, which means Microsoft implementation of AJAX, therefore callbacks are just awesome as long as your are working with IE.
You might want to try taking advantage of Page Methods to do the work you need done server side.
http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/
Also, if you want to prevent a control from posting back, you can add return false to the end of your javascript onclick event on the control.
For example, if you had an asp button you were using you could do this:
<asp:Button ID="myButton" runat="server" OnClientClick="DoThingsInJavascript(); return false;" />
Or if you were just using a standard button you could say:
<input type="button" onclick="DoThingsInJavascript(); return false;" />
I've never really liked the update panel and I have sometimes found it's behaviour awful. Have you thought of trying something like a proper ajax call from Javascript

Categories

Resources