How can I do this?

  • Thread starter Thread starter m.a
  • Start date Start date
M

m.a

Hello,

I know that I can do this:

</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 explained on 15-9-2008 :
Hello,

I know that I can do this:





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 said:
m.a explained on 15-9-2008 :

<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
 
for a data binding expression to be evaluated, you still need to call
DataBind().

-- bruce (sqlwork.com)
 
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
 
Mark Rae said:
Are you sure that your code-behind function is being called? Put a
breakpoint in it to check...

No it is not called? But I don't know why? What is wrong in my code?
 
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)
 
I have code like this. But I didn't use a label control. I just did
something like this:

<% GetAString(); %>

It works for me.
 
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
 
Not sure. For me, I'd simply write a little code in the Load event and set
the visibility there.
 
No I am not getting any error.
Whatr is the difference between
<%= %> and <%# %> ?

where can I read more about this?

Regards
 
Back
Top