viewstate and textbox in asp.net - asp.net

How is a TextBox able to persist changes (e.g. text) even after the EnableViewState property is set to false in ASP.NET?

Because the ASP.NET Textbox control generates an HTML form input element <input type="text" name="x" />. You can verify this by looking at the source view from your browser on an ASP.NET page. When the form is posted, ASP.NET is able to read the text value from the HTTP POST contents. You can read more about it here and here.

Related

Can client-side generated controls affect ViewState?

I have a page that generates new text inputs, checkboxes, selects with JavaScript. So none of these controls has runat="server" set.
I would like to know if these controls are sent to the server on PostBack and become part of the Viewstate, altering it in any way.
The short answer is yes, by assignment.
Dynamically generated plain HTML elements in ASPX page will be treated as LiteralControl elements rather than WebControl elements, hence they don't affect ViewState directly like some ASP .NET server controls. However, since they're placed inside form tag (usually with runat="server" attribute), their values are posted together as postback event stage triggered by submitting the form, which identified as key-value pair in Request.Form collection (the key is recognized by name attribute of literal HTML element).
Suppose you have dynamically generated text box with JS like this:
<input name="FirstName" type="text" />
Then you can retrieve its value using Request.Form:
If Not String.IsNullOrEmpty(Request.Form("FirstName")) Then
Dim firstName As String = Request.Form("FirstName")
And, the important point, you can omit the string assignment above and assign literal text box value to ViewState:
ViewState("FirstName") = Request.Form("FirstName").ToString()
Note that only HTML server controls (e.g. <input runat="server" />) and ASP .NET server controls have control name directly accessible in code-behind and ViewState maintained automatically (unless having EnableViewState attribute set to false).
Additional ViewState reference:
Understanding ASP.NET View State
Related issues:
How to viewstate in normal HTML input in asp.net
Which controls have ViewState maintained?
viewstate for HTML control

using classic asp code inside asp.net?

I have a application that running both classic asp and asp net. inside the classic asp page, I have a combo box and depends on the selection of the combo box I need to do something inside my asp.net page. For instance, if inside my classic asp page, I have a combo box and inside the combo box; book is selected than when I enter a price for book as a zero inside my asp.net page I supposed to get an alert. Is there any way to do that?
asp.net code
if (Convert.ToDecimal(Values["myBookPrice"]) == 0)
{
//You cannot use 0 price for books!
}
Let say that you have some input control on any page, html, asp what ever, with some parametre that you wish to pass to an asp.net page
if the input control have a name attribute like
<form method="post" action="thenewpage.aspx">
<input name="nameOfInput" type="text" id="idofinput" />
<input type="submit" name="btnGo" value="Create Box" id="btnGo" />
</form>
and you have a button that make post back to an asp.net page, then you get that parametre on asp.net side using the Request.Form and the name of the input control as:
Request.Form["nameOfInput"]
If you make a call the page with url parameter, you need to use some kind of javascript to dynamically create the URL string for the call.
The issue that you have here, is that the asp.net page will hit the validation of the page security because is not going to find the page validation informations he needed to secure the page.

How to get access to the input of textbox at aspx web page?

I am a starter for asp .net. On my webpage, there are a few textboxes and a submit botton. Is there a easy way to get access the data and use it to built an object ? The textboxes has there names and ids, there should be a way to get access them by names and ids.
Edit
this is normal html control
<input type="text" class="text-box" id=xxx />
if you want to access it in your codebehind file you need to add runat=server attribute to this
<asp:textbox>
Other one is server side control asp.net control not html control
I think its better you read out basic of asp.net before stating programming because this is very basic question you should know about
prev
step 1:
make use of asp.net Textbox
<asp:Textbox id="textbox1" runat="server"></asp:Textbox>
Step 2:
for asp.net you just need to write in you codebehind file
object.proertyname = texboxid.Text;
Read more : TextBox Class

Why are hidden fields considered client side state management?

