__doPostBack reloading entire page instead of just the UpdatePanel - asp.net

In my javascript, I have the following line:
__doPostBack('MyPanel', MyParam);
In my code behind, I use MyParam to query a database and bind the result to a gridview that's inside the MyPanel updatepanel. The updatemode of the updatepanel is set to conditional and in the postback part of the code I have MyPanel.Update();
The updatepanel works fine when I'm doing sorting and paging; only the panel is refreshed. However, when I trigger the updatepanel with my javascript, I see the traffic in firebug showing that the entire page is being refreshed.
What's the solution?
Thanks.

My assuption: your update panel is located inside the naming container, so its id in the client side will be a little bit different from the server side ID. This means you pass the wrong __EVENTTARGET parameter to the client side __doPostBack function and your partial postback became full(meaning not async).
So changing your client code to:
__doPostBack('<%= MyPanel.ClientID %>', MyParam);
should solve the problem.
BTW, you could get the second(MyParam in your code) parameter from the server side:
var arg = Request.Params.Get("__EVENTARGUMENT");

Related

TextBox keydown event at server side in asp.net

i have a textbox on a page, now whenever any age number is entered in that page, that page should be loaded in my datalist, i have created rest of the thing, but i am not getting how to trigger the textbox onkeydown event from asp.net, i know working with that from javascript, but the problem is i have below things done in that function:
it applies the currentpage value to the static variable from textbox
it binds the datalist
it enable disable the next previous buttons accordingly
and i dont think this i can do from javascript, anyone have any resolution, how this could be achieved from server side, onkeydown event
You can capture the keydown event in a javascript function then do a AJAX call to the server. If this does not suit you, then you could try manually do postback in javascript.
So in your textbox:
mytextBox.Attributes.Add("onkeydown", "return doDataListBind()");
Now in the javascript function:
function doDataListBind()
{
__doPostBack("myTextBox"); // etc, etc
// some other code here...
}
More info can be found here:
http://geekswithblogs.net/mnf/archive/2005/11/04/59081.aspx
http://www.asp.net/ajaxlibrary/AjaxControlToolkitSampleSite/
Try looking into or using either:
AJAX Control Toolkit, or
JQuery Autocomplete Plugin
Alternatively you could try to call _postback() function from a client side event/function for your textbox in javascript

Use asp.net AJAX from javascript

This is probably a simple question.
I use ASP.NET ajax toolkit and Jquery. I want to call a server-side function/method from Javascript and have it update a control. Can i do this?
Client-side
send_request(foobar,args);
Server-side
private void foorbar(){
Label1.Text = "updated";
}
Do you want it to fire a server-side method and update a server-side control on the page? You can create a ASP.NET UpdatePanel, let's say there is a button1 inside, and from your JQuery code, write this.
function OnClick()
{
__doPostBack(button1.ClientID, "argument")
}
and in your server side code, Page_Load event, you will find the EVENTTARGET and EVENTARGUMENT in the REQUEST variable, which contains the information you just postback, you can then update the control in the UpdatePanel itself as long as the control is within the UpdatePanel, it will be handled properly by ASP.NET AJAX.
More details here
http://www.dotnetspider.com/resources/16920-Post-back-gets-demystified-doPostBack-defined.aspx
Yes that can be done Client Callback
You could take a look at ASP.NET Page Methods.
jQuery.ajax({
url:'url to call', //usually webservices in asp.net
dataType:'json',
type:'POST', //(asp.net werbservices by default accepts POST request only)
success:function(data){
//do some thing with this data, like will dom elements etc
}
});
#smkngspcmn:
I placed everything inside an update panel and did something like $('#Year').change(function() { __doPostBack("submit", ""); }); That does do a full post back without Ajax. What am i doing wrong? Should i place the above script inside the update panel as well?
The first argument to __doPostBack() should be the UniqueID of a server-side control inside the UpdatePanel. For example, you can put a hidden button inside the UpdatePanel:
<asp:Button ID="HiddenButton" runat="server"
style="display:none" OnClick="HiddenButton_Click" />
When the button is rendered on the page, you can take the name attribute of the <input type="submit"> element that represents the submit button and use it for the first argument to _doPostBack(). In this way, whenever your script runs it the UpdatePanel will make an asynchronous postback and the HiddenButton_Click event handler will be fired.

