Putting value in asp:TextBox

Discussion in 'ASP .Net' started by =?Utf-8?B?UGF0cmljaw==?=, Jun 6, 2005.

  1. 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.
    =?Utf-8?B?UGF0cmljaw==?=, Jun 6, 2005
    #1
    1. Advertising

  2. 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.
    =?Utf-8?B?UGF0cmljaw==?=, Jun 6, 2005
    #2
    1. Advertising

  3. 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="<%# 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.)
    Steven Cheng[MSFT], Jun 7, 2005
    #3
  4. =?Utf-8?B?UGF0cmljaw==?=

    Guest

    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
    , Jul 5, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QW5hY2hvc3RpYw==?=

    RE: Textbox postback putting value,value

    =?Utf-8?B?QW5hY2hvc3RpYw==?=, Jun 18, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    633
    =?Utf-8?B?S2VpdGggRG9yYW4=?=
    Jun 21, 2004
  2. Ed West
    Replies:
    1
    Views:
    902
    Steve Willcock
    Aug 9, 2004
  3. Saurabh
    Replies:
    6
    Views:
    4,531
    Chris Smith
    May 30, 2004
  4. ErwinP
    Replies:
    1
    Views:
    737
    ErwinP
    Aug 19, 2005
  5. Replies:
    2
    Views:
    336
    Thomas 'PointedEars' Lahn
    Jun 6, 2005
Loading...

Share This Page