C# – Passing the value of an HTML text input (without runat server) from a user control to code behind

asp.netcuser-controls

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:

  1. User clicks on the ImageButton with ID="name_btn".

  2. 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" />  

Best Answer

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?

Related Topic