I am using asp.net 2.0
I set a hidden text box from javascript and on postback, i want to check the value stored on the text box in page init event. Here is what i tried
string t = Request.Form["currentMode"].ToString();
but i get an error saying " Object reference not set to an instance of an object."
Any idea?asp.
In init the postback values have not been loaded into the controls yet, you could in theory fish the values from Request.Form variables, but they are named as the client ID of the controls. If your controls are in any container like a contentplaceholder or detailsview they will have various guff prepended on the ID. e.g. ctl00$cphContentBody$txtMyTextbox could be the id of a control that has a server side id of txtMyTextbox.
Init is the page life cycle where everything is initialized. You can't thrust that anything is availble. In order to do that you have to use the Page_Load event...
I came across this the other day while trying to build->submit->retrieve values from a dynamically created form (assembled during the Init() event of a postback). As Ben stated, you can use the Request.Form collection to pull out the values of the page's controls manually, using the UniqueID of the desired control (which will contain the "guff" Ben is talking about).
Of course, this is only necessary when trying to discover the value of a page's control before it is populated from viewstate (before LoadComplete). Otherwise, you could simply do something like TextBox1.Text.
Related
This subject seems to be be beaten to death and yet I still can't find an answer.
I am trying to allow users to click a button to add a usercontrol to a page. Hypothetically, they can do this as many times as they want. I know I need to recreate these controls on Postback, and am trying to do this in Page Init so that viewstate can give them back their values in page load.
My problem is I have no idea how to keep a running counter that I can use in Init to recreate the proper number of controls the user has put on the page. Viewstate is not available in Init so I can't just use a hidden field where I could increment or decrement the value. I also cannot use Session variables. I am not allowed to use them in this particular project.
As an FYI, I'm adding and deleting these usercontrols successfully right now when I do it on Page Load. But, the fields within the usercontrol do not retain their values consistently between postbacks.
You can always keep this count in the client, and add it to the URL for instance.
OK, got pretty far on this one, but need an assist from someone with superior ASP skills. I'm using code behind to populate an ASP table with the results of a SQL query. The read-only values are stored in the .text of some of the table cells, while read-write values are stored in .text of textbox controls (dynamically created and added to table cells.)
This works fine on the first load. When the page reloads with a different query (for example: a user selects a different column to order by,) the table cell values repopulate correctly, while the textbox values remain unchanged. Throwing in a table.rows.clear() prior to the query does not seem to fix this.
More info:
I created a method to wipe all textbox.text values using table.findcontrol(). When tied to a button, this method works to spec (which indicates findcontrol is able to find/update the textboxes,) though all affected textboxes remain the blank if the page is reloaded. If placed in the page load, the method does nothing (textboxes retain their former values.) In debug mode, findcontrol pulls a value when used on the button, but comes up null when added to pageload. I have done this with table.rows.clear() commented and uncommented.
I have also attempted to throw all the code into oninit. This doesn't seem to make any appreciable difference.
Dynamic controls must be added on each page request, preferably during the Init event. It sounds like you are not recreating them on time.
Add them on every single page request and, as part of the page life cycle, they will receive their values from the viewstate
In order for dynamic controls to retain their values you need to:
a.) Ensure Viewstate is enabled for the page.
b.) Recreate the controls on every page load ensuring you create the controls with the same IDs as were given to the controls originally. Also you need to do this before the viewstate is loaded (preferbly in the Onit or PreInit method. See the page life cycle here: http://msdn.microsoft.com/en-us/library/ms178472%28v=vs.100%29.aspx
Hy,
I have created some dynamic textboxes with standard content.
Does anyone know how can I read the content of these textboxes (assuming that user modified the standard content) when I press one button?
Thanks a lot.
Jeff
Update
This is how I am creating the textboxes:
foreach (string name in listOfNames)
{
TextBox tb = new TextBox();
tb.Text = name;
tb.BorderStyle = BorderStyle.None;
tb.BorderWidth = 0;
tb.Font.Name = "Arial";
tb.Font.Size = 8;
}
The specific will vary depending on the technology you are using. However the concept would remain very similar, though for ASP.NET it will be a little more interesting.
WinForms/WPF/Silverlight
Maintain a list of the dynamically created textboxes and when the button is pressed you can run through the list of textboxes and read the Text property to get the user input.
ASP.NET - After the tag update it seems this section is most appropriate to your requirement.
For ASP.NET you will need to create the textboxes in an override of the OnInit method, this should happen on each postback. Then in the Button.Click event you can read the user input from the textboxes that you created in the OnInit function. You need to ensure that the controls are created with the same ID on each post back.
You need to ensure that the text boxes are recreated on every postback.
If you do not recreate them, you will not be able to access their properties or events.
The best place to create dynamic controls is the page Init event handler.
I suggest reading up on the ASP.NET page life cycle.
Update (following updated question)
Make sure to set an ID (and a different one, at that) for the text boxes, so you can refer to them later on.
I can't see where you are adding these controls to the page either.
Request.Form is a collection of Key-Value pairs that represents all of the data coming back from the ASP.NET request. If you access that you can get any value whether its control is specified in the ASPX code or dynamically created in the code behind file.
You can also get their values placed back in them automatically if you recreate them during the init part of the page lifecycle. If you do this, their values will be set when ASP.NET recreates the state of the page and applies the values from the form.
You should be able to access them as you would a normal control, you just need to get a reference to the control.
Remember to re-create all controls on each postback.
this is a useful article on dynamic controls in asp.net
Scratch this!
I have googled my ass off looking for this.
1. Lets say that i have a webform that has a few textboxes, with ID's textbox1, textbox2, textbox3. They all have viewstate enabled.
I put some values in these textboxes and push a empty postback button and all the values are still there after postback. How can i access them in the viewstate after postback ?
I would think that they were saved under the ID name of the textboxes but i dont get it to work like so.
String s = ViewState["textbox1"].ToString();
I'm trying to get this to work because I want to be able to save the viewstate into the session so i can retrieve the info after i visit another webform.
2. Isn't it right that i can only use the viewstate on the same page that it was made on ?
I could not use the viewstate on default.aspx in editor.aspx ?
3. And one more thing, isnt it right that the viewstate saves how a treeview nodes are expended ? I would like save the state on the treeview between two webforms that use the same masterpage.
EDIT:
Ok, this wasn't clear enough, thats a given.
Basicly i'm trying to understand the viewstate and what i can do with it.
I dont usually use viewstate to store values.
What i'm trying to do, or figure out if its possible with viewstate.
I have a masterpage and on the masterpage is a treeview. I have two pages that i use with the masterpage, Default.aspx and editor.aspx.
I do my navigations and everything in the Default.aspx. When i have expanded the nodes in the treeview and selected one of the treenode, the navigateurl on that treenode send me to editor.aspx?navID=3. The editor.aspx uses the same masterpage and i want that page to show the SAME state on the treeview as the Default.aspx did before i clicked on the node.
Take a look at this article to learn more about viewstate. I found it helpful
Truly understanding viewstate
The reason your code does not work is because ASP.NET uses a different name (I think it prefixes the control name with the form name and the master page name , if there is one). But even if you could pull it using that method, you shouldn't. You should manually add a property yourself to the viewstate. So if your trying to preserve the text in a text box, use the following code:
ViewState["TextBoxText"] = textbox1.ToString();
And to retreive this later, use:
String s = (String)ViewState["TextBoxText"];
To answer your questions:
You are right. The viewstates are sacred to each individual page and cannot be accessed
Treeview will automatically save the expanded nodes. Just make sure you are doing your initialzation to the treeview inside a if (!Page.IsPostBack) block.
The Viewstate collection in System.Web.UI.Control only allows you to access the viewstate bag for that control, not child controls. So basically you can't do what you want to do through ViewState.
You can get the values that a control posted through the Request.Form parameters. For example, if you have a control call textbox1 you could get its posted value through
Request.Form["textbox1"]
Depending on the control you may have to do some processing on the value you get out of there. For a treeview you can get the posted value of its expanded state using
Request.Form[TreeView1.ClientID + "_ExpandState"]
The value is a string with either an e (expanded) or an n (not expanded) for each node. So if the value was "eennene", nodes 1 2 5 and 7 would be expanded while the others would not be
I have a button that adds an already defined usercontrol programtically. I am rebuilding each control on Postback and they show up fine. If I put text in the textbox it shows up fine, however my images are losing their url. Any idea why or how I can fix this?
I tried adding AJAX updatepanel to see if that would help, but it does not.
To further explain - I have a button that after clicks set the url of the image - I also put this value in the textbox just to see if the same thing happens - after postback, my textbox still has the value, but the image does not - and all of my other ASP.Net images lose their image and they are defined right on the controls (ie: ImageUrl)
--- update
OK, I have found some more insight to my issue - some solutions but still one problem remains.
The reason the images were loosing their url is because they were and not ASP.Net images - ASP.Net remembers the values when they are recreated on postback - however the Main image that im changing via javascript looses its value - I'm very certain this is because of using javascript to change it, on post back it reverts back to the previous value... so for a solution I will try to stuff it in a hidden value, then use that value on postback to define the ImageUrl of the image...
You are correct <img> is an html control, while <asp:Image> is a server control. As long as server controls have viewstate enabled they will keep their values during postbacks.
You are correct that changing an image's URL via JavaScript will not get it returned during the postback as this is not a form value. Therefore, it will be lost forever if you do not save it into a form field, such as the hidden field that you suggested.
Image is not form field. That is why it's value is never posted back to server.
The reason your asp:Image controls retain value during postback is because they are stored in viewstate (which is stored in hidden field). When you click button this viewstate is posted back to server, and asp.net sets the ImageUrl property of image control from viewstate in early events of page lifecycle.
In short the server side form controls values are available after postback. Same way if you have normal html form fields (https://www.w3schools.com/html/html_form_elements.asp), you will get it's value using Request.Form collection.
You are setting your Image's src using javascript (and which is obviously not in original viewstate), that is why you are not getting it's value in postback. You should use Hidden Field to set value using javascript, which can be available after postback.