i wanna to understand difference between viewsate["x"] and __viewstate in view source
ie
in view source page there is view state in hidden control like:
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="dO3F9exemRdHoXxGHr"/>
but in c# i can create viewstate like:
viewstate["x"]="Hi";
so what is the difference???
and the string "Hi" where it be saved ? in this hidden or where?
When a page is rendered, it serializes its view state into a base-64
encoded string using the LosFormatter class and (by default) stores it
in a hidden form field. On postback, the hidden form field is
retrieved and deserialized back into the view state's object
representation, which is then used to restore the state of the
controls in the control hierarchy.
That means yes it saved in this hidden field, but it is encoded. Read MSDN article for more information. This quote is from 6.PARSING THE VIEW STATE.
If you have any interest you can parse the ViewState and see his "real values". You can search for view state parser, after research I found this Website
ViewState is a storage method on server side that is persisted on client side. What you're seeing is the two faces of the same coin
You use it on server side doing something like this
Viewstate["x"]="Hi";
And when the response is sent to the client, the ViewState storage is serialized and sent to the client in the form of an input field
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="dO3F9exemRdHoXxGHr"/>
When you set view state using ViewState["x"], it is stored in the hidden field __VIEWSTATE. It is first encoded, however, so you won't see "Hi" in that hidden field.
Related
I have a textbox set-up, like so:
#Html.TextBoxFor(m => m.NewVehCode, new {data_id = "" })
When a user selects a vehicle, the description of the vehicle becomes the value of the textbox and the vehicle code (which is what I want to send to DB) becomes the data-id (not full code but I set the values like this):
$("#NewVehCode").val(vehDescription);
$("#NewVehCode").attr("data-id", vehCode);
This all works fine, except for the fact that when I submit, MVC grabs the value of the textbox.
Is there a way I can on submit, get the data-id of that textbox instead of the value?
Note that I'm not using .js to gather the data. Form calls a controller action that sends the model directly to the controller.
There is no way to submit values of "data" properties. I am not sure why you wanted to use data property (is that a requirement or not, not sure). But you can have a hidden property in the form so, when user selects a vehicle, along with data property of text box , update this hidden value. This hidden value will be submitted back to form.
Stephen Muecke's comment helped me trigger this solution:
I've added a hidden textbox to use for the vehicle code, and made the textbox that shows the description a standard textbox with no data binding.
<input id="newVehInput" readonly="readonly" class="longInput" type="text">
#Html.TextBoxFor(m => m.NewVehCode, new { style = "display: none"})
This way I can use the value with no issues:
$("#newVehInput").val(vehDescription);
$("#NewVehCode").val(vehCode);
(Will mark as answer in 2 days)
Add a hidden textbox to your html which you post back on submit
In your model:
public string Id {get;set;}
Render it in the view with a hidden class
And with js.
$("#vehicleList").on("change",function(){
$("#Id").val($vehCode)
})
When you submit the value of id should be set in your model
You can have a hidden field hold the Updated value.
HTML:
<input type="hidden" id="HiddenVehCode" name="HiddenVehCode" Value="0"/>
JS
$("#HiddenVehCode").val(vehDescription);
Note: you should have the model set up so that HiddenVehCode will reach the actionMethod on Form Submit.Something Like,
public int HiddenVehCode {get;set;}
How to maintain a value assigned to a label through javascript after postback?
Problem: I have assigned a label value using a clientside function.
But whenever postback happened, label values are gone.
Solution i found: After searching a lot, all are suggessting to store and retrieve
the value to & from a hidden field.
Note: But i want to achieve this without using hidden field as it may
increase pageload time.
The state of label is not maintained in ViewState by asp.net. The labels are converted in to spans and the html of span is not posted on submitting form, this is why changes made by client are not persisted. You can put the state of label in some hidden field when you change it in javascript and access it on server.
HTML
<input id="hdnLabelState" type="hidden" runat="server" >
Javascript
document.getElementById('<%= hdnLabelState.ClientID %>').value = "changed value of span";
Server side (code behind)
string changedLabelValue = hdnLabelState.Value;
use html hidden field
<input type="hidden" runat="server" id="hiddenlabel">
try an html label, like
<label id="lbl" runat="server">Myv alue</label>
I have a generic 'form page' user control that we use that allows editors to insert whatever kind of html form they want to inside of it, and it handles all of the form posts.
My question is, is there a way to store the vanilla non-asp form entries in the viewstate or otherwise save the entries on a form post, in the case that some server-side validation fails, so they can be restored when the page refreshes?
We currently already do upfront javascript validation that catches the majority of the input errors. We store all the form post data in a db before we do further processing, and some of the entries contain junk (spam we wish to ignore) or only partial info (i'm assuming those are cases where the user doesn't have javascript enabled). I'm trying to catch these last fringe cases so we do not process them.
If I am understanding this correctly,
1>User fills form
2>Clicks submit
3>Error detected on the server side
4>The Html posted back should contain the form i already filled with an error message on top.
Have you considered using JQuery Ajax?
The jquery ajax will post to a web method. The web method returns a JSON response. If the response is success redirect user to the next page, else show error on top of the page.
That way you don't need to maintain the state of the user input (since it is never lost).
If you dont know what the form fields are ahead of time then I would reccomend that you look into partial postbacks.
Or else post the forms via ajax.
you could store the text HTML in
<asp:HiddenField ID="hid1" runat="server" />
this will be passed in View State
I dont know whether this simple task or not, but I tried to search in google but couldn't find anything.
I've a asp.net form and user enters some data in the text boxes provided. Whenever user submits the form, browser will save that form data. I don't want this form data to be saved in browser. How can I restrict the browser saving this form data without touching the browser settings?
Application is developed using asp.net and normal text boxes are used here.
I'm guessing you mean you want the browser to stop remembering values entered, i.e. the browser's autocomplete?
<input type="text" name="text1" autocomplete="off">
Or this will work in FF and IE, but it's not XHTML standard:
<form name="form1" id="form1" autocomplete="off" />
Note that autocomplete is only defined in the HTML 5 standards, so it will break any validations you run against HTML 4.
If not, on PostBack, just clear the inputs:
TextBox.Text = string.Empty;
If you're meaning an asp:TextBox with "[...]normal text boxes[...]", then the control's ControlState is responsible for saving the entered value. Unfortunately, you can't disable the ControlState (MSDN).
If I see something like:
if(Request["Email"])
{
}
What does this actually mean? Where is the Email collection member actually being set?
It's retrieving the variable from get/post parameters.
somepage.aspx?blah=1
string blahValue = Request["blah"];
Console.WriteLine(blahValue);
> 1
Even more specificially:
Cookies, Form, QueryString or ServerVariables
http://msdn.microsoft.com/en-us/library/system.web.httprequest_members(VS.71).aspx
See this for example.
Taken from the above link
All variables can be accessed directly by calling Request(variable) without the collection name. In this case, the Web server searches the collections in the following order:
QueryString
Form
Cookies
ClientCertificate
ServerVariables
It retrieves either the submited form values (POST) or the submitted querystring values (GET).
You would generally see it written as either Request.Form["Email"] or Request.Querystring[Email"] instead of just Request["Email"].
Example of Form (POST) method:
On the HTML or ASPX Page:
<form action="SomePage.aspx">
<input type="hidden" name="Email" value="someaddress#email.com" />
<input type="Submit" value="Submit Form" />
</form>
Once the form has been submitted by clicking the Submit Form button you would retrieve the form values using Request.Form["Email"] (or just Request["Email"] for the lazy :))
Just some additions to the posts of the others.
To have things more explicitly you normally use Request.QueryString[...] for getting values from the QueryString, so when a GET request has been done and Request.Form[...] when a POST request is done. Although in the latter case you usually directly access the values of your server controls, since ASP.net uses the ViewState mechanism to load back on your controls when the request comes back from the client.