Using a c# conditional statement to render html - c#

Is there a way to do a conditional statement in C# for my asp.net page? I want it to be, basically:
if bool is true, add <a href>
I've got this:
<asp:Label ID="BenLabel" CssClass='<%#((Entry)(Container.DataItem)).HasBenform ? "EnabledEntry" : "DisabledEntry"%>' Text="Benefits Form" runat="server" />
In this way it changes its CSS class based on the bool value. However, is it possible for me to add a link, too? How would I code this?

Add an HyperLink and then render it Visible based on this boolean condition.
<asp:HyperLink ID="BenLink" Visible='<%# ((Entry)(Container.DataItem)).HasBenform %>' ... runat="server" />

I like to handle this sort of logic in the code behind on page_init or Page_Load.
Something like this
If (NeedToShowLink)
{
Link.Visible = true;
}
else
{
Link.Visible = false;
}

Why do you want to render the control to client side and setting css to hide.
You can do something like below in the aspx page or suggested by Andrew.
<%if(condition)%>
Click me
Happy Coding !!!

Related

MailTo: Asp.net

I have a asp label that I need to be able to change according to the code behind. How can I do this?
ASPX: (The first part works correctly only for "TestA#abc.com" and the second part dynamically changes the label (EmailLabel) according to the "if" statement in the code behind. How can I integrate these two so the label is mailto? Thanks.
<p>Email at TestA#abc.com.</p>
<p>Email at <asp:Label ID="EmailLabel" runat="server"></asp:Label>.</p>
Code Behind:
public changeLabel()
{
if (//Some Condition Here)
{
this.EmailLabel.Text = "TestA#abc.com";
}
else
{
this.EmailLabel.Text = "TestB#abc.com";
}
}
What you are trying to do there won't work. Label's render out as <span> tags, so it will never be "clickable". You want to do something more like this:
<p>Email at TestA#abc.com.</p>
<p>Email at <asp:LinkButton ID="EmailLabel" runat="server"></asp:LinkButton>.</p>
And then instead of changing the Text property, change the NavigateUrl property.
You could also use an HtmlControl, which is basically a standard HTML tag that you add the runat="server" attribute to.
<p>Email at <a id="EmailLabel" runat="server" href=""></a>.</p>
You would then be able to modify this <a> tag via server side code, the properties will be slightly different, but now you've got a real live anchor tag to work with.
This other SO question might also be helpful: How to launching email client on LinkButton click event?
<html>
<body>
<span id="foo"
onclick="document.getElementById('foo').innerHTML = 'never say never'"
>
Click Me!
</span>
</body>
</html>
For your Label, just remember that .Text is the server equivalent of .innerHTML so you can put whatever HTML you want right into the asp:Label when setting .Text. Just watch out for cross-site-scripting exploits.

Hyperlink control not showing within ContentTemplate in ASP.NET

<ContentTemplate>
<div class="detail_purchase_button">
<a class="commandbutton" href='/Courses?RestoreFilters=1'>Return to Catalog</a>
<%# linkAddToCart %>
</div>
</ContentTemplate>
string url = "/Cart?AddItem={0}", DataItemID;
linkAddToCart = new HyperLink();
linkAddToCart.CssClass = "commandbutton";
linkAddToCart.NavigateUrl = url;
linkAddToCart.Text = "Add To Cart";
The button within the anchor tag shows up on the page. However, the Hyperlink button does not appear at all.
The second block of code is running in the Page_Load event (I will put it in a method after I get it to work) and is referencing a public Hyperlink field.
Thanks for any help.
Define the hyperlink control via markup in the presentation file.
<asp:HyperLink id="lnkAddToCart" runat="server" />
Place it where you need it to be. You can still reference its properties in your codebehind.
lnkAddToCart.CssClass = "commandbutton";
lnkAddToCart.NavigateUrl = url;
// etc.
If you were to define the control dynamically instead, you would need to add it to the appropriate container, such as a panel or placeholder
<asp:Panel id="theContainer" runat="server" /> or
<asp:PlaceHolder id="theContainer" runat="server" />
...
// define the HyperLink as in your original code snippet
theContainer.Controls.Add(lnkAddToCart);
However, unless you absolutely need to dynamically create the control, adding it to the the ASPX at design time is best. You can always set Visible="false" (to the markup, .Visible = false; in code) if it does not need to be displayed all the time.
Your question seems a bit vague but I am trying to answer based on what I understand out of it. Please ignore if I misunderstood your question.
It is not possible to add an hyperlink like this. First put a placeholder(i.e. panel) within the contenttemplate and then add the hyperlink into the placeholder from code behind.
<a class="commandbutton" href='/Courses?RestoreFilters=1'>Return to Catalog</a>
<asp:Panel id="pnlLink" runat="server"></asp:Panel>
</div>
</ContentTemplate>
And then in the code behind
string url = "/Cart?AddItem={0}", DataItemID;
linkAddToCart = new HyperLink();
linkAddToCart.CssClass = "commandbutton";
linkAddToCart.NavigateUrl = url;
linkAddToCart.Text = "Add To Cart";
pnlLink.Controls.Add(linkAddToCart);
Try setting an ID to the linkAddToCart control that you create dynamically. Your code does not define an ID for it. With that being said, I would recommend doing this the way Anthony suggested. I don't see the need to do this kind of thing. If you want to have some logic for making appear in certain cases, just define it in your markup with Visible="false" and make it visible when you need to.

Disable hyperlink in .NET

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>

How to make a button not show up but still be used by javascript?

I am using a button that has to be invible and should be used by a javascript function.
<asp:Button ID ="btnDummy1" runat="server" Visible ="true" OnClick="btnSubmit1_Click" width="0px" height="0px"/
I cannot keep visible = false as it the javascript will not use invible content in the poage. I havetried to give width=0 and height=0, still it showws up in Chrome. What do you guys think i should do?
Thanks in advance :)
A pretty clean approach in ASP.Net it give it a "hidden" class:
<asp:Button ID ="btnDummy1" runat="server" CssClass="hidden" />
Then in your stylesheet:
.hidden { display: none; }
If you set it to Visible="False" then the code will not be executed.
Instead I think you should wrap it in a <div> and set display:none via css:
<div style="display: none;">
<asp:Button ID ="btnDummy1" runat="server" OnClick="btnSubmit1_Click" />
</div>
adding style="display:none" would hide the button till you make it visible again
<asp:Button ID ="btnDummy1" runat="server" OnClick="btnSubmit1_Click" style="display:none"/>
How about
style="display:none"
for the button instead of Visible = "true".
Can you just use a hidden form field in this case? This is generally the preferred method of passing information along.
See http://www.tizag.com/htmlT/htmlhidden.php
<asp:Button ID="btnMyOrders" runat="server" Text="my orders" CssClass="dropdown-item" OnClick="btnMyOrders_Click" Visible="False" />
Example then in Form(){
btn.Visible = true; to show it again
}
I think the first question you should ask yourself is : why would I put in my HTML doc a button that should not be visible ?
An HTML document should be used ONLY TO DESCRIBE A CONTENT'S SEMANTICS. So an HTML doc should ONLY contain the content you want to publish and extra data to explain the content's SEMANTIC !! NOTHING about the way it is displayed, NOTHING about the way it behaves !!
All displaying and behaviors questions should be managed with CSS and Javascript, NEVER within HTML itself.
Any element needed for Javascript only purpose should be added in the doc by Javascript itself !
You should never find, in an HTML doc, such things as prev/next buttons for a javascript picture gallery for example. The HTML doc should only contain the pictures list, than your JS script will change the way this list is shown, making it a eye candy gallery and add buttons for navigation.
So in your example, your saying you want to add in your HTML doc an invisible button with some Javascript actions on it.... that's probably an example of some content that should never be in the HTML doc.

Need help with error- The name 'lnk1' does not exist in the current context

This is my link button:-
<asp:LinkButton ID="lnk1" Text="Set as Default" runat="server" Visible="false" OnClick="lnk1_OnClick"></asp:LinkButton>
In Code Behind I am simply making it visible
lnk1.Visible = true;
I have checked the IDs over n over..whats wrong ? Intellisense wont detect it either..I am doing something really silly..just cant figure what it is ..help!
I even restarted Visual Studio..still same error
Is the contol part of another control template? E.G. part of a repeaters ItemTemplate etc?
Update:
Since OP has said it's part of a repeaters ItemTemplate, just thought I'd explain what to do (Even though OP has sorted it)
You need to call FindControl on the Repeater, or Controls.OfType() depending on the situation, to get the control.
ASP:
<asp:Repeater runat="server" ID="rptrTest">
<ItemTemplate>
<asp:TextBox runat="server" ID="txtBxName" />
<asp:CheckBox runat="server" ID="chkBx1" />
<asp:CheckBox runat="server" ID="chkBx2" />
</ItemTemplate>
</asp:Repeater>
C#
IEnumerable<CheckBox> chkBoxes = rptrTest.Controls.OfType<CheckBox>();
TextBox txtBxName = (TextBox)rptrTest.FindControl("txtBxName");
What I'll often do for commonly used controls (though wether it's a good idea or not I'm sure someone will now let me know), is create a member which executes this code.
private TextBox _txtBxName;
public TextBox txtBxName {
get {
if (_txtBxName == null) {
_txtBxName = (TextBox)rptrTest.FindControl("txtBxName");
}
return _txtBxName;
}
}
Sometimes the designer class is not re-generated correctly. Things you can try:
select the line, cut, save, rebuild,
paste back, save
delete the designer
.cs file, right click the aspx,
convert to web application -> this
will generate the designer class from
scratch
Since I do not have the rights to comment; so ...
In which event you are making that item visible? try doing that in PageLoad.
Can you show your markups?
Alternatively, you can try to Find the control.

Categories

Resources