I have a button which causes a postback and also calls the javascript function hideInsert() which looks something like this:
function hideInsert() {
$('.hide').hide();
alert("hide");
}
All it does is hiding tablerows marked with ".hide". This works as intended but since the postback occurs, everything gets reset.
Is there anyway I can click the button to trigger the postback and then run the function, after the postback has occurred?
I have been looking at this http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx but with no success.
I would then need to press another button which would trigger the showInsert() function, which would need a similar function.
In whatever event makes most sense according to your current architecture, include:
if (Page.IsPostBack) {
ClientScript.RegisterStartupScript(this.GetType(), "HideOnPostback", "$(function() { hideInsert(); })", true);
}
Page_Load is a common place to include logic like this.
Alternatively, if you will never need whatever is classed as .hide after they postback and they are server-side controls, you could always set them to Visible = false.
Related
I have written a code in VB.NET in which there are around 300 asp controls and all of them are creating dynamically with more than 6 condition per control (like If control = dropdownlist then some code Elseif control = radiobuttonlist then some other code).
Now I want to write events for some controls but due to postback, when event is fired all of the controls are getting flushed.
When I set button1.onclientclick="return false" for button, the page stopped post back but the event also stopped working.
I have an option to save the values of controls in view state then recreating the controls and then refill the values to dynamic controls. This option will increase my line of execution.
Is there any other method though which I can prevent the page to post back on asp control event so that my asp control persists with entered values in it and also my event will work.
this is the Code1
this is the Code2
Create your dynamic controls OnPreInit Event of Page, Hope this solves your problem
override protected void OnPreInit(EventArgs e)
{
CreateDynamicControls();//Function which creates all dynamic controls
}
I have used JavaScript and Ajax to achieve my requirement.
I have called JS function for button's onclick and Textbox's onchange (like: btn1.Attribute.Add("onClick",JSFunction(); return false;) [return false is to prevent postback].
Then I used ajax post method to do my stuff on .vb page.
I'm using the Telerik RadSlider with jQuery 1.10.2. Currently I am capturing 2 events:
OnClientValueChanged: Fires when either the up/down arrow handles are clicked, or the drag slider is in the process of being moved.
OnClientSlideEnd: Fires when the drag slider has finished moving (but does not fire when the arrow handles are clicked).
My question is how can I detect when only the up/down arrow handles are clicked? Obviously I could just hook into the OnClientValueChanged (1st event above) and call the function hooked up to the OnClientSlideEnd (2nd event), but since I am doing calculations and saving values to the database each time the OnClientSlideEnd is triggered, it would be incredibly inefficient to do the calculations and save these values each time the slider handle is dragged.
Here is my current RadSlider setup. Please note that the handles are generated dynamically, but on inspecting the element in Chrome, it has a class of "rslHandle":
$(document).ready(function myfunction() {
$('.rslHandle, .rslHorizontal, .RadSlider').live('click', 'body', function () {
alert('arrow clicked');
});
});
<telerik:RadSlider ID="radSliderTest" runat="server" CssClass="CustomSliderLook" OnClientValueChanged="OnClientSlideChanged" OnClientSlideEnd="OnClientSlideEnd" MaximumValue="100" TrackMouseWheel="true" Width="300px" />
I've tried the outdated "live click", "on click", and just the plain ol' "click" method and none have triggered. Looking through Telerik's documentation for events on the RadSlider, I haven't been able to find anything for the arrow change events other than what is posted here. Thanks very much for your help!
Ok I found the solution. It worked by capturing the mouseup event like so:
$(".rslHandle").live('mouseup', function (e) {
alert('arrow clicked');
});
I just tried the click() jQuery way of attaching to the click event:
function pageLoad() {
$telerik.$(".rslHandle").click(function () { alert(1); });
}
which uses the Sys.Application.Load (preferred for IScriptControls like this).
or
$telerik.$(document).ready(function () {
$telerik.$(".rslHandle").click(function () { alert(1); });
});
where an anonymous function is attached in the document.ready jQuery event.
Both use the jQuery Telerik provide.
You can cascade the selectors and/or use the OnClientLoad event of the slider itself so you can know which is the HTML element where the slider is created.
I am not sure, however, why using the built-in events is not OK with you. If you need the final value, regardless of how many times the end user changes it, why not simply use the radSliderTest.SelectedValue property on the server? Ultimately, the page will be POST-ed so you can get the rest of the textboxes, dropdowns, etc., so you can use this moment to get the slider value on the server without any additional code. The slider also offers the AutoPostBack property so when the user changes the value, it can POST the page and fire the OnValueChanged server event.
Okay,
I have this scenario:
There is a user control with an update panel within it. There is a button within that update panel with proper postback trigger being set. The button_click event is also defined well. I need to call a full postback of the parent aspx page once the "button_click" event is completed. Under ideal case, all the form submission events such as postbacks occur before event based methods are executed. This means my page will first be reloaded then the button click event will be executed. I want something like to reverse this operation. First Button_click event execution then one postback after that on the aspx page(this page calls the user control-> and this user control has the updatepanel with button in it).
Any possible way out would be highly appreciated.
I don't think there's a way to change ASP.NET's lifecycle, like the one you described. A (dirty) way of postbacking the parent page is however to put a hidden button on that page, and call it via javascript in the UC. (via ScriptManager.RegisterStartupScript)
In my Asp.Net Web Form,
Assuming that, I have Main_page and Save_button that it is on Main_page.
When I click Save_button, firstly Page_Load eventexecute and after this btnSave_Click button execute.
I thought that when I click button firstly and only button might execute.
Is it correct or my program doesn't work correctly ?
It is normal that Page_Load is executed first and event handlers afterwards. So your program behaves as designed.
Excerpt from MSDN on the page lifecycle:
Load
During load, if the current request is a postback, control properties are loaded with information recovered from view state and control state.
Postback event handling
If the request is a postback, control event handlers are called. After that, the Validate method of all validator controls is called, which sets the IsValid property of individual validator controls and of the page. (There is an exception to this sequence: the handler for the event that caused validation is called after validation.)
If you are interested in details on the lifecycle of an ASP.NET Page, have a look at this link.
Resolution
If you need to perform certain steps in Page_Load (or any other method on your page) only when the page is requested first, you can check the IsPostBack property and thus make your program behave as you describe in your question:
public void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Steps are only run on initial GET, not when request is a PostBack.
}
}
It is correct that load event fires first then button event handler. If you want to execute code only at first, but not when any postback check IsPostBack property.
I'm working in ASP .Net. I need to be able to click a button, disable it, have the code behind kick in and execute some functions and once they are done, enable the button again. There is no postback taking place per say in this project as we are using an UpdatePanel. About as far as I've been able to get successfully is using this syntax to disable it on the code behind Page_Load function:
btnConnect.Attributes.Add("onClick", "document.body.style.cursor = 'wait';this.disabled = true;" + ClientScript.GetPostBackEventReference(btnConnect, string.Empty) + ";");
This disables the button and allows the code behind to execute. I just can't figure out how to re-enable the button once the functions that run in the code behind are done. Is there a way to capture when the code behind is done?
Are you able to use jQuery instead? Consider a flow like this:
on click, disable button
call/POST to an ASP.NET URL as needed
re-enable the button when the URL call returns. Update page elements, redirect, alert as needed.
You can use a timeout approach after the disable:
var that = this; //pointer to button
window.setTimeout(function() { document.body.style.cursor = '';that.disabled = false; }, 1000);
This approach can be useful to prevent a double-postback too.
Or the update panel finishes it fires the Sys.WebForms.PageRequestManager.endRequest event. Add an event handler to reenable the button in there as another alternative.
In addition to above...
Yes, you are using an UpdatePanel, but a PostBack is occurring.
So, in your submit button event, after successful processing is complete, remove the attribute for onClick you added above, have you tried to enable the button in the code behind and then issue an Update() on your UpdatePanel?