Get value from input html in codebehind c#

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

Why not use a server control?

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

Code behind:

var nickName = txtNickname.Text;

Method 3

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

They’re running on the server, see this

Method 4

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

Method 5

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] + ""); 
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x