|
 |
aspx_beginners thread: Setting Field Values in .Net
Message #1 by "Hugh McLaughlin" <hugh@k...> on Mon, 19 Nov 2001 22:41:29
|
|
Hello Everyone,
A very basic question. When using ASP, you can set the value of a form
field like value="<%=varValue%>". I am unclear as to the "proper" way to
do this with ASP.Net in order to get maximum performance. Also, I am
confused with regards to the state of these values as they are passed from
page-to-page. In the examples I have seen, the state is retained from
page-to-page without using any type of "value" tag. If I use a "value" in
the form, will this mess up the state? Could someone point me to some
code examples. Thanks for the help in advance.
Message #2 by "J House" <jesse@s...> on Wed, 21 Nov 2001 20:31:10
|
|
I am pretty new to .net as well but here is one way to do it.
this would be in the body
<form runat="server">
<input type="text" id="Name" runat="server" /><br>
<input type="text" id="Email" runat="server" /><br>
<input type="submit" value="submit">
</form>
You would want to bind values to these in the Page_Load
this is using C#, the syntax is a bit different for vb.net
void Page_Load(Object Source, EventArgs E)
{
string iID;
iID = Request.Params["ID"]; // use () instead of [] in vb
// left out all db connection and db command code
try
{
// load DataReader
oDataReader = oCmd.ExecuteReader();
// open
oDataReader.Read();
// bind values to input fields based on id
Name.Value = oDataReader.GetString(0);
Email.Value = oDataReader.GetString(1);
}
catch
{
Name.Value = "";
Email.Value = "";
}
finally
{
oDataReader.Close();
oDataReader = null;
oCmd = null;
}
}
I beleive you could also use Web Forms in place of input type="text"
<asp:TextBox id="Name" runat="server" /><br>
<asp:TextBox id="Email" runat="server" /><br>
but you would set the Text property not the Value property
Name.Text = oDataReader.GetString(0);
hope this is of some help.
> Hello Everyone,
>
> A very basic question. When using ASP, you can set the value of a form
> field like value="<%=varValue%>". I am unclear as to the "proper" way
to
> do this with ASP.Net in order to get maximum performance. Also, I am
> confused with regards to the state of these values as they are passed
from
> page-to-page. In the examples I have seen, the state is retained from
> page-to-page without using any type of "value" tag. If I use a "value"
in
> the form, will this mess up the state? Could someone point me to some
> code examples. Thanks for the help in advance.
Message #3 by "J House" <jesse@s...> on Wed, 21 Nov 2001 20:54:04
|
|
it looks like you can use the syntax
<input type="text" value="<%# sValue %>" name="Name" runat="server" />
in your script tags you would have to declare sValue outside any functions
string sValue;
then set the value in the Page_Load function
void Page_Load(Object Source, EventArgs E)
{
sValue="Hello World";
// it won't work unless you call DataBind()
Page.DataBind();
}
> Hello Everyone,
>
> A very basic question. When using ASP, you can set the value of a form
> field like value="<%=varValue%>". I am unclear as to the "proper" way
to
> do this with ASP.Net in order to get maximum performance. Also, I am
> confused with regards to the state of these values as they are passed
from
> page-to-page. In the examples I have seen, the state is retained from
> page-to-page without using any type of "value" tag. If I use a "value"
in
> the form, will this mess up the state? Could someone point me to some
> code examples. Thanks for the help in advance.
|
|
 |