According to MSDN and the MCTS self-paced training, asp.net can use Hidden fields for client-side state management. The book material goes on to say view-state is more secure than hidden fields because the data is encrypted.
I must be missing something here. I setup a Label and made it hidden. I can store data in this hidden label and it won't even be sent to the client browser. This not only works like server side state (note the runat=server), but this seems more secure than view-state because there's no need for encryption as the client can't even see the field.
<asp:Label ID="Label1" Visible="false" runat="server">secret info</asp:Label>
Contrast this with an HTML input field. Here, the client state info makes sense.
<input id="Text2" type="text" style="visibility:hidden;" value="secret 99" />
So what's the deal?
When you create a label in .net and set it's visibility to Hidden, it does not render to the client and its data is stored in viewstate.
Therefore, it is not "more" secure than viewstate as it is using viewstate to maintain the data.
Regarding hidden fields, there are four kinds: First up is the regular HTML one which is simply an input of type hidden. This has no visible rendering although it is in the html. It also has no viewstate properties. It is declared as:
<input id="MyId" type='hidden' value='whatever' />
The second one is a regular input with a css property marking it as hidden: If CSS is disabled or otherwise overriden then the control would be visible to the user. Other than that its pretty close to the same thing as a type='hidden'.
<input id='MyId' type='text' value='whatever' style='visibility:hidden' />
The third one is the .Net hidden field. This does has viewstate storage, but it also causes a regular hidden field to be generated in the html.
<asp:HiddenField id='MyId' runat='server' value='whatever' />
And, the fourth one is a regular .net text box that is marked as not-visible.
<asp:TextBox id='MyId' runat='server' Text='whatever' Visible='False' />
The .net ones will cause data to be placed in viewstate. The HTML ones do not. If you set Visible=False on a .Net control then it is not rendered to the client however it's data is typically stored in viewstate.
There are other ways of throwing data into the page, but they are derivations of the above.
Generally speaking if you have a value that your javascript code needs but you don't need to display it to the client then you use a hidden field (html or .net). If you have a secret value then typically you don't want this to go to the client side if at all possible. And that means even keeping it out of viewstate. As a side note, don't depend on viewstate "security' there are tools out there which will easily decrypt it.
A field which is not displayed is not a hidden field (even though it is "hidden").
Hidden fields are <input type="hidden" name="somename" value="somevalue" /> fields. And those can be manipulated by users.

When does an HTML input tag post back on enter?

I have a couple of asp.net pages with TextBox controls on them. Some of them postback on enter (within a TextBox control) and others don't. For the life of me, I can't figure out why or why not...
I don't think this has anything to do with asp.net. Not using ajax etc. Tried playing with a very simple html page and still can't figure it out.
Maybe within or not within a <form> tag? But all my asp:textbox controls are within the standard form tag and some do and some don't. Not related to autoPostback from what I can see.
EDIT:
Sample markup which causes a post back:
<html>
<body>
<form>
<input type=text />
</form>
</body>
</html>
I have asp.net pages that definately don't post back. The code is extensive and I can't figure out why not.
EDIT2:
How can I prevent a postback?
If you only have one textbox and don't wan't this behavior, you could always put a hidden input on the form.
<html>
<body>
<form>
<input type="text" ></input>
<input type="text" style="visibility:hidden"></input>
</form>
</body>
</html>
I've tried this in IE7 and FF3 and it works.
Generally most web browsers will detect when a form has only one textbox and will automatically submit the form when enter is pressed.
If you have multiple text boxes or multiple form buttoms, the behaviour varies. ASP.Net has the "defaultButton" mechanism for buttons, meaning you can specify which button is the submit button for a given panel in the case of the enter key being pressed. You could check out this post for more information on setting up your forms to correctly post back.
As Boo mentioned, this won't be triggered by multi-line textboxes.
If the behaviour is really crappy in your form between browsers, you may need to look at a javascript solution to catch the enter key being pressed, and "manually" submitting the form via javascript.
If the textbox control is set to autopostback it will postback on every keypress (thus allowing the firing of the TextChanged event).
Next, text boxes that have their Multiline property set to false will appear to postback on Enter.. what is actually happening is a form submit as defined by the behaviour within the browser.
Boxes that have Multiline set to true will not cause a form submit on Enter.

Resources