bizimunda
bizimunda

Reputation: 1035

Copy value from HTML controls to ASP.NET user control

I'm new to this world. Let me explain you by photos.enter image description here

Here is the first photo, I have a user control called "Adress.ascx" and on this page there are 2 different types of controls.

  1. Asp.net controls (red)

  2. Html controls (green)

This user control, I have implemented on another page called Index.aspx

enter image description here

What I want to do is, whenever I click on the button "Copy button", whatever is in the html controls copy to User control.

On Html controls, I'm using Google Maps API to search addresses.

Even, if I don't implement my user control to another page, I'm not able to copy html controls value to asp.net user controls.

For example, I have one asp:TextBox called tbStreet on user control and one Html textbox whose ID is id=street_name. I want to copy street_name's value to tbStreet.

Asp.net control code

 <asp:TextBox ID="tbStreet" runat="server" MaxLength="50" 
 CssClass="inputfield M"></asp:TextBox></td>

Html control code

<input class="field" id="street_name" name="streetName"
            disabled="true"></input></td>

Button code

protected void btnCopy_Click(object sender, EventArgs e)
    {

        string street = Request.Form["street_name"];

    }

But I don't get any value in variable street

Upvotes: 0

Views: 439

Answers (2)

Tetsuya Yamamoto
Tetsuya Yamamoto

Reputation: 24957

If you have the HTML input like this:

<input class="field" id="street_name" name="streetName" />

You should set the name attribute value as Request.Form argument instead of id:

protected void btnCopy_Click(object sender, EventArgs e)
{
    string street = Request.Form["streetName"].ToString();
}

Note that disabled attribute prevents value to be submitted in postback, I recommend to remove that attribute and replace it with readonly if you just want to disallow users editing its value:

<input class="field" id="street_name" name="streetName" readonly="readonly" />

If the text box belongs to a usercontrol, you can create a property to get the text value from it, assumed the input element has runat="server" attribute:

public string StreetName
{
    get { return street_name.Text; }
}

Then you can access usercontrol object properties with this:

UserControl control = (UserControl)Page.FindControl("UserControlId");
string street = control.StreetName;

Upvotes: 1

Jaybird
Jaybird

Reputation: 541

See the docs - https://msdn.microsoft.com/en-us/library/ms525985(v=vs.90).aspx

The element is referenced by name rather than ID, so:

string street = Request.Form["streetName"];

Upvotes: 0

Related Questions