MailTo: Asp.net - c#

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.

Related

Using ASP.NET to Hide an HTML Div

I am using ASP.NET for a web page in order to make some server calls that involve looking up user organization information. Based on that information, we need to either hide or display a div. In the header I have a C# function that definitely runs. I have tried the following lines to hide the div.
divID.Style.Add("display","none");
and
divID.Visible = false;
In the body, I am currently using an asp:Panel that runs at server and contains the id "divID". No matter what I do, I can't get the div to hide (without manually putting the styling in). I tried putting the scripts before and after the body, and it didn't make a difference. Any suggestions on the best way to do this would be appreciated.
EDIT:
Here is the C# initiating code.
<script runat="server" language="C#">
void getUserInfo(Object sender, EventArgs ev){
The rest of the C# code is irrelevant, but the relevant line shown above is definitely being run.
The HTML portion looks something like this.
<asp:Panel runat="server" id="divID" style="width:200px; height:130px; ">
<div style="text-align:center">Test Data</div>
</asp:Panel>
C# code is always compiled and run from the server-side, and so cannot impact the state of a page once rendered unless you use postbacks or callbacks. If you want to change the visible state of a control on the client-side, you will need to use Javascript on the client side (possibly triggered by a button click) to show and hide the control.
As an example, check out the solution at the link below.
https://forums.asp.net/t/1603211.aspx?Show+hide+div+on+button+click+without+postback
<script type="text/javascript">
function ToggleDiv(Flag) {
if (Flag == "first") {
document.getElementById('dvFirstDiv').style.display = 'block';
document.getElementById('dvSecondDiv').style.display = 'none';
}
else {
document.getElementById('dvFirstDiv').style.display = 'none';
document.getElementById('dvSecondDiv').style.display = 'block';
}
}
</script>
<asp:Button ID="btn" runat="server" Text="Show First Div"
OnClientClick="ToggleDiv('first');return false;" />
<asp:Button ID="Button1" runat="server" Text="Show Second Div"
OnClientClick="ToggleDiv('second');return false;" />
<br />
<div id="dvFirstDiv" style="display: none;">
First Div
</div>
<div id="dvSecondDiv" style="display: none;">
Second Div
</div>
In the header I have a C# function that definitely runs.
If you're talking about the HTML page header - no, it definitely not running. C# code is executed only server side.
Based on your post, I'm assuming we're talking WebForms here and yo have a script block in your aspx file. While this is fine, I recommend placing the server-side code into a code behind file.
So all you need to do is to add a handler for the PreRender phase of the page life cycle and place your logic for showing/hiding the div in there.
public void Page_Prerender(object sender, EventArgs e)
{
divID.Visible = false;
' OR
'divID.Style.Add("display","none");
}
Note that setting the Visible property of a WebForms control excludes the control from rendering to the page, whilst setting display: none renders it as HTML but it isn't displayed on the page.
Try in backcode: divID.Controls.clear();
This worked for me.

Using a c# conditional statement to render html

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 !!!

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.

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.

I am trying to hide text in a ASPX page that is tied to a textbox

So there is a a table and a text box in one of the cells
<td>
<asp:TextBox ID="tbSomeTextBox" Columns="5" runat="server"> %
</td>
This textbox gets shown if a certain selection is made in a drop down. The problem is that I would like the "%" character to also be hidden or shown with the textbox.
I have tried putting the whole textbox control inside a DIVand in my JQuery hiding the DIV at the same time I hid the textbox.
<td>
<div id="divSomeDIV"><asp:TextBox ID="tbSomeTextBox" Columns="5" runat="server"> % </div
</td>
But I get an error in my java script that id="divSomeDIV" doesn't exist in the current context.
$("#<%=divSomeDiv.ClientID%>").hide();
Wrapping that single character in a asp:Label seems like overkill.
Any suggestions?
divSomeDiv is running client-side (i.e. no "runat=server"), so there's no need for
$("#<%=divSomeDiv.ClientID%>").hide();
Just do
$("#divSomeDiv").hide();
create an asp.net label, set the label value to be %, and make them both visible or both not visible at the same time... ?
If you were getting "does not exist" in your Javascript it's probably because your Javascript is running before the div is created in the dom. In other words, this is incorrect.
<script>$("#divSomeDiv").hide()</script>
<div id="divSomeDiv">...</div>
You can either put the Javascript after the div
<div id="divSomeDiv">...</div>
<script>$("#divSomeDiv").hide()</script>
Or you can make your Javascript run after the DOM has loaded.
<script>
$(function() {
$("#divSomeDiv").hide()
});
</script>
<div id="divSomeDiv">...</div>

Categories

Resources