Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Putting value in asp:TextBox

Reply
Thread Tools

Putting value in asp:TextBox

 
 
=?Utf-8?B?UGF0cmljaw==?=
Guest
Posts: n/a
 
      06-06-2005
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.
 
Reply With Quote
 
 
 
 
=?Utf-8?B?UGF0cmljaw==?=
Guest
Posts: n/a
 
      06-06-2005
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.

 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      06-07-2005
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.)

 
Reply With Quote
 
shariffshiraz@hotmail.com
Guest
Posts: n/a
 
      07-05-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Putting an int value in a char array Vlad Dogaru C Programming 6 05-03-2008 05:25 PM
XSL: putting a XSL value inside an html attribute? Kourosh XML 6 05-17-2006 09:13 PM
Re: Putting a "<" in an attribute value (was about CDATA sections) Jon Noring XML 5 11-16-2005 10:00 PM
RE: Textbox postback putting value,value =?Utf-8?B?QW5hY2hvc3RpYw==?= ASP .Net 1 06-21-2004 10:50 AM
Difference between putting code in constructor and putting code in static{} Saurabh Java 6 05-30-2004 02:44 PM



Advertisments