I have set an asp.net page to refresh every after sometime. The problem is that once the page is refresh, the state of the controls are lost, like the dropdown loses all its item. I checked the viewstate property of the controls but they are set to be enabled. Any idea why is it so?
Thanks,
Because a web form is also an HTML form. You need to POST the form instead of refreshing.
Related
i have a problem with my asp.net site.
i have a checkbox on this site, which is per default checked.
then i have a javascript code which reloads the page every minute.
now the problem:
when i uncheck the checkbox and the page reloads, it gets checked again.
but i want to avoid this, i want the checkbox to have the same checked/unchecked property as before the reload.
how to do this?
You will need to inform your server(again with javascript) when the state of the checkbox changes.
This information will be saved (i.e. in viewstate if you're using Web Forms, db, session) and will be used when serving subsequent requests.
You can send the current states of your checkboxes (check/uncheck) in querystring, when the page reloads from javascript.
IN C# page load:
if(!string.IsNullOrWhiteSpace(Request.QueryString("states")))
{
SetCheckBoxes(Request.QueryString("states"));
}
When the ASP.net Page is Postback the controls inside the table is disappear,
but when i click on button that has that code which cause transfer to the page:
Server.Transfer("~/Admins/EditUsers.aspx");
all controls appear easy with no problems.
Then,is there is need to make refresh to the page, or what can i do?
Thanks
A postback already performs a page refresh automatically.
If controls are disappearing, that suggests that you might not be creating them on the postback. Note that tables do not store their contents in ViewState. Is there any chance you are testing for IsPostBack in your page Load handler? If so, you must recreate the table on every load, whether a postback or not.
Beyond that, you'd probably need to provide a bit more specific information.
I've got a master page with a section for subnavigation links on it:
<div id="sub_nav" runat="server"></div>
I programatically populate this with Hyperlinks when my main asp:Menu data is bound depending on the address of the page I'm on.
This works fine and all my correct submenu stuff shows up on each page. The problem is that when one of these pages does a postback, I lose all the links that were in my sub_nav div.
Now, I could just populate the div with links every time regardless of whether the master page load is a postback or not, but I figured there is a better way of doing this. I was thinking enabling the viewstate on the div and links inside it might persist them through postbacks, but apparently that is not how viewstate works.
What is the correct way of doing this?
Viewstate only stores the current state of a control and not the controls by themselves. If you are dynamically adding controls make sure to add them on page init method irrespective of postback
This MSDN sample should help you.
According to the excellent article TRULY Understanding ViewState, that's not really the purpose of ViewState. Furthermore, ViewState costs additional bandwidth so in general we want to avoid it if possible. It sounds like this data should be "cheap" to obtain (cacheable or whatnot), so I'd definitely populate it on every request and disable ViewState on those controls.
To understand the main purpose of ViewState consider a page with two buttons, btnA and btnB and two labels lblA and lblB.
When the user clicks btnA , the page posts back and sets lblA to "You clicked A!".
When the user clicks btnB, the page posts back and sets lblB to "You clicked B!".
With ViewState, the page remembers that lblA.Text was set to "You clicked A!" previously and restores that value. Without ViewState, if the user clicked A and then B, the page would only display "You clicked B!" because there's nothing to store the previous value of lblA.
I have a simple user control containing two text boxes which I am adding to placeholder on Button Click event. I am storing the number(count) of clicks in View state and running a loop using count to create all previously added user control. I am also adding IDs to each User control (appending "UC" and count). I have also checked in view source the ids are same each time they are created. I have another button which basically does an post back. I have EnableViewState enabled ="true" in all controlls all the way up to Page Level.
My problem is that User Input does not persist on postback. please advice. Should this not be happening automatically for me?
Have a look at this:
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
I've encountered minor problems with it on a web farm, but for simple deployments its use is pretty straightforward. (Comes with source code, and the web farm glitch is a pretty simple fix.)
You need to create your dynamic controls in the Page_PreInit event rather than Page_Load or in a Click event handler. That way, they'll be there before ViewState, and then your posted values are applied.
I thinks what is happening is that you are creating your controls during the click event handler which happens AFTER ViewState and PostBack have been applied. This means your controls will be created empty each time.
please tell me how to maintain state of placeholder. i have a placeholder in which i add many image controls dynamically but when my page get refresh all controls from placeholder gets removed from it. the enableViewstate of placeholder is set to true.. please tell me how to maintain its state..
ViewState registration happens just after the Init events in the Page lifecycle.
If you are adding your dynamic images after Init, then they are not registered as part of ViewState. Your issue will be solved if you add them to your Placeholder control during Page_Init.
I think you need to add the controls in the Page_PreInt() event
You must add dynamic controls on every page load. Controls are not stored in ViewState, only control STATES and data for controls which do not post their data.