Putting value in asp:TextBox

G

Guest

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.
 
S

Steven Cheng[MSFT]

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-us/cpgenref/html/cpconDatabindingExpres
sionSyntax.asp?frame=true

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

<asp:TextBox id="TextBox1" runat="server"
Text= said:
</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.)
 
S

shariffshiraz

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top