Question

i have a small form on SharePoint application page:

 <form method="post" id="registration-form" action="default.aspx" class="form-general cf" data-querycompletion-url="ajax/searchinstant.aspx">
     <input type="text" id="name" name="name" placeholder = "Your name" runat="server" />
          <input type="text" id= "company" name="company" placeholder= "Your company" runat="server"/>
    <input type="text" id="srchtxtx" class="search" name="visitor" placeholder="visitor" runat="server" />
    <input name="btnConfirm" id="Submit1" class="right" value="register" type="submit" runat="server" /></form>

At codebehinde of default.aspx, i want to access values as:

string val1 = name.value;
string val2 = company.value;
string val3 = srchtxtx.value;

But the above returns empty string in val1, val2 nd val3 respectively. But if I remove runat = "server" from <input> tags, then i can access values successfully as:

string val1 = Request.Form["name"];
string val2 = Request.Form["company"];
string val3 = Request.Form["visitor"];

The question is why it returns empty string values when I use runat = "server" in <input> tags ?? Due to some reasons, I could not remove runat = "server" from <input> tags.

Is there any other way to access <input> values at codebehind while assuming runat = "server" attribute in <input> ??

I am also restricted not to use <asp:TextBox> control.

Était-ce utile?

La solution

As I hinted in html form submitted with null values, this is because the form does not runat="server" so the postback data is not processed by asp.net, i.e. properties of srchtxtx etc are not populated.

However html elements are still given unique ids on the page, so you cannot just do Request.Form["srchtxtx"];

To get around this, you can use the UniqueID of the input element to get the posted back value:

HTML

<form method="post" id="registration-form" action="default.aspx" class="form-general cf" data-querycompletion-url="ajax/searchinstant.aspx">
    <input type="text" id="srchtxtx" name="srchtxtx" runat="server" />
    <input name="btnConfirm" id="Submit1" class="right" value="register" type="submit" runat="server" />
</form>

C#

protected void Page_Load(object sender, EventArgs e)
{
    string val1 = srchtxtx.Value; // always ""
    string val2 = Request.Form["srchtxtx"]; // always null
    string val3 = Request.Form[srchtxtx.UniqueID]; // input value
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top