UpdatePanel refresh from client

I'm trying to refresh update panel via Javascript:
__doPostBack("<%=upMyPanel.ClientID %>", "");
But somehow its controls are all empty. On other hand they are all filled when I click any trigger control.
How can I fix this?
thanx.
AFAIK the UpdatePanel doesn't really postback, so your postback should be using a control that is registered for postbacks (i.e. the trigger control you speak of).
__doPostBack("<%=btnMyTrigger.ClientID %>", "");
The UpdatePanel has no client-side API; third party update panels typically do but not the MS one... You could try __doPostBack() as mentioned and target a control that posts to the server (like a button).
Try making the update panel mode to always to see if that works properly with __doPostBack. Also, try the UniqueID instead of the ClientID.

how to get javascript to execute in update panel

I have an update panel which contains a table, to which I add rows of controls on a button click. One of the controls that is added, is a user control and it is a datepicker. Inside of that user control I have a textbox, and I have JQuery which applies the JQuery UI DatePicker plugin to it thereby turning it into a datepicker. It's not a problem if that user control is loaded onto a page dynamically, however, if it is done on async postbacks inside an update panel, the javascript doesn't fire and therefore the textbox is render but without all the jquery datepicker functionality. Here's some code that's inside the DatePicker.ascx:
$(function() {
//reset the localization
$.datepicker.setDefaults($.extend($.datepicker.regional['']));
$("#<%=txtDate.ClientID%>").datepicker({ dateFormat: 'mm/dd/yy', showOn: 'button', buttonImage: '/images/calendar.gif', buttonImageOnly: true, altField: '#<%=txtDate.ClientID%>' });
}
So this jquery isn't fired when the control is loaded in dynamically on an async postback. So how can I make this work?
Check this Page
look for the session on DatePicker
it says that ASP.NET AJAX UpdatePanel's wipes the DOM, so the load doesn't fire.
the post says you can either, use the EndRequest handler, or the pageLoad shortcut, however I'm thinking if you just couldn't register the javascript with ScriptManager.
Hope this helps.
When you render a script in a response, it does just that: render the script. It is during the rendering of the page that the script is first rendered, and that has already happened by the time that this code is being executed.
Microsoft has come up with a solution for this, which is to register the script block via ScriptManager.RegisterClientScriptBlock, and the ScriptManager will then see to it that the script is executed after page load.
Depending on your situation, that may or may not be helpful. Or, in the intermediate: it may be helpful only with a bit of workarounds. From your comment, I can think of one workaround I might have considered using:
Add a GetClientScript method to your UserControl, where the JavaScript is returned. If the UserControl is loaded on a regular page load, you invoke that method at the point where you are currently rendering your script. If the control is loaded in a UpdatePanel postback, you get the scriptbody from the control using that method, and register it in the ScriptManager at hand. You should, in this case, also pass a parameter back to the UserControl not to register the script once more, where it usually does.
Is the the DatePicker.ascx inserting that javascript at the top of the page during the initial load? And then that function fails since the datepicker is not loaded yet. And then you load the datepicker on async into the update panel, but the function is not called.

How do I force a full postback from codebehind?

I'd like to programmatically force a full page postback to occur after an event is fired by one of the child controls. I can't add any triggers to make this work so it will have to be done via code. Thanks.
Sorry, don't understand. By the time an event in codebehind is running, a postback has already happened.
If you mean a client side event then setup OnClientClick to call what is returned by the following after your other client side functionality:
Page.ClientScript.GetPostBackEventReference(control, "")
(It will be something like __dopostback)

Resources