472,122 Members | 1,521 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 software developers and data experts.

Putting value in asp:TextBox

Try to put in a TextBox next month's date in format (MMM yyyy). The following
does NOT work, why is that?

<!--When rendered, the texbox is blank, although the text in the round
bracket after the textbox is rendered correctly as (July, 2005)-->
<asp:TextBox id="MonthTextBox"
runat="server"><%=System.DateTime.Now.ToString("y" )%></asp:TextBox>
(<%=System.DateTime.Now.ToString("y")%>)

<!--Same again. defaultMonth is initialised as early as page_init (as
opposed to page_load makes no difference
the text in round bracket after the textbox is displayed correctly-->
<asp:TextBox id="MonthTextBox"
runat="server"><%=defaultMonth%></asp:TextBox> (<%=defaultMonth%>)
//OnInit method appended here for reference
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
DateTime nextMonth = System.DateTime.Now;
nextMonth = nextMonth.AddMonths(1);
// Put user code to initialize the page here
//defaultMonth = nextMonth.ToString("y");
defaultMonth= "abc";
InitializeComponent();
base.OnInit(e);
}

However, I can do:
<asp:TextBox id="MonthTextBox" runat="server">at least this
works</asp:TextBox> (<%=defaultMonth%>)

What is going on? Using ASP.NET 1.1 on IIS5.1/WinXP Pro SP1.
Nov 19 '05 #1
3 2056
Updating the Text property of the TextBox in page_load does the trick!

"Patrick" wrote:
Try to put in a TextBox next month's date in format (MMM yyyy). The following
does NOT work, why is that?

<!--When rendered, the texbox is blank, although the text in the round
bracket after the textbox is rendered correctly as (July, 2005)-->
<asp:TextBox id="MonthTextBox"
runat="server"><%=System.DateTime.Now.ToString("y" )%></asp:TextBox>
(<%=System.DateTime.Now.ToString("y")%>)

<!--Same again. defaultMonth is initialised as early as page_init (as
opposed to page_load makes no difference
the text in round bracket after the textbox is displayed correctly-->
<asp:TextBox id="MonthTextBox"
runat="server"><%=defaultMonth%></asp:TextBox> (<%=defaultMonth%>)
//OnInit method appended here for reference
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
DateTime nextMonth = System.DateTime.Now;
nextMonth = nextMonth.AddMonths(1);
// Put user code to initialize the page here
//defaultMonth = nextMonth.ToString("y");
defaultMonth= "abc";
InitializeComponent();
base.OnInit(e);
}

However, I can do:
<asp:TextBox id="MonthTextBox" runat="server">at least this
works</asp:TextBox> (<%=defaultMonth%>)

What is going on? Using ASP.NET 1.1 on IIS5.1/WinXP Pro SP1.

Nov 19 '05 #2
Hi Patrick,

Yes, as for the <% %> code block, they're included in asp.net for asp
compatiable purpose, and it can only be used in aspx template at the top
html content , in another word, we can only mix the <% %> code with normal
html content , but can not inject them into ASP.NET server control's
tag(like <asp:TextBox..... ).

For ASP.NET Server Controls, we have the following means to
programmatically assign property values for them:

1. Use code to set value for Control's Properties in code behind , this is
what you've found.

2. Using the <%# %> databinding expression to bind value for asp.net
server control,

#Data Binding Expression Syntax
http://msdn.microsoft.com/library/en...abindingExpres
sionSyntax.asp?frame=true

For example, suppose we have the following control definitaion in aspx
template:

<asp:TextBox id="TextBox1" runat="server"
Text="<%# GetTextValue() %>"
</asp:TextBox>


GetTextValue() is a public/protected function defined in the page's
codebehind class, then, we can call

TextBox1.BataBind();

in Page_Load or any other sub functions which will make he <%# %>
databiding experssion in TextBox1 control get invoked and bind the value
returned from GetTextValue to the TextBox1. For detailed info , you can
have a look on the above MSDN reference or searching the web for some
further reference.

If there're anything else we can help, please feel free to post here.

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 19 '05 #3
Hi Steven

A related question I believe:

I am using a FormView, Insert Template. I have 3 textboxes which are
bound. I would like to use a default (static) value in one of the
textboxes.

Here's the (relevant) sample code:

<InsertItemTemplate>

Stamp: <asp:TextBox ID="StampTextBox" runat="server"
Text='<%#Bind("fldStamp") %>'></asp:TextBox><br />

Again, what I would like to do is assign a default/static text "Canada"
to this textbox and bind it to fldStamp

I am fairly new at this and would appreciate your assistance and some
sample code.

Thanks
Shiraz
is it possible to assign a default value to a textbox which is

Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Craig Buchanan | last post: by
1 post views Thread by Ed West | last post: by
reply views Thread by Luis Esteban Valencia | last post: by
reply views Thread by datakix | last post: by
reply views Thread by CharlesA | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.