2

I'm writing in asp.net. When I add runat="server" property to my input tag, its value is null. When I remove runat="server" it works correctly. Who knows the reason?

I want change its property from code behind, that's why I wrote runat="server". However, the value is null.

protected void btnSaveChanges_Click(object sender, EventArgs e)
{
    string current_id = Session["Current_user"].ToString(); 
    string a = Request.Form["newusername"]; 
    string b = Request.Form["newpassword"]; 
    string c = Request.Form["rewpassword"];
}

Code for control:

<input type="text" name="newusername" placeholder="Enter Username" required="required" runat="server"/>
Andrew
  • 307
  • 7
  • 13
fico
  • 9
  • 2
  • 8
  • Its value is null but exactly when ? can you please provide some code where its value is coming as null. – Sagar Shirke Dec 14 '18 at 06:56
  • protected void btnSaveChanges_Click(object sender, EventArgs e) { string current_id = Session["Current_user"].ToString(); string a = Request.Form["newusername"]; string b = Request.Form["newpassword"]; string c = Request.Form["rewpassword"];} – fico Dec 14 '18 at 06:58
  • 2
    `runat="server"` indicates that the input will exist in server-side code, not using `Request.Form` anymore. If you want to use its value just do `newusername.Text`, assumed you give `id="newusername"` attribute. – Tetsuya Yamamoto Dec 14 '18 at 07:02
  • its value isn't null. But I add `runat = "server"` its value returns null. – fico Dec 14 '18 at 07:03
  • Thanks, @TetsuyaYamamoto it works.But it has not Text property.`newusername.Value` works – fico Dec 14 '18 at 07:13
  • @fico - when you have updates to your question, please [edit] it into your question. Especially when it contains code as that is hard to format or read in a comment. – Hans Kesting Dec 14 '18 at 07:16

3 Answers3

1

When you add runat="server" and make the simple HTML control to asp.net HTML control, then asp.net renders the id and the name of that control in a manner that does not conflict with other asp.net controls on the same page.

So change the input to: (note now I add id, and remove the name!)

<input type="text" id="newusername" placeholder="Enter Username" required="required" runat="server"/>

and get the value using the post like this:

Request.Form[newusername.UniqueID]

or using the value:

newusername.value

other links to consider: Accessing control client name and not ID in ASP.NET

Andrew
  • 307
  • 7
  • 13
Aristos
  • 66,005
  • 16
  • 114
  • 150
0

Use newusername.Value to access the value of the control in your server side function.

Like

protected void btnSaveChanges_Click(object sender, EventArgs e)
    {
        string current_id = Session["Current_user"].ToString();
        string a = newusername.Value;
        string b = newpassword.Value;
        string c = rewpassword.Value;
    }
Sagar Shirke
  • 648
  • 9
  • 32
-2

You can't change an HTML control's properties using c# server side code. But you can do that using several other methods -

Method 1 you can use plain javascript or jquery to alter the HTML DOM element.

Method 2 you can create the HTML elements you want dynamically using HtmlGenericControl.

HtmlGenericControl username = new HtmlGenericControl("input");
username.attr("type", "text");
username.attr("name", "newusername");

And then you can append the controls into some div, like,

Front-End HTML code where you will add your dynamic controls-

<div id="dynaHtml" runat="server"></div>

Now, you can use that div to add your dynamic controls to page -

dynaHtml.Controls.Add(username);
Ashutosh Pandey
  • 197
  • 3
  • 13