Reputation: 1325
I'm going crazy trying to send the value of an HTML text input (without runat="server") from a user control to a code behind. I get an empty string with no error. The steps are as follows:
User clicks on the ImageButton with ID="name_btn".
The value must be passed to the code behind. I'm testing to see if the value is passed by using the page load (if (IsPostBack)) and the button click function but still NO VALUE in both.
Note that for some other reason, I don't want the text input to run on the server so I don't want to add runat="server". Any help please?
User Control page (name.ascx)
<input type="text" ID="name" />
<asp:ImageButton runat="server" ID="name_btn" OnClick="name_Click" ImageUrl="~/icon-ok.png" />
User control - Code behind (name.ascx.cs)
public string nameBox;
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
nameBox = Request.Form["name"];
Response.Write("Name: " + nameBox);
}
}
protected void name_Click(object sender, EventArgs e)
{
nameBox = Request.Form["name"];
Response.Write("Name: " + nameBox);
}
Master Page
<uc:name runat="server" ID="UCname" />
Upvotes: 2
Views: 11790
Reputation: 149
you should add a name attribute to your input tag, example:
<input type="text" id="name" name="name"/>
then you can use:
if (IsPostBack)
{
nameBox = Request.Form["name"];
Response.Write("Name: " + nameBox);
}
The Request.Form(element)[(index)|.Count]
Parameters:
The name of the form element from which the collection is to retrieve values.
An optional parameter that enables you to access one of multiple values for a parameter. It can be any integer in the range 1 to Request.Form(parameter).Count.
Upvotes: 2
Reputation: 862
I would imagine that there is a bit of a conflict with what you are trying to do. For example, on the server you are using Request.Form[""] to get the html input element but you are requesting it by the id attribute. Request.Form supports finding elements by index and name.
So have you tried adding a name="" to the html input element?
Upvotes: 4
Reputation: 42
You can pass the value to codebehind using a javascript in two ways. 1. Onclick of your image button call a javascript. Set your textbox value in __EVENTARGUMENT and call __doPostback(). And in pageload you can check the value in __EVENTARGUMENT.
Upvotes: 1