How can I do this?

Discussion in 'ASP .Net' started by m.a, Sep 15, 2008.

  1. m.a

    m.a Guest

    Hello,

    I know that I can do this:

    <asp:Label runat="server" ID="test" text='<%# Eval("a_field")%>'
    ></asp:Label>




    but how can I do this:

    <asp:Label runat="server" ID="test" text='<%# GetAString()%>' ></asp:Label>



    where GetAString() is a C# function returning a string (sample as follow):



    public static string GetSString()

    {

    return "test";

    }

    when I add the above line to my aspx page, I am not getting any error, but
    nothing for label is shown when it should show test.



    Any suggestion on what is the problem?

    Regards
    m.a, Sep 15, 2008
    #1
    1. Advertising

  2. m.a

    Hans Kesting Guest

    m.a explained on 15-9-2008 :
    > Hello,
    >
    > I know that I can do this:
    >
    > <asp:Label runat="server" ID="test" text='<%# Eval("a_field")%>'
    > ></asp:Label>

    >
    >
    >
    > but how can I do this:
    >
    > <asp:Label runat="server" ID="test" text='<%# GetAString()%>' ></asp:Label>
    >
    >
    >
    > where GetAString() is a C# function returning a string (sample as follow):
    >
    >
    >
    > public static string GetSString()
    >
    > {
    >
    > return "test";
    >
    > }
    >
    > when I add the above line to my aspx page, I am not getting any error, but
    > nothing for label is shown when it should show test.
    >
    >
    >
    > Any suggestion on what is the problem?
    >
    > Regards


    <asp:Label runat="server" ID="test" text='<%= GetAString()%>' />

    use "%=" instead of "%#"

    Hans Kesting
    Hans Kesting, Sep 15, 2008
    #2
    1. Advertising

  3. m.a

    m.a Guest

    "Hans Kesting" <> wrote in message
    news:...
    > m.a explained on 15-9-2008 :
    >> Hello,
    >>
    >> I know that I can do this:
    >>
    >> <asp:Label runat="server" ID="test" text='<%# Eval("a_field")%>'
    >> ></asp:Label>

    >>
    >>
    >>
    >> but how can I do this:
    >>
    >> <asp:Label runat="server" ID="test" text='<%# GetAString()%>'
    >> ></asp:Label>

    >>
    >>
    >>
    >> where GetAString() is a C# function returning a string (sample as
    >> follow):
    >>
    >>
    >>
    >> public static string GetSString()
    >>
    >> {
    >>
    >> return "test";
    >>
    >> }
    >>
    >> when I add the above line to my aspx page, I am not getting any error,
    >> but nothing for label is shown when it should show test.
    >>
    >>
    >>
    >> Any suggestion on what is the problem?
    >>
    >> Regards

    >
    > <asp:Label runat="server" ID="test" text='<%= GetAString()%>' />
    >
    > use "%=" instead of "%#"
    >
    > Hans Kesting
    >
    >


    Thanks, but it doesn't work.

    I created a new project in MSVS2008 and the default.aspx is :



    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
    Inherits="WebApplication1._Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server">

    <title></title>

    </head>

    <body>

    <form id="form1" runat="server">

    <div>

    <asp:Label runat="server" ID="test" text='<%= GetAString()%>' />

    </div>

    </form>

    </body>

    </html>



    And the default.aspx.cs is as follow:



    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Web;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    namespace WebApplication1

    {

    public partial class _Default : System.Web.UI.Page

    {

    protected void Page_Load(object sender, EventArgs e)

    {

    }

    protected string GetAString()

    {

    return "test";

    }

    }

    but when I run the application, the result is a blank page.

    any suggestion?

    Regards
    m.a, Sep 15, 2008
    #3
  4. m.a

    bruce barker Guest

    for a data binding expression to be evaluated, you still need to call
    DataBind().

    -- bruce (sqlwork.com)


    "m.a" wrote:

    >
    > Hello,
    >
    > I know that I can do this:
    >
    > <asp:Label runat="server" ID="test" text='<%# Eval("a_field")%>'
    > ></asp:Label>

    >
    >
    >
    > but how can I do this:
    >
    > <asp:Label runat="server" ID="test" text='<%# GetAString()%>' ></asp:Label>
    >
    >
    >
    > where GetAString() is a C# function returning a string (sample as follow):
    >
    >
    >
    > public static string GetSString()
    >
    > {
    >
    > return "test";
    >
    > }
    >
    > when I add the above line to my aspx page, I am not getting any error, but
    > nothing for label is shown when it should show test.
    >
    >
    >
    > Any suggestion on what is the problem?
    >
    > Regards
    >
    >
    >
    >
    >
    bruce barker, Sep 15, 2008
    #4
  5. m.a

    m.a Guest

    Thanks,
    But it is not a databinding expression. I also add the databind() to the
    form_load but no difference, (with the refrence to the codes that I send
    with my second post.

    Regards

    "bruce barker" <> wrote in message
    news:...
    > for a data binding expression to be evaluated, you still need to call
    > DataBind().
    >
    > -- bruce (sqlwork.com)
    >
    >
    > "m.a" wrote:
    >
    >>
    >> Hello,
    >>
    >> I know that I can do this:
    >>
    >> <asp:Label runat="server" ID="test" text='<%# Eval("a_field")%>'
    >> ></asp:Label>

    >>
    >>
    >>
    >> but how can I do this:
    >>
    >> <asp:Label runat="server" ID="test" text='<%# GetAString()%>'
    >> ></asp:Label>

    >>
    >>
    >>
    >> where GetAString() is a C# function returning a string (sample as
    >> follow):
    >>
    >>
    >>
    >> public static string GetSString()
    >>
    >> {
    >>
    >> return "test";
    >>
    >> }
    >>
    >> when I add the above line to my aspx page, I am not getting any error,
    >> but
    >> nothing for label is shown when it should show test.
    >>
    >>
    >>
    >> Any suggestion on what is the problem?
    >>
    >> Regards
    >>
    >>
    >>
    >>
    >>
    m.a, Sep 15, 2008
    #5
  6. m.a

    m.a Guest

    "Mark Rae [MVP]" <> wrote in message
    news:...
    > "m.a" <> wrote in message
    > news:...
    >
    >>>> <asp:Label runat="server" ID="test" text='<%# GetAString()%>'
    >>>> ></asp:Label>
    >>>>
    >>>
    >>> <asp:Label runat="server" ID="test" text='<%= GetAString()%>' />
    >>>
    >>> use "%=" instead of "%#"

    >>
    >> Thanks, but it doesn't work.
    >>
    >> I created a new project in MSVS2008 and the default.aspx is :
    >>
    >> protected string GetAString()

    >
    > Are you sure that your code-behind function is being called? Put a
    > breakpoint in it to check...
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net


    No it is not called? But I don't know why? What is wrong in my code?
    m.a, Sep 15, 2008
    #6
  7. > protected string GetAString()
    > {
    > return "test";
    > }


    Try this instead:

    protected void GetAString()
    {
    Response.Write("test");
    }

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com

    "m.a" <> wrote in message
    news:...
    >
    > "Hans Kesting" <> wrote in message
    > news:...
    >> m.a explained on 15-9-2008 :
    >>> Hello,
    >>>
    >>> I know that I can do this:
    >>>
    >>> <asp:Label runat="server" ID="test" text='<%# Eval("a_field")%>'
    >>> ></asp:Label>
    >>>
    >>>
    >>>
    >>> but how can I do this:
    >>>
    >>> <asp:Label runat="server" ID="test" text='<%# GetAString()%>'
    >>> ></asp:Label>
    >>>
    >>>
    >>>
    >>> where GetAString() is a C# function returning a string (sample as
    >>> follow):
    >>>
    >>>
    >>>
    >>> public static string GetSString()
    >>>
    >>> {
    >>>
    >>> return "test";
    >>>
    >>> }
    >>>
    >>> when I add the above line to my aspx page, I am not getting any error,
    >>> but nothing for label is shown when it should show test.
    >>>
    >>>
    >>>
    >>> Any suggestion on what is the problem?
    >>>
    >>> Regards

    >>
    >> <asp:Label runat="server" ID="test" text='<%= GetAString()%>' />
    >>
    >> use "%=" instead of "%#"
    >>
    >> Hans Kesting
    >>
    >>

    >
    > Thanks, but it doesn't work.
    >
    > I created a new project in MSVS2008 and the default.aspx is :
    >
    >
    >
    > <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
    > Inherits="WebApplication1._Default" %>
    >
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    >
    > <html xmlns="http://www.w3.org/1999/xhtml" >
    >
    > <head runat="server">
    >
    > <title></title>
    >
    > </head>
    >
    > <body>
    >
    > <form id="form1" runat="server">
    >
    > <div>
    >
    > <asp:Label runat="server" ID="test" text='<%= GetAString()%>' />
    >
    > </div>
    >
    > </form>
    >
    > </body>
    >
    > </html>
    >
    >
    >
    > And the default.aspx.cs is as follow:
    >
    >
    >
    > using System;
    >
    > using System.Collections.Generic;
    >
    > using System.Linq;
    >
    > using System.Web;
    >
    > using System.Web.UI;
    >
    > using System.Web.UI.WebControls;
    >
    > namespace WebApplication1
    >
    > {
    >
    > public partial class _Default : System.Web.UI.Page
    >
    > {
    >
    > protected void Page_Load(object sender, EventArgs e)
    >
    > {
    >
    > }
    >
    >
    > }
    >
    > but when I run the application, the result is a blank page.
    >
    > any suggestion?
    >
    > Regards
    >
    >
    >
    >
    Jonathan Wood, Sep 15, 2008
    #7
  8. m.a

    m.a Guest

    It is not working since the function never called.

    "Jonathan Wood" <> wrote in message
    news:...
    >> protected string GetAString()
    >> {
    >> return "test";
    >> }

    >
    > Try this instead:
    >
    > protected void GetAString()
    > {
    > Response.Write("test");
    > }
    >
    > --
    > Jonathan Wood
    > SoftCircuits Programming
    > http://www.softcircuits.com
    >
    > "m.a" <> wrote in message
    > news:...
    >>
    >> "Hans Kesting" <> wrote in message
    >> news:...
    >>> m.a explained on 15-9-2008 :
    >>>> Hello,
    >>>>
    >>>> I know that I can do this:
    >>>>
    >>>> <asp:Label runat="server" ID="test" text='<%# Eval("a_field")%>'
    >>>> ></asp:Label>
    >>>>
    >>>>
    >>>>
    >>>> but how can I do this:
    >>>>
    >>>> <asp:Label runat="server" ID="test" text='<%# GetAString()%>'
    >>>> ></asp:Label>
    >>>>
    >>>>
    >>>>
    >>>> where GetAString() is a C# function returning a string (sample as
    >>>> follow):
    >>>>
    >>>>
    >>>>
    >>>> public static string GetSString()
    >>>>
    >>>> {
    >>>>
    >>>> return "test";
    >>>>
    >>>> }
    >>>>
    >>>> when I add the above line to my aspx page, I am not getting any error,
    >>>> but nothing for label is shown when it should show test.
    >>>>
    >>>>
    >>>>
    >>>> Any suggestion on what is the problem?
    >>>>
    >>>> Regards
    >>>
    >>> <asp:Label runat="server" ID="test" text='<%= GetAString()%>' />
    >>>
    >>> use "%=" instead of "%#"
    >>>
    >>> Hans Kesting
    >>>
    >>>

    >>
    >> Thanks, but it doesn't work.
    >>
    >> I created a new project in MSVS2008 and the default.aspx is :
    >>
    >>
    >>
    >> <%@ Page Language="C#" AutoEventWireup="true"
    >> CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
    >>
    >> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    >> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    >>
    >> <html xmlns="http://www.w3.org/1999/xhtml" >
    >>
    >> <head runat="server">
    >>
    >> <title></title>
    >>
    >> </head>
    >>
    >> <body>
    >>
    >> <form id="form1" runat="server">
    >>
    >> <div>
    >>
    >> <asp:Label runat="server" ID="test" text='<%= GetAString()%>' />
    >>
    >> </div>
    >>
    >> </form>
    >>
    >> </body>
    >>
    >> </html>
    >>
    >>
    >>
    >> And the default.aspx.cs is as follow:
    >>
    >>
    >>
    >> using System;
    >>
    >> using System.Collections.Generic;
    >>
    >> using System.Linq;
    >>
    >> using System.Web;
    >>
    >> using System.Web.UI;
    >>
    >> using System.Web.UI.WebControls;
    >>
    >> namespace WebApplication1
    >>
    >> {
    >>
    >> public partial class _Default : System.Web.UI.Page
    >>
    >> {
    >>
    >> protected void Page_Load(object sender, EventArgs e)
    >>
    >> {
    >>
    >> }
    >>
    >>
    >> }
    >>
    >> but when I run the application, the result is a blank page.
    >>
    >> any suggestion?
    >>
    >> Regards
    >>
    >>
    >>
    >>

    >
    m.a, Sep 15, 2008
    #8
  9. m.a

    bruce barker Guest

    but it is a databinding expression, even it if does not access the databound
    object. call DataBind() on the label, when you wnat the evaluation to take
    effect.

    Databinding expressions are pretty simple, when the page is compiled, if a
    control has a databinding expression (property = "<%# expr() %>") then the
    equalivent to following code is generated (actually ondatabinding fires an
    event to call a delegate, instead of calling the code direct) :

    OnDataBinding(EvaneArg e)
    {
    property = expr();
    }

    -- bruce (sqlwork.com)


    "m.a" wrote:

    > Thanks,
    > But it is not a databinding expression. I also add the databind() to the
    > form_load but no difference, (with the refrence to the codes that I send
    > with my second post.
    >
    > Regards
    >
    > "bruce barker" <> wrote in message
    > news:...
    > > for a data binding expression to be evaluated, you still need to call
    > > DataBind().
    > >
    > > -- bruce (sqlwork.com)
    > >
    > >
    > > "m.a" wrote:
    > >
    > >>
    > >> Hello,
    > >>
    > >> I know that I can do this:
    > >>
    > >> <asp:Label runat="server" ID="test" text='<%# Eval("a_field")%>'
    > >> ></asp:Label>
    > >>
    > >>
    > >>
    > >> but how can I do this:
    > >>
    > >> <asp:Label runat="server" ID="test" text='<%# GetAString()%>'
    > >> ></asp:Label>
    > >>
    > >>
    > >>
    > >> where GetAString() is a C# function returning a string (sample as
    > >> follow):
    > >>
    > >>
    > >>
    > >> public static string GetSString()
    > >>
    > >> {
    > >>
    > >> return "test";
    > >>
    > >> }
    > >>
    > >> when I add the above line to my aspx page, I am not getting any error,
    > >> but
    > >> nothing for label is shown when it should show test.
    > >>
    > >>
    > >>
    > >> Any suggestion on what is the problem?
    > >>
    > >> Regards
    > >>
    > >>
    > >>
    > >>
    > >>

    >
    >
    >
    bruce barker, Sep 15, 2008
    #9
  10. I have code like this. But I didn't use a label control. I just did
    something like this:

    <% GetAString(); %>

    It works for me.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com

    "m.a" <> wrote in message
    news:...
    > It is not working since the function never called.
    >
    > "Jonathan Wood" <> wrote in message
    > news:...
    >>> protected string GetAString()
    >>> {
    >>> return "test";
    >>> }

    >>
    >> Try this instead:
    >>
    >> protected void GetAString()
    >> {
    >> Response.Write("test");
    >> }
    >>
    >> --
    >> Jonathan Wood
    >> SoftCircuits Programming
    >> http://www.softcircuits.com
    >>
    >> "m.a" <> wrote in message
    >> news:...
    >>>
    >>> "Hans Kesting" <> wrote in message
    >>> news:...
    >>>> m.a explained on 15-9-2008 :
    >>>>> Hello,
    >>>>>
    >>>>> I know that I can do this:
    >>>>>
    >>>>> <asp:Label runat="server" ID="test" text='<%# Eval("a_field")%>'
    >>>>> ></asp:Label>
    >>>>>
    >>>>>
    >>>>>
    >>>>> but how can I do this:
    >>>>>
    >>>>> <asp:Label runat="server" ID="test" text='<%# GetAString()%>'
    >>>>> ></asp:Label>
    >>>>>
    >>>>>
    >>>>>
    >>>>> where GetAString() is a C# function returning a string (sample as
    >>>>> follow):
    >>>>>
    >>>>>
    >>>>>
    >>>>> public static string GetSString()
    >>>>>
    >>>>> {
    >>>>>
    >>>>> return "test";
    >>>>>
    >>>>> }
    >>>>>
    >>>>> when I add the above line to my aspx page, I am not getting any error,
    >>>>> but nothing for label is shown when it should show test.
    >>>>>
    >>>>>
    >>>>>
    >>>>> Any suggestion on what is the problem?
    >>>>>
    >>>>> Regards
    >>>>
    >>>> <asp:Label runat="server" ID="test" text='<%= GetAString()%>' />
    >>>>
    >>>> use "%=" instead of "%#"
    >>>>
    >>>> Hans Kesting
    >>>>
    >>>>
    >>>
    >>> Thanks, but it doesn't work.
    >>>
    >>> I created a new project in MSVS2008 and the default.aspx is :
    >>>
    >>>
    >>>
    >>> <%@ Page Language="C#" AutoEventWireup="true"
    >>> CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
    >>>
    >>> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    >>> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    >>>
    >>> <html xmlns="http://www.w3.org/1999/xhtml" >
    >>>
    >>> <head runat="server">
    >>>
    >>> <title></title>
    >>>
    >>> </head>
    >>>
    >>> <body>
    >>>
    >>> <form id="form1" runat="server">
    >>>
    >>> <div>
    >>>
    >>> <asp:Label runat="server" ID="test" text='<%= GetAString()%>' />
    >>>
    >>> </div>
    >>>
    >>> </form>
    >>>
    >>> </body>
    >>>
    >>> </html>
    >>>
    >>>
    >>>
    >>> And the default.aspx.cs is as follow:
    >>>
    >>>
    >>>
    >>> using System;
    >>>
    >>> using System.Collections.Generic;
    >>>
    >>> using System.Linq;
    >>>
    >>> using System.Web;
    >>>
    >>> using System.Web.UI;
    >>>
    >>> using System.Web.UI.WebControls;
    >>>
    >>> namespace WebApplication1
    >>>
    >>> {
    >>>
    >>> public partial class _Default : System.Web.UI.Page
    >>>
    >>> {
    >>>
    >>> protected void Page_Load(object sender, EventArgs e)
    >>>
    >>> {
    >>>
    >>> }
    >>>
    >>>
    >>> }
    >>>
    >>> but when I run the application, the result is a blank page.
    >>>
    >>> any suggestion?
    >>>
    >>> Regards
    >>>
    >>>
    >>>
    >>>

    >>

    >
    >
    Jonathan Wood, Sep 15, 2008
    #10
  11. m.a

    m.a Guest

    Thanks, it is working but what if I want to use it with an asp control? for
    example, if I want to hide/show a control based on some calculation?

    for example I may want to do:

    <asp:Label runat="server" ID="test" Text='test' visible=<%# ShouldShow()%>'
    />



    Regards



    "Jonathan Wood" <> wrote in message
    news:...
    >I have code like this. But I didn't use a label control. I just did
    >something like this:
    >
    > <% GetAString(); %>
    >
    > It works for me.
    >
    > --
    > Jonathan Wood
    > SoftCircuits Programming
    > http://www.softcircuits.com
    >
    > "m.a" <> wrote in message
    > news:...
    >> It is not working since the function never called.
    >>
    >> "Jonathan Wood" <> wrote in message
    >> news:...
    >>>> protected string GetAString()
    >>>> {
    >>>> return "test";
    >>>> }
    >>>
    >>> Try this instead:
    >>>
    >>> protected void GetAString()
    >>> {
    >>> Response.Write("test");
    >>> }
    >>>
    >>> --
    >>> Jonathan Wood
    >>> SoftCircuits Programming
    >>> http://www.softcircuits.com
    >>>
    >>> "m.a" <> wrote in message
    >>> news:...
    >>>>
    >>>> "Hans Kesting" <> wrote in message
    >>>> news:...
    >>>>> m.a explained on 15-9-2008 :
    >>>>>> Hello,
    >>>>>>
    >>>>>> I know that I can do this:
    >>>>>>
    >>>>>> <asp:Label runat="server" ID="test" text='<%# Eval("a_field")%>'
    >>>>>> ></asp:Label>
    >>>>>>
    >>>>>>
    >>>>>>
    >>>>>> but how can I do this:
    >>>>>>
    >>>>>> <asp:Label runat="server" ID="test" text='<%# GetAString()%>'
    >>>>>> ></asp:Label>
    >>>>>>
    >>>>>>
    >>>>>>
    >>>>>> where GetAString() is a C# function returning a string (sample as
    >>>>>> follow):
    >>>>>>
    >>>>>>
    >>>>>>
    >>>>>> public static string GetSString()
    >>>>>>
    >>>>>> {
    >>>>>>
    >>>>>> return "test";
    >>>>>>
    >>>>>> }
    >>>>>>
    >>>>>> when I add the above line to my aspx page, I am not getting any
    >>>>>> error, but nothing for label is shown when it should show test.
    >>>>>>
    >>>>>>
    >>>>>>
    >>>>>> Any suggestion on what is the problem?
    >>>>>>
    >>>>>> Regards
    >>>>>
    >>>>> <asp:Label runat="server" ID="test" text='<%= GetAString()%>' />
    >>>>>
    >>>>> use "%=" instead of "%#"
    >>>>>
    >>>>> Hans Kesting
    >>>>>
    >>>>>
    >>>>
    >>>> Thanks, but it doesn't work.
    >>>>
    >>>> I created a new project in MSVS2008 and the default.aspx is :
    >>>>
    >>>>
    >>>>
    >>>> <%@ Page Language="C#" AutoEventWireup="true"
    >>>> CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
    >>>>
    >>>> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    >>>> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    >>>>
    >>>> <html xmlns="http://www.w3.org/1999/xhtml" >
    >>>>
    >>>> <head runat="server">
    >>>>
    >>>> <title></title>
    >>>>
    >>>> </head>
    >>>>
    >>>> <body>
    >>>>
    >>>> <form id="form1" runat="server">
    >>>>
    >>>> <div>
    >>>>
    >>>> <asp:Label runat="server" ID="test" text='<%= GetAString()%>' />
    >>>>
    >>>> </div>
    >>>>
    >>>> </form>
    >>>>
    >>>> </body>
    >>>>
    >>>> </html>
    >>>>
    >>>>
    >>>>
    >>>> And the default.aspx.cs is as follow:
    >>>>
    >>>>
    >>>>
    >>>> using System;
    >>>>
    >>>> using System.Collections.Generic;
    >>>>
    >>>> using System.Linq;
    >>>>
    >>>> using System.Web;
    >>>>
    >>>> using System.Web.UI;
    >>>>
    >>>> using System.Web.UI.WebControls;
    >>>>
    >>>> namespace WebApplication1
    >>>>
    >>>> {
    >>>>
    >>>> public partial class _Default : System.Web.UI.Page
    >>>>
    >>>> {
    >>>>
    >>>> protected void Page_Load(object sender, EventArgs e)
    >>>>
    >>>> {
    >>>>
    >>>> }
    >>>>
    >>>>
    >>>> }
    >>>>
    >>>> but when I run the application, the result is a blank page.
    >>>>
    >>>> any suggestion?
    >>>>
    >>>> Regards
    >>>>
    >>>>
    >>>>
    >>>>
    >>>

    >>
    >>

    >
    m.a, Sep 15, 2008
    #11
  12. Not sure. For me, I'd simply write a little code in the Load event and set
    the visibility there.

    --
    Jonathan Wood
    SoftCircuits Programming
    http://www.softcircuits.com

    "m.a" <> wrote in message
    news:%...
    > Thanks, it is working but what if I want to use it with an asp control?
    > for example, if I want to hide/show a control based on some calculation?
    >
    > for example I may want to do:
    >
    > <asp:Label runat="server" ID="test" Text='test' visible=<%#
    > ShouldShow()%>' />
    >
    >
    >
    > Regards
    >
    >
    >
    > "Jonathan Wood" <> wrote in message
    > news:...
    >>I have code like this. But I didn't use a label control. I just did
    >>something like this:
    >>
    >> <% GetAString(); %>
    >>
    >> It works for me.
    >>
    >> --
    >> Jonathan Wood
    >> SoftCircuits Programming
    >> http://www.softcircuits.com
    >>
    >> "m.a" <> wrote in message
    >> news:...
    >>> It is not working since the function never called.
    >>>
    >>> "Jonathan Wood" <> wrote in message
    >>> news:...
    >>>>> protected string GetAString()
    >>>>> {
    >>>>> return "test";
    >>>>> }
    >>>>
    >>>> Try this instead:
    >>>>
    >>>> protected void GetAString()
    >>>> {
    >>>> Response.Write("test");
    >>>> }
    >>>>
    >>>> --
    >>>> Jonathan Wood
    >>>> SoftCircuits Programming
    >>>> http://www.softcircuits.com
    >>>>
    >>>> "m.a" <> wrote in message
    >>>> news:...
    >>>>>
    >>>>> "Hans Kesting" <> wrote in message
    >>>>> news:...
    >>>>>> m.a explained on 15-9-2008 :
    >>>>>>> Hello,
    >>>>>>>
    >>>>>>> I know that I can do this:
    >>>>>>>
    >>>>>>> <asp:Label runat="server" ID="test" text='<%# Eval("a_field")%>'
    >>>>>>> ></asp:Label>
    >>>>>>>
    >>>>>>>
    >>>>>>>
    >>>>>>> but how can I do this:
    >>>>>>>
    >>>>>>> <asp:Label runat="server" ID="test" text='<%# GetAString()%>'
    >>>>>>> ></asp:Label>
    >>>>>>>
    >>>>>>>
    >>>>>>>
    >>>>>>> where GetAString() is a C# function returning a string (sample as
    >>>>>>> follow):
    >>>>>>>
    >>>>>>>
    >>>>>>>
    >>>>>>> public static string GetSString()
    >>>>>>>
    >>>>>>> {
    >>>>>>>
    >>>>>>> return "test";
    >>>>>>>
    >>>>>>> }
    >>>>>>>
    >>>>>>> when I add the above line to my aspx page, I am not getting any
    >>>>>>> error, but nothing for label is shown when it should show test.
    >>>>>>>
    >>>>>>>
    >>>>>>>
    >>>>>>> Any suggestion on what is the problem?
    >>>>>>>
    >>>>>>> Regards
    >>>>>>
    >>>>>> <asp:Label runat="server" ID="test" text='<%= GetAString()%>' />
    >>>>>>
    >>>>>> use "%=" instead of "%#"
    >>>>>>
    >>>>>> Hans Kesting
    >>>>>>
    >>>>>>
    >>>>>
    >>>>> Thanks, but it doesn't work.
    >>>>>
    >>>>> I created a new project in MSVS2008 and the default.aspx is :
    >>>>>
    >>>>>
    >>>>>
    >>>>> <%@ Page Language="C#" AutoEventWireup="true"
    >>>>> CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
    >>>>>
    >>>>> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    >>>>> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    >>>>>
    >>>>> <html xmlns="http://www.w3.org/1999/xhtml" >
    >>>>>
    >>>>> <head runat="server">
    >>>>>
    >>>>> <title></title>
    >>>>>
    >>>>> </head>
    >>>>>
    >>>>> <body>
    >>>>>
    >>>>> <form id="form1" runat="server">
    >>>>>
    >>>>> <div>
    >>>>>
    >>>>> <asp:Label runat="server" ID="test" text='<%= GetAString()%>' />
    >>>>>
    >>>>> </div>
    >>>>>
    >>>>> </form>
    >>>>>
    >>>>> </body>
    >>>>>
    >>>>> </html>
    >>>>>
    >>>>>
    >>>>>
    >>>>> And the default.aspx.cs is as follow:
    >>>>>
    >>>>>
    >>>>>
    >>>>> using System;
    >>>>>
    >>>>> using System.Collections.Generic;
    >>>>>
    >>>>> using System.Linq;
    >>>>>
    >>>>> using System.Web;
    >>>>>
    >>>>> using System.Web.UI;
    >>>>>
    >>>>> using System.Web.UI.WebControls;
    >>>>>
    >>>>> namespace WebApplication1
    >>>>>
    >>>>> {
    >>>>>
    >>>>> public partial class _Default : System.Web.UI.Page
    >>>>>
    >>>>> {
    >>>>>
    >>>>> protected void Page_Load(object sender, EventArgs e)
    >>>>>
    >>>>> {
    >>>>>
    >>>>> }
    >>>>>
    >>>>>
    >>>>> }
    >>>>>
    >>>>> but when I run the application, the result is a blank page.
    >>>>>
    >>>>> any suggestion?
    >>>>>
    >>>>> Regards
    >>>>>
    >>>>>
    >>>>>
    >>>>>
    >>>>
    >>>
    >>>

    >>

    >
    >
    Jonathan Wood, Sep 15, 2008
    #12
  13. m.a

    m.a Guest

    No I am not getting any error.
    Whatr is the difference between
    <%= %> and <%# %> ?

    where can I read more about this?

    Regards



    "Mark Rae [MVP]" <> wrote in message
    news:%...
    > "m.a" <> wrote in message
    > news:...
    >
    >>>> protected string GetAString()
    >>>
    >>> Are you sure that your code-behind function is being called? Put a
    >>> breakpoint in it to check...

    >>
    >> No it is not called?

    >
    > Well, there's the problem!
    >
    >> But I don't know why? What is wrong in my code?

    >
    > Difficult to tell immediately...
    >
    > If you do this:
    >
    > <asp:Label runat="server" ID="test" text='<%= zzzzGetAString()%>' />
    >
    > do you get a runtime error...?
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    m.a, Sep 15, 2008
    #13
    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. Bob
    Replies:
    1
    Views:
    375
    Shawn B.
    Jan 12, 2004
  2. Richard Dixson
    Replies:
    1
    Views:
    499
    Joe Fallon
    May 18, 2004
  3. Rutger Smit
    Replies:
    1
    Views:
    538
    =?Utf-8?B?THVrZWI=?=
    Sep 6, 2004
  4. duncan
    Replies:
    2
    Views:
    6,071
  5. Casey Hawthorne
    Replies:
    1
    Views:
    700
    Arne Vajhøj
    Mar 18, 2009
Loading...

Share This Page