Right, please don't point me there:
Disable a HyperLink from code behind
My problem is as follows.
I have a hyperlink on my aspx page:
<asp:HyperLink Visible='<%= _myUser.hasPermission("Intranet Management")%>' Text="Intranet Management" runat="server" NavigateUrl="/Apps/Admin/Default.aspx" />
_myUser.hasPermission("Intranet Management") returns boolean with value of TRUE or FALSE depends if a current user has that permission or not. _myUser is declared in aspx.cs file as protected member so I am able to access it from aspx file.
On my page I am getting following error:
Parser Error Message: Cannot create an object of type 'System.Boolean'
from its string representation '<%= _myUser.hasPermission("Intranet
Management") %>' for the 'Visible' property.
Is there any other way of doing that in aspx file? Please don't ask me to do it in the code behind, I have my reasons to do it here...
Thanks for any help.
The problem you're facing is that asp:Hyperlink is a server control, and these wont evaluate code inside <%= %> for their properties. They will databind though IIRC, so you could try
<asp:HyperLink Visible='<%# _myUser.hasPermission("Intranet Management")%>'...
And make sure to call Page.DataBind().
In order to do it this way, you cannot have runat="server". The idea is that server-side controls will be modified using code behind.
If you don't want to use code behind, use a regular <a> tag without runat="server". There doesn't seem to really be any reason why you need a server control here anyway.
use the following instead:
<%# _myUser.hasPermission("Intranet Management") %>
Got it from here
This should work ...
<asp:HyperLink ID="HyperLink1" Visible='<%# Convert.ToBoolean(_myUser.hasPermission("Intranet Management")) %>' Text="Intranet Management" runat="server" NavigateUrl="/Apps/Admin/Default.aspx" />
Perhaps this is better after all
<a style='<%= Convert.ToBoolean(_myUser.hasPermission("Intranet Management")) ? "" : "display:none;" %>' href="Apps/Admin/Default.aspx"> Intranet Management </a>
Related
I have an Html image which I need to update dynamically but without runat="server" my code behind cannot see it.
<img name="MDI" id="detailimg" src="" alt="blankimage.jpg" />
I can access it using Page.Request.Form["MDI"]; but not update it
I need to load the src image at run time so can someone please tell me how I can update the control in my code.
Do with inline syntax like below
<img name="MDI" id="detailimg" src="<%= getImageSource() %>" alt="blankimage.jpg" />
In codebehind
public string getImageSource()
{
return "urlpath/img.jpeg";
}
More about Inline Syntax
If you want to update via code behind, you have to set runat="server"
If inline is fine, use src="<%= something%>
Otherwise use javascript for example
as you know if you want to access an element/control from code behind,you should set runat="server" but there is 2 other ways to set properties from code behind
use inline syntax as other guys shows (<%= YourVoidToGetImgSrc %>
use Ajax and get value from code behind in your html page.
I am trying to execute some javascript on the click of a asp gridview's hyperlinkfield. The javascript executes fine but the page posts back so when the iframe flickers before showing (The javascript simply sets the iframe's source and then its display from none to block.). How can I go about disabling autopostback on this control since it does not have such an attribute?
<Columns>
<asp:hyperlinkfield Text="Update"
NavigateUrl="javascript:ShowForm('frmAddAudits.aspx');"
HeaderText="Update"
target="_blank"/>
<asp:hyperlinkfield Text="Add Findings"
NavigateUrl="javascript:ShowForm('frmAuditFindings.aspx');"
HeaderText="Add Findings"
target="_blank" />
</Columns>
CSS
.ShowMe{display:block;}
.HideMe{display:none;}
Javascript
function ShowForm(urlToGoTo){
document.getElementById('myiFrame').src = urlToGoTo;
document.getElementById('myiFrame').className = "ShowMe";
}
Try using the TemplateFiled and the anchor tag like this :
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href="#" onclick="javascript:ShowForm('frmAddAudits.aspx');return false;">
</ItemTemplate>
</asp:TemplateField>
if you want you can set runat="server" and give it an ID
In your javascript event handler add return false so the browser does not continue the click action, in this case doing the postback.
Of course, ideally, you wouldn't be using <asp:HyperlinkField>. Have you considered using <a runat="server"> instead?
NavigateUrl="javascript:ShowForm('frmAddAudits.aspx');return false"
Try this NavigateUrl attribute in the markup
NavigateUrl="javascript:ShowForm('frmAddAudits.aspx');return false;"
And also you can try basic HTML anchor tag and javascript to open new window.
See if you are not doing anything on postback of these hyper links then what is the purpose to draw server side control.
These affects the performance while rendering on web page. So I would suggest you to use <a> insread of these hyperlinkfields
you can use instead :
<a href="#" onclick="javascript:ShowForm('frmAddAudits.aspx');return false;">
Postback will never occure in this case and reduce the rendering time and momory cost in comparative to byperlinkfield.
I want to output something like this on an aspx page (not codebehind):
<asp:text id="txt1" runat="server" value="<%# Fields.FirstName %>">
Where Fields.FirstName is a static class. How do I do this? I'm getting an error saying "The name 'Fields' does not exist in the current context". What am I missing? Do I have to include something on the .aspx page?
You could try this:
<input type="text" value="<%=Fields.FirstName %>" id="txt1" />
However, bear in mind that it is no longer a server control.
It is possible to use the <%# Fields.FirstName %> notation in server controls, however they will only be populated when you call DataBind from the code-behind. It is quite custom to use single quotes in the outer scope since double quotes are often needed in the inner scope, like here:
<input type="text" value='<%=Fields["FirstName"] %>' id="txt1" />
But if no quotes are needed it should also work as you described:
<asp:text id="txt1" runat="server" value="<%# Fields.FirstName %>">
As long as you call txt1.DataBind() somewhere in the code behind.
See also this question for more info.
Use the full class name (including all nested namespaces) and an = sign, you are not databinding (denoted by the # sign). I commonly do this...
<%=Namespace.MyStrings.MyConstantString%>
Also, depending on how your page is setup, you might have to use single quotes areound the response write brackets....
<asp:TextBox ID="..." runat="server" Text='<%=Namespace.MyStrings.MyConstantString%>'></asp:TextBox>
UPDATE:
Super hacky, but I got it to work...
<supr:SuprTextBox ID="txt" runat="server" ClientIDMode="Static"></supr:SuprTextBox>
<div id="preload" style="display:none;"><%=Supr.Strings.ASSET_CONTROL_LOCATION%></div>
<script type="text/javascript">
$(function () {
$('#txt').val($('#preload').html());
});
</script>
Had to redeem myself after the <%= syntax not working.
You would need to do this in the code behind or in a code snippet on the aspx page. You cannot nest asp tags (<%# %>) cannot be nested in the asp:text element.
I am using asp:hyperlink button to open a Terms and Condition pop up window.
Code for the hyperlink is -
<asp:HyperLink ID="HyperLink4" Target="_blank"
NavigateUrl="javascript:window.open('test.aspx');"
ForeColor="#F58022" runat="server">Terms and Conditions
</asp:HyperLink>
When I click this URL in a browser it opens up my test.aspx page. But along with test.aspx it opens up another page:
the URL of the page is: "javascript:window.open('test.aspx');"
the body of the page is: [object]
Can you please suggest me how to get rid of this unwanted page?
Thanks
Use:-
<asp:HyperLink ID="HyperLink4" Target="_blank"
NavigateUrl="javascript:window.open('test.aspx'); return false;"
ForeColor="#F58022" runat="server">Terms and Conditions</asp:HyperLink>
The problem is that window.open returns a window object. One purpose of the javascript: "protocol" was to allow javascript code to generate HTML content which is return be the expression following the protocol. Navigation then happens to a new page containing that HTML.
In you case because you have Target="_blank" a new page is opened and the object return by your expression (the new window opened by window.open) has its toString() method called and that is what is displayed in this extra window.
Edit:
I've struck the code because it doesn't work. The correct solution is provided by silky. However I'm not deleting the answer because the explanation of what is going on in the question code stands. Hence the solution really is:-
<asp:HyperLink ID="HyperLink4" href="#"
onclick="window.open('test.aspx'); return false;"
ForeColor="#F58022" runat="server">Terms and Conditions</asp:HyperLink>
Target no longer need it isn't used. Left as HyperLink control since there may be other reasons the OP needs it as a control on the page.
Is there any reason why you need to use the HyperLink control?
You could probably just use a standard HTML link (or an HtmlAnchor control) instead and use the client-side onclick event to fire your JavaScript:
<a id="HyperLink4" runat="server" href="test.aspx" target="_blank"
onclick="window.open('test.aspx');return false;"
style="color:#F58022">Terms and Conditions</a>
Make it:
NavigateUrl="javascript:window.open('test.aspx'); return false;"
Better practice, however, is to put this in OnClientClick
NavigateUrl="#" OnClientClick="window.open('text.aspx'); return false"
-- edit:
<asp:LinkButton ID="HyperLink4" Target="_blank"
NavigateUrl="#" OnClientClick="window.open('text.aspx'); return false"
ForeColor="#F58022" runat="server">Terms and Conditions</asp:LinkButton >
Updated per comments.
<asp:HyperLink ID="HyperLink4" Target="_blank"
NavigateUrl="javascript:void window.open('test.aspx');"
ForeColor="#F58022" runat="server">Terms and Conditions</asp:HyperLink>
Remove Target=_Blank, you don't need it, the JavaScript opens a new window already...
Tip: to know why it acts differently, view source in the browser and check what the web control produces in HTML terms.
Can somebody please explain this to me:
I have a label and I want to be able to set the Text property by calling a method in the aspx file. It works fine if I set the property in code behind, but I really want to set this property in the aspx file.
I have tried a couple of things, but what I expected to work was this:
<asp:Label ID="Label1" runat="server" Text=<%# GetMyText("LabelText") %> />
I get no errors when doing this, but my method is never called and the Text property is left empty.
Is it not possible to set property values to server side controls directly in the aspx without using resources or use hard coded values?
Update: My first try was:
<asp:Label ID="Label1" runat="server" Text=<%= GetMyText("LabelText") %> />
But that results in the following error:
Server tags cannot contain <% ... %> constructs.
The syntax =<%# ... %> is Data binding syntax used to bind values to control properties when the DataBind method is called.
You need to call DataBind - either Page.DataBind to bind all the controls on your page, or Label1.DataBind() to bind just the label. E.g. add the following to your Page_Load event handler:
if (!IsPostBack)
{
this.DataBind();
// ... or Label1.DataBind() if you only want to databind the label
}
Using Text='<%= GetMyText("LabelText") %>' as others have proposed won't work as you'll find out. This syntax is inherited from classic ASP. It can be used in some circumstances in ASP.NET for inserting dynamic values in static HTML, but can not be used for setting propeties of server controls.
The sysntax you are looking for is <%= %> the # is for data binding. So your code should read:
<asp:Label ID="Label1" runat="server" Text='<%= GetMyText("LabelText") %>' />
EDIT: This answere is incrrect
I am leaving this answer here because lots of people agreed with me that this is infact the correct answer, but it will not work. This line of code will produce the following HTML output:
<span id="Label1"><%= GetMyText("LabelText") %></span>
Try this:
<asp:Label ID="Label1" runat="server" Text='<%= GetMyText("LabelText") %>' />
Edit
Yep. I was wrong. #Joe was right.
However, THIS works (and I'm not sure what the difference is):
<asp:Label ID="Label1" runat="server"><%= GetMyText("LabelText") %></asp:Label>
CodeBehind:
protected string GetMyText(string input)
{
return "Hello " + HttpUtility.HtmlEncode(input);
}