Inline statement is not interpreted correctly

F

Froefel

Hi all,

I have a curious thing that I can't figure out.

I have an aspx page with associated code-behind file; the aspx page
has the following code:

<asp:LinkButton ID="lnkShowMyProjects" runat="server" PostBackUrl="~/
SelectProject.aspx?show=<%= User.Identity.Name %>">Show only my
projects</asp:LinkButton>

When I run this project and I look at the Page Source after the page
has been rendered, the above statement was translated into:

<a id="ctl00_pageContent_lnkShowAllProjects"
href="javascript:WebForm_DoPostBackWithOptions(new
WebForm_PostBackOptions(&quot;ctl00$pageContent
$lnkShowAllProjects&quot;, &quot;&quot;, false, &quot;&quot;,
&quot;SelectProject.aspx?show=&quot;, false, true))">Show all
projects</a>

As you can see, instead of replacing <%= User.Identity.Name %> with
the actual value of User.Identity.Name, it included the statement as a
literal string. Consequently, the Querystring doesn't look the way I
intended it.

Can anyone tell me what the reason is for this behavior? Also can you
tell me how I should fix it?

Thanks
-- Hans
 
P

Patrice

The <% notation inside attributes is AFAIK only available to replace the
full attribute i.e. you could do :
PostBackUrl='<%="SelectProject.aspx?show=" & User.Identity.Name %>'

You may still want to check as I prefer to do such thing in codebehind and
doesn't work much perhaps even never more with "inlined" code...

If you still want to go this route you could use a compromise such as
(clearer IMO) :
PostBackUrl='<%=ProjectSelectionUrl%>' with ProjectSelectionUrl as a
function that returns the correct string.

As a side note, passing the user name in the query string is generally not a
good idea (if on the same server why to let this go client side at all where
it could be changed ?)
 
B

bruce barker

there are 3 more requiments.

1) you can only use a binding expression (<%# expression %>), not a
response.write (<%= expression%>).

2) the control must support the method DataBind() or be nested inside a
control that supports databinding its children.

3) you must call DataBind() on the control.

-- bruce (sqlwork.com)
 
G

GroupReader

I think you can't use the <%= xxx %> syntax with a "runat=server"
webcontrol. It only works when replacing text in an html control
(without runat=server).

Is that correct?

... and as Bruce mentioned, you can use databinding with the data-
centric webcontrols with this syntax: <%# xxx %>
 
F

Froefel

Thanks to everyone for their input...
Eventually I found the same solution as what GroupReader suggested,
and indeed what Patrice was saying made sense (allthough I didn't try
it).

Thanks again for the insights.

-- Hans
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top