1

I am not quite sure how to attack this, basically I have two html fields in my aspx page:

<input type="text" name="fname" />
<input type="text" name="lname"/>

Now i would like to populate them from the server side when the page loads based on some data collected from the database, basically this data is stored in two properties

public string FirstName { get; set;}
public string LastName {get; set;}

How can I pass the value from such properties into the html inputs On_Load ?

I would appreciate the help.

0

3 Answers 3

2

Here is one way, assuming Webforms:

<input type="text" name="fname" value="<%:FirstName%>" />
<input type="text" name="lname" value="<%:LastName%>" />

If using .NET before 4.0, replace the <%: with <%=.

Another option is to change the input types to be runat="server" and assigning the values directly on the server side.

Sign up to request clarification or add additional context in comments.

4 Comments

You may want to check that second input ;)
@Oded Very useful. I never knew about using "<:" over "<%=". +1!
@Oded, is there a difference between using <@: and <@=?
@AshBurlaczenko - Yes. <%: is <%= with Html.Encode included. See weblogs.asp.net/scottgu/archive/2010/04/06/…
1

Alternatively add runat="server" to your elements, then you could do something like

fname.Value = FirstName;
lname.Value = LastName;

2 Comments

Is that right? If it was a server side control you wouldn't need to FindControl, use could access it directly.
True. But FindControl doesn't work on client side controls. See MSDN - "Searches the current naming container for a server control with the specified id parameter."
0

The info I was looking for was like this:

protected void Page_Load(object sender, EventArgs e)
{
    address.Value = Request.QueryString["lat"];
    address1.Value = Request.QueryString["long"];
}

takes values from the URL string and puts them in an HTML input="text"

http://localhost:64375/Map.aspx?lat=detroit&long=windsor

 Enter Address A: <input runat="server" name="address" id="address" type="text" />
 Enter Address B: <input runat="server" name="address1" id="address1" type="text" />    

thanks Ash and Oded for the combined answer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.