473,386 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 2105
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Craig Buchanan | last post by:
I have an asp:textbox embedded in a repeater. I would like to default a value in the textbox, but when I try to use the format: <asp:textbox id="qty" runat="server"> <%#...
1
by: Ed West | last post by:
Hello, How can I put a textbox control into a cell of an asp:Table? I was not able to do it from the properties sheet, so I put it in via html tab, but now I can't select it from the Design tab...
0
by: Luis Esteban Valencia | last post by:
am trying to databind within an asp:textbox control. I have tried many variations found here but nothing seems to work. It works fine if I use a regular HTML textbox input though. Here is what I...
3
by: Chris | last post by:
I am trying to use a calendar popup to populate a date in a textbox. I am using the example described in this article: http://aspnet.4guysfromrolla.com/articles/030202-1.aspx However, this...
0
by: datakix | last post by:
After 16 hours of frustration, I've managed to solve this problem for a project I'm working on. The 'trick' is set EnableViewState="False" for the asp:textbox inside the Repeater control. The...
1
by: Imran Aziz | last post by:
Hello All, I have an option of search on my web page coded as under <p>Search Bookmarks: <asp:TextBox ID="txtSearch" runat="server" class="texta" ></asp:TextBox><asp:Button ID="btnSearch"...
11
by: Joe | last post by:
Hello All, I have an ASP.NET page with one Textbox (SearchTextBox) and one ImageButton (SearchButton) server controls. The user can type search text in SearchTextBox and click SearchButton and...
0
by: CharlesA | last post by:
Hi folks, I'm using ASP.net 1.1 with C# I've got this kind of thing going <div class="row"> <label class="col1">Rm Name</label> <asp:textbox id="txtRM" runat="server" cssclass="col2"...
0
by: sjickells | last post by:
Hi I am having a problem using asp:TextBox's in a transparent table. I have a background image on the page and a table in the middle of the page. I have set the background colour of the table...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.