8

I did some research and found out how I can read a value from the input html textbox.

This worked fine for me, but at once it doesn't work.

This is my code, it input html returns null

<input type="text" name="inpNickname" placeholder="Nickname" data-placement="right" data-toggle="tooltip" title="Nickname" id="txtNickname" runat="server"/>

<input type="text" name="inpPassword" placeholder="Password" data-placement="right" data-toggle="tooltip" title="Password" id="txtPassword" runat="server"/>

string Nickname = Request.Form["inpNickname"];
string Password = Request.Form["inpPassword"];

If I change the Request.Form[] to the ID's, it still doesn't work.

3
  • Did you try getting it by the ID? Commented Jul 17, 2013 at 15:01
  • Yes, that also doesn't work Commented Jul 17, 2013 at 15:01
  • 1
    What do you mean by "doesn't work"? The strings are staying null? Commented Jul 17, 2013 at 15:07

5 Answers 5

26

Since it is running at the server...

txtNickname.Value and txtPassword.Value will give you what you need.

When you specify runat="server" you are essentially giving a property to your codebehind class. So you can access that property and it's properties directly.

Sign up to request clarification or add additional context in comments.

5 Comments

When I tryt this, I can't put .Text after txtNickname and txtPassword
use txtNickname.Value instead.
I would recommend using ASP.NET controls (i.e. <asp:TextBox .. />), it'll make your life easier in the long run
@SamHood I would agree to an extent. Using server controls is a good choice if you're bound to asp.net web forms. I would highly recommend using a better platform like mvc though to really make life easier :) ... but that wasn't the original question.
I agree with both Sam Hood and Bluetoft, if you want to work closer to HTML then use ASP.NET MVC, but I also agree that dressing up HTML controls with runat="server" is asking for trouble, especially anyone coming after to maintain your code and you will also be able to fully leverage the Intellisense of Visual Studio.
5

Why not use a server control?

<asp:TextBox ID="txtNickname" runat="server" />

Code behind:

var nickName = txtNickname.Text;

Comments

4
string Nickname = txtNickname.Text;
string Password = txtPassword.Text;

They're running on the server, see this

Comments

1

Using name="inpNickname" doesn't work, only use the ID so in this case: txtNickname

Comments

0

The standard approach:

Frontend :

 <asp:TextBox runat="server" id="testid"></asp:TextBox>

Backend :

String test = testid.text;

Usually i use these simple server controls, but with complicated forms, and post backs, things are not always so simple... I've found another surefire way to get the clients control from the back end, even when the control is not found in the code-behind by using thecontrol.text or thecontrol.value for example :

Backend :

String test = Request.Form["ctl00$MainContent$TextboxControlsId"].ToString();

This will fetch the contents of the textbox with id: TextboxControlsId, which is defined outside of sitemaster.

If you need to find the ID of your control (most likely it wont conserve the same id you wrote in the front end), you can loop through your form control keys by using :

foreach (string s in Request.Form.Keys ) { 
    Response.Write(s.ToString() + ":" + Request.Form[s] + ""); 
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.