In my project I have placed a dropdownlist in an updatepanel.what I wanted to do is to select a value from dropdownlist and use it in a session.
but whatever I do, it will always give me null value because of not checking "Enable AutoPostBack".and when I do this, it will refresh the page so this isn't what I wanted.
It sounds like you may not be using the UpdatePanel feature properly. If you have the UpdatePanel set to update when children fire events, only the UpdatePanel should refresh, not the entire page. The code below seems to behave similar to what you are seeking. When changing the drop down, only the update panel posts back to the server and when you refresh the page, you can get the value out of the session.
ASPX CODE
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
Current Time: <asp:Label ID="lblTime" runat="server" /><br />
Session Value: <asp:Label ID="lblSessionValue" runat="server" /><br />
<br />
<asp:UpdatePanel ID="upSetSession" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlMyList" runat="server"
onselectedindexchanged="ddlMyList_SelectedIndexChanged"
AutoPostBack="true">
<asp:ListItem>Select One</asp:ListItem>
<asp:ListItem>Maybe</asp:ListItem>
<asp:ListItem>Yes</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlMyList"
EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
CODE BEHIND
protected void Page_Load(object sender, EventArgs e)
{
this.lblTime.Text = DateTime.Now.ToShortTimeString();
if (Session["MyValue"] != null)
this.lblSessionValue.Text = Session["MyValue"].ToString();
}
protected void ddlMyList_SelectedIndexChanged(object sender, EventArgs e)
{
Session.Remove("MyValue");
Session.Add("MyValue", this.ddlMyList.SelectedValue);
}
In order to get anything stored to Session, you have to submit it to the server.
Perhaps some more details on why you don't want the UpdatePanel refreshing would be helpful, and what you are trying to accomplish using the value in Session.
EDIT: Based on your comments, it seems to me that the solution would be to store the current .ascx file in Session, and set your DropDownList to have autopostback enabled.
So, on your handling of the "Next" and "Back" buttons, store an indicator for the correct .ascx to Session.
During your postback handling of the dropdownlist event, you could simply ensure that the current .ascx file is still being shown, by checking session for the correct file to show. When the result is returned to the client, nothing will appear to have changed, because the UpdatePanel is smart enough to realize it's the same content, and you will have successfully dealt with the dropdownlist value.
It sounds like you're doing way more work than you need to here. Have you looked into using an ASP.NET Wizard Control? http://msdn.microsoft.com/en-us/magazine/cc163894.aspx or just Google it.
If you still want to do it your way, you have to submit to the server (either with no autopostback + manual submit button click, or by enabling autopostback) since the Session is a server-side concept. HTTP is a stateless protocol, so the only concept of state has to be done outside of HTTP's domain. This means you're stuck storing state on the server (for instance, in the session) or, much more restrictively, on the client's computer (such as in a cookie).
thanks a lot I solved problem by controlling variables in Page_Load event.
If Label1.Text = 1 Then
Dim tempcontrol2 As Control = LoadControl("Page1.ascx")
PlaceHolder1.Controls.Add(tempcontrol2)
ElseIf Label1.Text = 2 Then
Dim tempcontrol2 As Control = LoadControl("Page2.ascx")
PlaceHolder1.Controls.Add(tempcontrol2)
End If
thank u for all answers
Related
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="updatePanel2" runat="server" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:Button ID="btnBlock" Text="BlockCalls" runat="server"
OnClick="btnBlock_Click" Enabled="True" Width="100px" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnBlock" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
When i click on btnBlock pageload even fires. I don't want page refresh when user clicks on button
So, If you put the trigger on button click it is normal when you click it to postback and refresh the page.
If you want to postback without page refresh you should have something like this:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="updatePanel2" runat="server">
<ContentTemplate>
<asp:Button ID="btnBlock" Text="BlockCalls" runat="server"
OnClick="btnBlock_Click" Enabled="True" Width="100px" />
</ContentTemplate>
</asp:UpdatePanel>
Either way your Page_Load event will be fired, but you can do this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//this is page load
}
else{
//this is PostBack
}
}
You still ALWAYS get a partial page post-back, and the page load event DOES fire each time with update panel - just like it does for any button outside of the update panel.
So, your page load event should have IsPostBack = false for first page setup code.
So code inside IsPostBack=False code stub only runs first time.
So, while the whole page will not re-fresh, and whole page is NOT re-posted? You still get what is called a "partial" page post back - and while only stuff inside panel is posted back to the server? Standard form events STILL fire each time including page load.
You need to build that page to handle post backs, and introduction of a update panel does NOT fix this requirement.
If your page can't survive a post-back, then it will not handle a update panel. So, a update panel does NOT prevent a post-back, but results in what we call a partial post-back.
So, say if you pop up say a jQuery.UI (or bootstrap) dialog, and do a post-back? Then that dialog will get blown up and out due to the page post back. As noted, update panels STILL do a post-back, but not a full page, and hence the term partial post-back, and thus any server side button or whatever inside of that panel that does a post back? Well, the page load event still fires each time.
So, while some jQuery and some controls can (and will) go zonkey with a full page post back? And using a update panel can help? Keep in mind that server side events in that update panel still result in post-backs - just not full page ones.
there. I have a gridview with a column of check boxes which was working when it was a small test project but when adding it to a page on my teams project it stopped firing the checkedChanged event. The check mark still appears or disappears but nothing fires. The only major difference is I was using an sqlDataSource object at first but for the new project i had to bind it to a database in the behind code.
Here's my html:
<asp:TemplateField HeaderText="Create Incident">
<ItemTemplate>
<asp:CheckBox ID="Selections" runat="server" ViewStateMode = "Enabled" OnCheckedChanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
and some simple behindcode:
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
Console.WriteLine("clicked!");
}
set AutoPostBack to True to enable post back
<asp:CheckBox ID="Selections" runat="server" ViewStateMode = "Enabled" OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="True" />
I found the solution. I added the conditional the block if(!page.isPostBack) around the stuff in my page load event.
Kept searching for about an hour till I found out that the actual reason why the OnCheckedChanged event was not firing on my page was due to duplication of names. Duh
An input control had a name 'submit' and there also exists a method in my JavaScript that is called submit(). The JavaScript interpreter was confused between the name of the control and a function, every time it needed to fire the event. Changed the name of the control and everything went back to working perfectly.
I'm new to ajax and using visual studio 2005 and framework 2.0. A simple Ajax sample always takes to the page load event for a button click. No deployment and all just running in debug mode takes me to the Page_Load event. Don't know what is the problem? I have checked the values of UpdatePanel1.IsInPartialRendering and ScriptManager1.IsInAsyncPostBack which is false.
Here is my code,
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" >
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />
<asp:Button ID="Button1" runat="server" Text="PostBack" OnClick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToLongTimeString()+UpdatePanel1.IsInPartialRendering+ScriptManager1.IsInAsyncPostBack;
}
Google and stackoverflow doesnt help me so far. So any kind hearts help me...
Control Inside Update Panel Cause asynchronous postback.
What is Asynchronous Postback?
From Refrence To MSDN
An asynchronous postback behaves much like a synchronous postback. All the server page life-cycle events occur, and view state and form data are preserved. However, in the rendering phase, only the contents of the UpdatePanel control are sent to the browser. The rest of the page remains unchanged.
Now if it cause all event on server than what is the use of Partial Rendering...
Partial-page rendering removes the need for the whole page to be
refreshed as the result of a postback. Instead, only individual
regions of the page that have changed are updated. As a result, users
do not see the whole page reload with every postback, which makes user
interaction with the Web page more seamless
I'm a bit rusty on my Web.forms, but as I recall, that is by design. The Page_Load does fire when an AJAX update panel sends a request to the server.
I have an asp.net page for a simple blog which looks like
<asp:TextBox ID="txtContent" runat="server" TextMode="MultiLine">Default Text</asp:TextBox>
<asp:LinkButton ID="lbSend" runat="server" Text="Send" OnClick="lbSend_Click" />
<script type="text/javascript">
//some javascript to click btnSave in update panel for every five minutes.
</script>
<asp:UpdatePanel ID="upSave" runat="server" Style="display: none;">
<ContentTemplate>
<asp:Button ID="btnSave" runat="server" Text="Button" OnClick="btnSave_Click" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSave" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
And in the code behind
protected void lbSend_Click(object sender, EventArgs e)
{
//Gets UserID from session and saves it to table A with txtContent
}
protected void btnSave_Click(object sender, EventArgs e)
{
//Gets UserID from session and saves it to table B with txtContent
}
Everything works okay. I fill the form and insert the content into tables with lbSend or btnSave. JavaScript is also clicking btnSave. However when it takes long to fill txtContent, about 40 minutes or more, the page is like expired. Clicking lbSend refreshes the page in an asynchronous way and everything you have written is gone. It is like the page have sleeped and wakes ups when I click. And lbSend works normal when you click it again.
What is the reason of this "sleep" altough btnSave continues to communicate with server? And how can I prevent it.
You are doing an Ajax post. Since it is an asynchronous post your browser happily moves on and you don't notice a problem until there is an error. If it was not Ajax then the whole page would have refreshed and you would have seen an error - assuming you display errors in a friendly format.
While implementing Ajax you should implement your own error handler so you can display to the user that an error has occurred. Also, you should implement a floating div or dialog that prevents the user from using your web page until the Ajax request has completed. I realize 40minutes is really really bad for a simple insert but I will leave that for your DBA to fix.
Let's talk about the floating div or dialog or "modal popup". You want to open it in the before_Ajax_send function and close it in your after_Ajax_receive function. Please refer to your Ajax library documentation for more information on these functions.
When you implement this dialog box it will enhance your user experience because it will let the user know that your application is doing something so please hold your horses.
Hope this helps.
I have a couple of dropdown boxes on a normal ASP.Net page.
I would like the user to be able to change these and to have the page Pseudo-post back to the server and store these changes without the user having to hit a save button.
I don't really need to display anything additional as the dropdown itself will reflect the new value, but I would like to post this change back without having the entire page flash due to postback
I have heard that this is possible using AJAX.Net...
Can someone point me in the right direction?
Add a reference to System.Web.Extensions and System.Web.Extensions.Design to your website. Then put a scriptmanager on your page and wrap your ddl in an updatepanel. Do whatever you want on the back-end.
For example...
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="yourDDL_SelectedIndexChanged">
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
protected void yourDDL_SelectedIndexChanged(object sender, EventArgs e)
{
// do whatever you want
}
Depends upon your Ajax Framework, Ra-Ajax have a sample of that here...