Several question : postback, focus

C

Christian Ista

Hello,

I have 2 questions :

1. On an ASP.NET page I have several controls (5 TextBox, 1 Dropdown and 1
button)
Only the dropdown is AutoPostBack = true, the TextBox are SingleLine

When I execute the page, I fill in the textbox, I change the dropdown
selection, the page is reloaded no problem I see the textbox still fill in.

Now I change TextMode to Password for 2 TextBox. I execute the page, I fill
in, I change the Dropdown selection when the page is reloaded the TextBox
with TextMode to password are empty.

Is it normal ? How can I solve that ?

2. How can I give the focus to the first textbox of the page ? For the
moment I use a javascript in the bottom of the page but that's not work very
well mainly when I use the dropdown the focus is on the first control.

Thanks,

C.
 
G

Guest

When a textbox's textmode property is set to password the viewstate for the textbox does not work. The reason for this is that if the user views the source of the HTML rendered after the postback, he/she will see the password in plain text. This defeats the purpose of having a masked password textbox. One way to circumvent the problem is to keep the password in memory on the server, or to use other means other than viewstate.

A technique that I've seen which executes javascript after a page loads, so that it can reference the controls, is to put a literal control at the very bottom of the page, and fill it with the javascript code which sets the focus. Another way would be to put it in the body onload event, which executes only when the controls on the page are loaded.

hope this helps,
John
 
C

Christian Ista

When a textbox's textmode property is set to password the viewstate for the
textbox does not work. The >reason for this is that if the user views the
source of the HTML rendered after the postback, he/she will see
the password in plain text. This defeats the purpose of having a masked
password textbox. One way to >circumvent the problem is to keep the password
in memory on the server, or to use other means other than >viewstate.

What do you say by "memory on the server" ? You talk about a Session
variable ?

I tried this kind of solution, I wanted fill in the textbox in Page_Load
event but at this time the control is not neated yet and I receive an error.
Is there an another event where I can do that ?

Thanks,
 
T

Tommy

Question 1:
For security reasons, it is by design that textbox fields with
type="password" are not cached by the browser, and ASP.NET does not
preserve the value of a password textbox in the viewstate. This is a
normal behavior.

You can ask the user to enter the password again, or maybe put the
password textbox on another page, and only bring the user to the
password page when the user has entered all the informations
correctly.

Question 2:
I don't fully understand this question. Could you provide more
details?

I had the following at the end of the page, and it works fine.
<script language="javascript">
document.getElementById('txtInput').focus();
</script>

One thing I want to point out is that the asp.net dropdownlist only
autopostback if the user has selected a different item. If you only
have one item in the dropdownlist, it'll never postback to the page.

Tommy,
 
G

Guest

Yes Session object is one, but not so secure as it can be viable to cross site scripting attacks since it uses cookies. If you store Session state in SQL Server though it would work great. Or you can store the info in a variable declared globally to the page.

For your second question, I did not mean the Page_Load, but the <body onload=>. To do this, you will have to go directly on the HTML page and put in the code, you cannot do this is codebehind. If you are using Visual Studio .NET you have to switch to the HTML view, find the body tag and enter the javascript that puts focus to the textbox there. This event occurs when all the controls are loaded into memory on the client-side. If you put the code at the end of the page, it will work most of the time, but you are not guaranteed that the textbox where you want to put focus to will be loaded. This is the only surefire technique.

hope this helps,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top