trigger postback between two aspx pages with change in asp:dropdown value - asp.net

I am having an aspx page with div tag:
<div id="report"><div>
I am calling other page with the help of javascript function because the "id" decides what drop-downs should be able on page.
function loadreport(id){
$("#report").load('/lists.aspx?id=' + id + ' #reportdropdowns', function () {
});}
In that lists.aspx i am having couple of dropdowns.
<asp:DropDownList ID="Account" AutoPostBack="true" AppendDataBoundItems="true" runat="server">
<asp:DropDownList ID="Company" AppendDataBoundItems="true" runat="server">
When i change a value from account drop-down, it is triggering postback event, but that event is coming back to report.aspx instead of lists.aspx.
I thought of catching that postback in page_load of report.aspx and call loadreport() using
Page.RegisterStartUpScript();
Then it has thrown error of "id" not defined. Keeping that aside, whole report.aspx is getting refreshed.
Is there any way, i can do postback to lists.aspx by passing selected value of first drop-down and only refresh second drop-down instead of whole page.

If you load aspx page through ajax request and assign html to div then all your postback control event will not work with your page that is loaded via ajax request.
To achieve this you have to use ajax request then it will work
Another way to make this work is to do a full page load in an iframe

Related

How can I persist the changes of a text box when it loses focus?

I have several text boxes on a page. I want to save the text in the corresponding TextBox on LostFocus. It is to ensure the data is not lost when power failure or internet connectivity is lost. How can I accomplish something like this?
Another solution would be to use an UpdatePanel. You could then leverage server-side events rather than an event handler. You're markup might look something like this:
<asp:ScriptManager ID="ScriptManager" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<asp:TextBox ID="SomeTextBox" runat="server" TextChanged="SomeTextBox_TextChanged" AutoPostBack="true" />
</asp:UpdatePanel>
</asp:ScriptManager
and then in the TextChanged handler you could do your work.
Explanation: the TextChanged event will fire on the text box in which the text was actually changed when the post back occurs. The AutoPostBack property is necessary so that the page will in fact post back when the text box loses focus.
One final thing to remember, you will probably need to leverage this.IsPostBack on the page in the Load handler because every time a control is updated it's going to reconstruct the page. It's common that logic in the Load handler only needs to execute one time, but that would pose a problem if you didn't check to see if it was a post back because then it would execute every single time you left a text box.
Use jquery.
First on attach blur event handler, where you call ajax method to server passing new value of the textbox.
Handle this ajax event on serverside and write your data to the database or anywhere else.
Here is a piece of code that may help.
$('#textbox_id').blur(function() {
$.ajax({url:"handler_url.ashx?textbox_value=" + $('#textbox_id').val()});
});
Then create your ashx handler on server and handle your requests with ProcessRequest method. From there you will have access to Request.QueryString where new value of textbox will be stored.

How can I refresh a dropdownlist during an event of another dropdownlist without refreshing whole web page?

I am developing my first asp.net website, my requirement is to refresh DropDownListB at SelectedIndexChanged event of DropDownListA, I have set AutoPostBack="True" for DropDownListA. Now the problem is whole web page gets refreshed, its unnecessary for me, is there any other technique that i can use to refresh only that control or only that panel rather than refreshing whole page?
Put the dropdowns inside
<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
// Dropdowns
</ContentTemplate>
</asp:UpdatePanel>
and include <asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager> at the top
1- You can simply place the dropdown in an UpdatePanel, this will avoid a complete post back.
You can get more details on UpdatePanel here
2- You can use jQuery AJAX to fetch the data in JSON format and bind it to the dropdown list, this approach is more efficient but little complex in comparison to UpdatePanel
You can find so many articles on this if you search this on google , like
[EDIT]
You can find a similar implementation here

UpdatePanel update without trigger button

I have an UpdatePanel with ContentTemplate specified. When page loads, user can do some AJAX work in other part of the page. Then, after that work is finished, I would like to update only content inside UpdatePanel, but without pressing any buttons etc. I should be done automatically using JavaScript when previously started AJAX work finishes. How to do it without manual clicking on the trigger button?
EDIT:
Ok, I've followed that _doPostBack rule, and whole page is posted.
<asp:UpdatePanel ID="panelAttachments" runat="server">
<ContentTemplate>
........
</ContentTemplate>
</asp:UpdatePanel>
<input type="text" name="test" onchange="__doPostBack('<%=panelAttachments.UniqueID %>',''); return false;" />
</td>
Thanks, Pawel
To refresh an update panel from javascript:
__doPostBack(updatePanelUniqueID,'');
The first parameter is the UniqueID (not CientID) of the UpdatePanel.The 2nd parameter is optional arguments you can pass which will be available to your server code. Both are stored in hidden form fields by ASP.NET, you can access them in codebehind:
Request.Form["__EVENTTARGET"];
Request.Form["__EVENTARGUMENT"];
But if you just want to refresh a panel and don't need to pass any additional info from the client, you can ignore then 2nd argument.
If you look at the HTML generated by ASP.NET for an async postback control, you'll see it's exactly this.

How to enable html select post back apsx page when selection changed?

With asp.NET control dropdownlist, there is a property AutoPostBack, if it is set "True", the whole page will be posted back.
If the aspx page include a html element "select" like:
<select id="list" name="list" runat="server"
DataTextField="Name" DataValueField="ID" ></select>
and it data is filled by code-behind.
Question is: how to allow this Select have AutoPostBack function too?
The DropDownList approach adds a __doPostBack('selectelementname', 'commandname'); call to the onchange event. When you change the value, this then proceeds to post back to the server, and then the ASP.NET control processes the postback data in LoadPostData method.
HTH.
You can't apply Auto post back property for html select control.To invoke function writen inside c# code page(serverside),you need to use webservice. You can call javascript function(client side) on 'onchange' event of html select control.

Reverse order of operations for OnClick and OnClientClick?

I have some simple javascript that I'd like to run when a button is clicked, but I also want some postback action to occur on the server. The logical code for this looks like this:
<asp:Button ID="btnOK" runat="server" Text="Save Changes" OnClientClick="UpdateParent();" OnClick="btnOK_Click" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClientClick="window.close();" />
<script language="javascript" type="text/javascript">
function UpdateParent()
{
window.opener.document.location.reload(true); // or should we postback instead?
window.close();
}
</script>
Basically, a popup window should refresh its parent and then close itself. However... if I call window.close(), the postback does not occur and the button handler is not called. But obviously OnClientClick is called before the postback happens. Am I going to have to emit this javascript in the button handler and run it when the page loads after postback? If so, what is the proper way to do this these days for ASP.NET 2.0?
It's a shame that the code above doesn't work as it's elegantly simple and straightforward.
You have to do the postback before closing the window. Also you want to do the postback before refreshing the parent window, as I guess that the reason to refresh the window is to display the information that you are about to save.
Use the RegisterStartupScript in the ClientScript object to run the code after postback:
Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "window.opener.location.reload(true);window.close();", true);
However, if the parent page is a result of a postback, this would cause a dialog window in the browser informing the user that a post request is needed to reload the page. To avoid this you would have to do something like calling a function in the parent page that could do a postback to update the page.

Resources