Newbie PostBacks And State Of Controls?
May 4, 2010
In the Page_Load() event handler for one of my pages I use the new statement about 100 times equating to memory allocation for about 100 objects. As follows:
using System.Web.UI.WebControls;
Table mytable1 = new Table();
TableRow [] myrows = new TableRow[5];
TableCell [] mycells = new TableCell[100];
int i;
for(i=0; i<5; i++) myrows[i] = new TableRow();
for(i=0; i<100; i++) mycells[i] = new TableCell();
My question is, should all of these new statements be enclosed like so:
if(IsPostBack==false) {
// Initialize all controls for page just once during the session
}
Or should the Controls be freshly allocated, initialized, and added to the page each time the Page_Load event handler is called? I personally don't think its efficient to create mytable from scratch every Page_Load since all I really ever change is the contents of the table and not the table itself.
View 2 Replies
Similar Messages:
May 13, 2010
I have a simple ASP page with databound grid (bound to an object source). The grid is within the page of a wizard and has a 'select' checkbox for each row.
In one stage of the wizard, I bind the GridView:
protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
{
// Bind and display matches
GridViewMatches.EnableViewState = true;
GridViewMatches.DataSource = getEmailRecipients();
GridViewMatches.DataBind();
And when the finish button is clicked, I iterate through the rows and check what's selected:
protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
{
// Set the selected values, depending on the checkboxes on the grid.
foreach (GridViewRow gr in GridViewMatches.Rows)
{
Int32 personID = Convert.ToInt32(gr.Cells[0].Text);
CheckBox selected = (CheckBox) gr.Cells[1].FindControl("CheckBoxSelectedToSend");
But at this stage GridViewMatches.Rows.Count = 0! I don't re-bind the grid, I shouldn't need to, right? I expect the view-state to maintain the state. (Also, if I do rebind the grid, my selection checkboxes will be cleared)
NB: This page also dynamically adds user controls in OnInit method. I have heard that it might mess with the view state, but as far as I can tell, I am doing it correctly and the viewstate for those added controls seems to work (values are persisted between postbacks)
UPDATE: Could this be to do with the fact I am setting the datasource programatically? I wondered if the asp engine was databinding the grid during the page lifecycle to a datasource that was not yet defined. (In a test page, the GridView is 'automatically' databound'. I don't want the grid to re-bound I just want the values from the viewstate from the previous post!
Also, I have this in the asp header: ViewStateEncryptionMode="Never" - this was to resolve an occasional 'Invalid Viewstate Validation MAC' message
For reference, my GridView is defined as follows:
[Code]....
View 3 Replies
Jul 20, 2010
how to make radiobuttons created using Html.RadioButton("id","value") maintain its checked state through postbacks?
View 6 Replies
Jan 17, 2011
I'm working on a webform with various controls. Depending on user-input I show/hide (using JQuery's show()/hide() functions) bits of the GUI. However if the form is posted-back and fails validation, I want the GUI to remain in the same state it was pre-postback rather than returning to the first-load state. Obviously the ASP.Net controls retain state, but I have HTML containers that are pure client-side objects.
In attempting to design a solution I find myself heading towards the murky (and tricky-to-debug) realms of hidden form fields - more reminiscent of my pre-JQuery work than anything 21st Century :-(
View 2 Replies
Jan 14, 2010
1. I have couple of checkboxes and some text fields outside the updatepanel.
2. Based on the checkbox selection, I will be hiding/showing the fields to user using javscript.
3. I have a upload control in the same page which is placed under the updatepanel. When I upload the file, I will be reading the content of the file and I'm trying to load it into the controls which are outside the updatepanel. As I know the controls won't updated until unless it has been added under the updatepanel.
4. I moved all the controls outside the udpatepanel (checkboxes and textboxes) and placed it inside the updatepanel so that when the file is uploaded I can read the file and assign to the controls.
After this, I was able to assign the values to the control. However, the problem is, the state of the page (javascript hide and show) has not been retained during the postback. For example, if i have 2 checkboxes and 2 textboxes, if i click firstone, it should show the first checkbox. If I click the second checkbox, it will show the second textbox. By default, when the fresh load happens, first textbox will be shown. When I click the second checkbox (its showing the second textbox) and do the postback by uploading the file, the state gets lost and when the page load completes, it's showing the first textbox instead of showing the second textbox.
View 3 Replies
Nov 14, 2010
I am developing a web part that uses AJAX Timer and Update Panel to perform some data check. There are two buttons outside the Update Panel on the web part that I also want to maintain during the Timer postback. I cannot put these two buttons in any UpdatePanel because they need to call Response.Write() to display some Excel data to users in their On_Click event. My current solution is using ViewState to maintain these two button in On_Load event. However, I am not sure if this is a correct solution (haven't tested it). Does any one know that during a Timer postback, if I disable/enable these two buttons using ViewState in On_Load, will it update the page properly with the two buttons disabled/enabled? Or will it ignore any control that are not in the UpdatePanel regardless where you maintain it? If my solution is not a good practice, can someone suggest other alternatives?
View 1 Replies
Feb 9, 2010
I've a checkbox with autopostback=true. If i check the checkbox the postback occurs and changes a label text. But if i click back in the browser the checkbox is not returning to its previous unchecked state. How can i bring back its state?
Can i write some JavaScript to persist the state of a control?
View 1 Replies
Mar 5, 2010
1) I found that my viewstate was not being persisted across postbacks for web user controls. I ensured that the Enable Viewstate property was set to true (of the page)
2) The Viewstate isnt being persisted on the 1st postback.
3) However, it is being persisted from the 2nd postback onwards.
I stepped through the code to ensure that the Viewstate had the 2 keys that I was adding on page load. However, on the 1st postback, the viewstate is empty. Everything is ok from the 2nd postback onwards.
[Code]....
View 3 Replies
Feb 5, 2011
I am having tough time finding out this reason. I have a web control where i add some variable in viewstate. like viewstate["test"] = "1".
when i post back this viewstate no more exists in collection.
[Code].....
View 1 Replies
Jul 14, 2010
I have a requirement where i need to add file upload contorls dynamically.Intially i have a upload control in my page and button named 'Add fileupload' and another button named 'Upload'.the fileupload control is added dynamically when 'Add fileupload' button is clicked andUpload button is used to upload the files.when i click on 'Add fileupload' button after browsing the file to fileupload control, the new fileuplaod contorl has been adding successfully but the first fileupload control is empty.Can any one please help me how the state of fileuplaod control is maintained.Here is the code
[code]...
View 6 Replies
Oct 12, 2010
how can DropDownList attributes be persisted accross page Postbacks?
Example:
DropdownList1.AutoPostBack = True
DropdownList1.items(0).Attributes.Add("Attribute1","somevalue")
On Postback the attribute is no longer available (ie. attributes.count=0).
I tried persisting using the code below, but was unsuccessful:
Before PostBack: ViewState.Add("DDL","DropdownList1")
On PostBack: DropdownList1 = ViewState("DDL")
This generated the error: DropDownlist is not marked as serializable.
I than tried:
Before PostBack: Session.Add("DDL","DropdownList1")
On PostBack: DropdownList1 = Session("DDL")
This did not err but attributes were still not available.
View 4 Replies
Feb 11, 2010
I'm a complete newbie to MVC. I just started playing with it and to my surprise IntelliSense still offered my the familiar server controls e.g. GridView, Button, etc. I thought the idea w/ MVC was to have much tighter control on HTML rendered. Also, the Button server control still shows me OnClick. I thought MVC was not events driven.
View 10 Replies
Jan 19, 2011
Code:
public class GroupInfo
{
private List<Structure> _sectors = new List<Structure>();
[XmlElement("Sector")]
public List<Structure> Sectors
{ get; set; }
[XmlElement("Person")]
public List<Users> Person
{ get; set; }
[code]...
View 10 Replies
May 4, 2010
I know almost nothing about coding in .NET and XML and web services.I need to call a web service and pass it a structured XML statement and catch the return from the web service.I have no idea where to start except I think I am suppose to create a new 2008 VB.NET ASP Web Service App. After that I have no clue. Here is a copy of the bindingattribute I think I need but do not know how to use it: (I think it is correct)
<WebServiceBindingAttribute(Name:="TestConnection", _
Namespace:="http://www.exchangenetwork.net/schema/header/2", _
Location:="https://naas.epacdxnode.net/xml/securityToken_v30.wsdl")>
I also have the xml statement I want to test with. I need to send it and recieve the response. I am pretty sure the web service is using SOAP and I know the XML will have to be wrapped in a SOAp wrapper but I have been told .NET will do that for me.
View 2 Replies
Jul 1, 2010
I'm all set up with Visual Developer 2010 Express and successfully (yaaah) worked my way through the NerdDinner tutorial (thank you, thank you, thank you!). So, I think I have everything set up correctly so far. I used the "Install Everything" without the prebuilt apps and all went smoothly.
However, my goal is to set up CMS for a small company owned by a relative so they can update their website as necessary. I'd love to do this with MVC.
I have tried to install both N2 CMS and Umbraco, but can't get past the admin and user names and passwords for the database. Since I'm failing with both apps, I think it's something about setting up the database server that I need to understand.Win 7 Home Premium 64, home system, not on a network. what I need to enter for admin / admin passwords and user / user passworks for N2 CMS and Umbraco?
View 2 Replies
Feb 28, 2011
I have a table where a primary key is a composite key build from columns: phone_nr and phone_ext.
How do you put a KeyAttribute in a Model in this case?
When a primary key is one column in a Model I coded:
[Key]
public string category_name { get; set; }
public string user_id { get; set; }
in the above case the column category_name is the primary key, but what to do when you have a composite primary key?
View 2 Replies
Jan 28, 2011
I'm trying to learn MVC and I'm playing around with the MvcMusicStore tutorial.I'm trying to modify the StoreController to change some of the behaviours.I'd like to change the route behaviour from:
[Code]....
View 5 Replies
Jan 25, 2011
I have controller method that looks something like this:
[Code]....
notice the commented out line. This method used to return an IEnumerable<Sport> but now that I have used a LINQ query it is returning an IEnumerable of an anonymous type (I think that's the correct terminology - please correct me if I'm wrong).
Question I have is...can I add a strongly-typed view based on this anonymous type and if not, how do I write a view that can access this collection?
View 3 Replies
Oct 10, 2010
As an ASP.NET newbie I'm trying to get an initial project going without laying out huge sums for advanced visual components. recommend any good open source control libraries for ASP.NET? Something that includes a table, tree, calendar, and the usual mix of input/display controls.
View 1 Replies
Mar 18, 2011
On a dashboard page I'm loading several controls dynamically; the controls always need to be loaded so there is no if (!Page.IsPostBack) code. The problem though is in that in the code-behind, we are using Request.Form.GetValues and a particular dropdown field's value is always null (presumably because the controls are being cleared and reloaded on a postback) and this is triggering an error later on in the page that expectes a value from this dropdown. I have to use Request.Form.GetValues because the page enables dynamically adding rows (each row containing 5+ HTML inputs) via Javascript.
If I check for postback, then the button event of the control does not fire at all but instead the dashboard page is reloaded, and no controls are loaded at all (i.e. the page is blank).
How do I get around this issue? I need to dynamically load the controls at all times, but I also need to retrieve the values via Request.Form.GetValues.
EDIT
Here's what the code looks like, roughly:
(Client-side HTML)
<table>
<tr>
<td align="center">
<select id="Class1" class="txtboxcomm" name="Class1" runat="server" />
<a href="javascript:classFinder('1')"> Look Up</a>
</td>
</tr>
</table>
<div id="action">
<asp:LinkButton ID="ibtnGetQuote" runat="server" OnClick="ibtnGetQuote_Click" CssClass="getQuote" AccessKey="G">Get Quote</asp:LinkButton>
</div>
(Server-side, in ibtnGetQuote_Click - this is actually a loop since user can append multiple rows; "i" is appended to the word "Class" to find the exact row)
IList<string> classes = this.GetFormValues("Class1");
// This will return null all the time...
private IList<string> GetFormValues(string clientID)
{
String[] values;
values = Request.Form.GetValues(clientID);
if (values == null)
{
Control ctl = FindControl(clientID); // uses built-in FindControl method
values = Request.Form.GetValues(ctl.ClientID);......
The original code was hardcoding the "ctl00$xx$xx" stuff, but the control may or may not be loaded so we cannot guarantee the name the control will get. In fact, I'm not even sure if this will work for all scenarios since only the FIRST control is a server control, the rest are just standard HTML controls so wouldn't have a "ClientID".
View 3 Replies
Feb 12, 2011
I'm inserting some non-server controls (plain html controls) dynamically into an update panel using jQuery.
If I do a full postback, I can get the values of those controls using Request.Form, however if I do a partial postback, I cannot.
Should I be able to get the values of html controls during a partial postback? Or only controls with runat="server"?
I am using non server controls as I'm playing around with facebox and file upload controls...
View 1 Replies
Sep 27, 2010
Can I dynamically create controls in Silverlight without a postback to the server (even an asynchronous one). Does silverlight drag-n-drop requires postback?
I'm asking this because I've an asp.net application where I dynamically create/delete lots of controls. So after the postback I'm getting error with view state stating that the control tree doesn't match the view state tree.
Can I avoid such problems in Silverlight?
View 4 Replies
Apr 28, 2010
I am generating an html flow into Update Panels in a content ASPX page (VB.Net 2008 Professional w/ SP1). These include EDIT anchor tags that call a javascript function which calls __doPostBack(target, args); so I can interact with the code behind. This all works fine except on some of the postbacks the connection with the browser just seems to get closed prematurely and suddenly the webpage displays a directory listing. When debugging it, it only happens sometimes, at different places for the E and D options below but not for the V or AddPost or AddArticle. I am just stepping through E or D and suddenly the web page show a directory listing. (What is that? Is the connection crashing? Is the page suddenly redirecting to the base directory because it crashed?)
[code]....
View 2 Replies
May 6, 2010
I have a C# web application that dynamically adds user controls to the default page for whatever "mode" the application is in. The problem is that it is not persisting these across postbacks. How do I persist these across postbacks and keep the content of the controls that are in the user control?
For instance, say I have a user control that creates a new tour record. The user clicks on the Tour item from the menu on the default page, it dynamically loads the tours user control. The user then fills out the form in the tours user control and clicks save. This is where the problem happens. When the postback occurs, the web application has no idea that the new tours user control was ever loaded. So, no save takes place because the Save button's click event never even gets fired.
View 11 Replies
Jun 30, 2010
I have a grid and there are several dropdowns in a row of the grid.The dropdown selection change is driving other dropdowns in the row.
I put the dddls inside UpdatePanels still dropdowns are losing the tab position after a partial update. How is this generally done?
View 1 Replies