When page is to be posted back to the server, browser collects the current values of each control and pastes it together into a string. This postback data is then sent back to the server via HTTP POST.
Q1 - Besides control’s Text attributes and SelectedIndexchanged ( thus besides user input data), are there other attributes/values of a control which are saved by a browser as postback data?
Q2 - In case of GridView, which values are saved by a browser on a postback? Only those in a row which the user chooses to edit?
byte
The values of textarea, select, input and button fields are returned in the post. Each value is a key-value pair where the key is the name property of the element.
I think that I have got all the elements that include data in the post:
textarea: The value propery is included, i.e. what's typed in the textarea.
select: The value property of the selected option is included. If the selected option doesn't have a value property specified, the text of the option is used.
input type="text": The value property is included, i.e. what's typed in the input field.
input type="password": The value property is included, i.e. what's typed in the input field.
input type="submit": If the button was used to send the form, the value property is included, i.e. the text of the button.
input type="image": If the button was used to send the form, the coordinates of the mouse click within the image is sent in the post. Names for the x and y coordinates are created by adding ".x" and ".y" to the name of the element.
input type="checkbox": If the checkbox is checked, the value property is included. If the element has no value property specified, the value "on" is used.
input type="radio": The value property is included from the selected item from each group. (A group is all radio buttons with the same name.)
input type="file": The content of the selected file is included, along with the original file path (or only the file name, depending on browser and security settings).
input type="hidden": The value property is included.
button: If the button was used to send the form, the innerText property is included, i.e. the text of the button with any html markup removed.
A TextBox control is rendered either as an input type="text", an input type="password" or a textarea, depending on the TextMode property. A DropDownList control is rendered as a select element. A Button control is rendered as an input type="submit". A CheckBox control is rendered as an input type="checkbox". And so on... check the rendered html code to see what the actual html elements rendered are.
A GridView only includes any data in the post if it contains any editable form fields, or if it causes a postback (by navigating in the list for example). When doing a postback there is some information stored in a pair of hidden fields, so any control that causes a postback but doesn't send any form data by itself (like a LinkButton for example) does include information about what caused the postback.
Controls may also put data in the ViewState, which is kept in a hidden field in the form. This is also included in the post, but it's just sent to the browser and back again without being changed by the browser.
I'm not asp programmer, so i can't give exact answer, but I'd suggest you to use firefox with addons Live Http Headers, and Firebug (console section).
With this setup you will be able to see exact data sended by browser to your server.
You'd probably want to use a ViewState decoder as well. You can get them in Browser extensions, and use ones off the web. Scott Gu recommends one here: http://weblogs.asp.net/scottgu/archive/2003/02/16/2495.aspx. The ViewState should tell you all you need to know of persisted server properties.
Related
I'm a new C# programmer and have just created a simple program. I first wanted it to count button clicks, maintaining the cumulative count across clicks (post backs). This worked.
Now I want to turn off EnableViewState and see that the clicks do NOT accumulate. So I turned off EnableViewState (EnableViewState="False") for the text box (named txtCount), but somehow it still keeps counting and accumulating my clicks across posts. Am I missing something here? How can it be maintaining the state of this text box without ViewState on?
protected void Button1_Click(Object sender, EventArgs e)
{
int count = int.Parse(txtCount.Text);
count++;
txtCount.Text = count.ToString();
}
You're not using ViewState, you're using the form element itself. Here you read from the form element:
int count = int.Parse(txtCount.Text);
And here you write back to the form element:
txtCount.Text = count.ToString();
So the value is being stored in the input that's emitted to the HTML, and posted back as part of the HTTP form post.
If you want to examine this more closely, take a look at your browser's debugging tools. (FireBug, Chrome developer tools, IE developer tools, etc.) Look for the tab/panel/etc. where the network activity is captured. When you click your button, you'll want to watch the POST request that's being sent to the server.
Within that POST request is a series of key/value pairs. Every active form element on the page sends a key/value pair, and the entire viewstate is itself just a key/value pair from a hidden form element. (Where the value is a base-64 encoded string.) If the form element has the value in question, it's in the POST request and, thus, available server-side.
Remove the text from the text box between posts and it should stop counting. (Probably even throw an exception on int.Parse().
Both really good answers above. A good way to remember it (and to realize why ViewState is required at all) is that standard HTML form elements (such as asp:TextBox which renders as an HTML input) will be included in the POST, whereas non-standard HTML form elements (such as asp:Label which renders as an HTML span) won't be so you have to use them with ViewState.
Try switching your asp:TextBox to an asp:Label. You'll see that it breaks when you turn off ViewState, but it works with ViewState on. This is because HTML span values are not included in HTML form posts.
This is a pretty good article that discusses ViewState, even though it's pretty old: http://msdn.microsoft.com/en-us/library/ms972976.aspx
You are misunderstanding the difference between post back data (Form) and ViewState.
ViewState is actually a hidden field with encoded data that is used by ASP.NET to reconstruct certain parts of the page on postback, which is why you think you need it to be turned on for your text box example to work.
While you did not show your markup, I will pretend that this is what you have:
<asp:TextBox id="txtCount" runat="server" text="Enter a number here" />
Now the user types in a number at run-time (say 7 for argument's sake) and posts the page back to the server.
The server receives two pieces of data, the post back data (7) via the Form and ViewState, which is the hidden field.
In your example, the ViewState from the text box's point of view, contains the data Enter a number here, while the Form contains the value the user actually typed into the text box.
Note: As an aside, if you look into ASP.NET MVC you will find that ViewState does not even exist. After learning some of the ASP.NET WebForms constructs, I recommend you read Compatibility of ASP.NET Web Forms and ASP.NET MVC.
I've two text boxes in one page and after those text boxes two separate search buttons, search buttons redirect to another page where user searchs and result is displayed in a grid, there can be multiple results, each row of grid contains 'select' command so that when user clicks on select of perticular row its data is selected and page redirects to previous page and data of row is displayed in textbox..
I've done so far. This is what I want to do:
Since there are two textboxes, two separate search buttons, what I am experiencing is when user selects 1st data for 1st textbox and searchs for 2nd text box then while returning for 1st page after 2nd search, 1st textbox becomes empty, What I want is that it should contain the value user has entered before.
textboxes should keep the value. If searching for 1st text box then 2nd should keep its value and if searching for 2nd text box 1st should keep its value.
I am using response.redirect method in dg_RowCommand event. I think by this page is rendering with its initial values which is what I don't want.
kindly tell me the solution I have searched so much but came up with nothing.
You can store the values in either Cookie or in a Session variable. Session variable is application specific and cookies are browser specific.
try this, for creating a session variable
Session["key"] = "your value"
or
for Cookie
Response.Cookies["key"].Value ="your value"
on the another page check first is it not null
if(Session["key"]!=null)
// use it
and same for the cookie (access it with Request object)
if (Request.Cookies["key"] != null)
// use it
use Session variable. Session["YourKey"] = your value; You can access it cross page
Update:
If I am right then you are thinking of some wizard type interface. If you can take benefit of ASP.Net Wizard control then look into this. It might help you.
Combine the pages into one, with each page(step) becoming a div or asp:Panel. hide/show divs rather than "navigate to the next page" on Click_Next event (or whatever). This way all controls stay on the same page and maintain all values when the divs are shown/hidden. No Session or other methods needed.
If you are redirecting with the Response.Redirect method in the C# Code then before you execute this redirect you can store the items in Session variables:
Session["Test1"] = test1.Text;
Sessions are maintained per user. These will be retained for as long as the application is running, but be warned that if an Application timeout occurs or the user closes the application (closes browser) these values will be lost.
I have an ASP.NET web app with a dropdown list and a number of ASP.NET textboxes/labels. when the user chooses a particular item from the dropdownlist the labels change text and some of them are hidden etc using jquery. However after clicking on a button the controls are being changed back as when the page is first loaded.
Please suggest
How i Can preserve the new labels name etc?
Should i put the same jquery code i wrote for the dropdownlist on change event also in the on document load event?
Should i use hidden fields to store the value?
1: How i Can preserve the new labels name etc?
Yes,
You can use hidden field to store.
2: Should i put the same jquery code i wrote for the dropdownlist on change event also in the on document load event?
No,
On page_load access hidden field, extract and use values.
3: Should i use hidden fields to store the value?
Yes,
You can use hidden field to store changes you made in GUI control using jQuery. Make that hidden field server accessible by making it runat server. On server end when you get postback access the hidden field, extract and assign values to respective controls. You can see how it would work in this post.
As you might be knowing , after clicking the button if it is submit type it will do the postback to the server , as HTTP is stateless , it is your responsbility to generate the label text and value again. Generally ASP.NET viewstate take care of doing so for input texts, but for labels you have to do by yourself. I can suggest the solution like this
1) Have a javascript method which will have the logic to change the value of label and hidden field based on the selected value of the dropdown
function MyFunction ( ddlSelectedValue)
{
if(ddlSelectedValue == "1")
{
$("#LABELID").html('YOUR VALUE');
// rest other logics
}
}
2) ASP.NET viewstate will help you to preserve the dropdown state after button click . So on add this jquery script on the page top
$(document).ready(function(){
var ddlSelectedValue = $("#DDLID").val();
MyFunction(ddlSelectedValue);
});
3) attach the the point 1 method to onchange of the dropdown also.
4) Using the hidden field is Ok , because hidden fields will serve your requirement in using in the server side as they will also be posted back.
I am trying to figure out how I would setup a group of images to work like radio buttons. Basically when a user clicks a image it wil display a check mark below to indicate it has been selected. I could accomplish showing the check mark I believe by a onclick event and some css. But what would be the best way to do this with ASP.net I also need to transmit the data to the server via a form.
I would populate a hidden field with the resulting value of the selection via client-side code. Then you just read that value in your code behind on submit.
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.