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("ctl00$pageContent
$lnkShowAllProjects", "", false, "",
"SelectProject.aspx?show=", 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
Patrice - 12 Jul 2007 15:03 GMT
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 ?)
---
Patrice
> Hi all,
>
[quoted text clipped - 27 lines]
> Thanks
> -- Hans
bruce barker - 12 Jul 2007 16:35 GMT
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)
> The <% notation inside attributes is AFAIK only available to replace the
> full attribute i.e. you could do :
[quoted text clipped - 46 lines]
>> Thanks
>> -- Hans
GroupReader - 12 Jul 2007 17:44 GMT
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 %>
GroupReader - 12 Jul 2007 17:45 GMT
In your code-behind, you can put lnkShowMyProjects.PostBackUrl =
"xxxx";
Froefel - 12 Jul 2007 23:23 GMT
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