I have some code that essentially looks like this:
<div>
<% if(Something) { %>
<div id="someUniqueMarkup">
This markup should not be output if Something==true.
<units:MyUserControl runat="server"/>
</div>
<% }
else { %>
<units:MyUserControl runat="server" />
<% } %>
</div>
Depending on Something, one of them is hidden, and that is fine. But if I set break points in the user control, I notice it's being loaded twice (once for each of the controls above) and all it's logic is being run twice. I could of course control this with placeholders or multiviews, but the same thing seems to apply - OnLoad/Page_Load etc is run once for each control that is actually on the page.
EDIT:
The reason why im showing/hiding this is because I need to include some markup around the control if Something == true. I could wrap the "unique markup" itself in if-else before and after the control, but that just seems dirty for something that really should be as simple as I've imagined above. The user control itself should be exactly the same in both scenarios, sorry for the confusing property it had.
Is it just me, or is this just a really unintuitive interface? And is it actually possible to not load/execute a user control at all as long as it's on the page?
Since you have two controls on the page it will render them both. The if-check you create, only determines whether it's included in the output. The easiest way to prevent this is to change your code like this:
<div>
<units:MyUserControl runat="server" SomeSetting="<%= Something %>" />
</div>
EDIT: Answer to edit in the original post:
<div>
<% if(Something) { %>
<div id="someUniqueMarkup">
This markup should not be output if Something==true.
<asp:placeholder id="phItemInDiv" runat="server" />
</div>
<% }
else { %>
<asp:placeholder id="phItemOutsideDiv" runat="server" />
<% } %>
</div>
MyUserControl ctrl = (MyUserControl)LoadControl("/pathtousercontrol.ascx")
if (something){
phItemInDiv.Controls.Add(ctrl);
}
else{
phItemOutsideDiv.Controls.Add(ctrl);
}
This way you will only have the user control emitted (and loaded) if Something is true
The best way, in my opinion, is to declare your user control once in the ASPX.
In the code behind, on PageLoad, apply the logic you see fit:
if(something)
MyUserControl.SomeSettings = ...
If there is a problem in the chronology, do the above logic in PreLoad since it will fire before Page Load of the page and all of its related user controls.
EDIT:
You can put two different IDs on the user controls with Enabled = false.
In Page_load, set Enabled to one of them based on the logic you desire.
Related
I have an aspx webpage with embedded code block that displays one of the two instances of the same user control. Simplified code:
<form id="form1" runat="server">
<div>
<%if (true)
{ %>
<UC:UserControl runat="server" ID="ucFirst"></UC:UserControl>
<%}
else
{ %>
<UC:UserControl runat="server" ID="ucSecond"></UC:UserControl>
<%} %>
</div>
</form>
Registered user control is cached:
<%# OutputCache Duration="60" VaryByParam="none" %>
However, only ucFirst gets cached, while ucSecond goes through Page_Load and Page_PreRender each time I refresh the page, even though it doesn't appear in rendered HTML code at all. Is it possible to either prevent ucSecond entirely from loading, or load it just once like ucFirst and keep it cached?
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.
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.
I have a webpage with server accessible controls, see 'FileIconLink' below:
<body>
<p class="FirstTitle style5">Downloads:</p>
<div id="BreadcrumbDiv">
<p style="padding-left:5px; ">Page Loading...</p>
</div><!--/BreadcrumbDiv-->
<div id="DirLinksDiv">
<p><span class="SecondTitle">Files:</span></p>
<a runat="server" href="#" id="FileIconLink">File</a>
<% WriteFileLinks(); %>
<p><span class="SecondTitle">Folders:</span></p>
<a runat="server" href="#" id="FolderIconLink">Folder</a>
</div><!--/DirLinksDiv-->
</body>
<%RemoveHTMLTemplates(); %>
Both 'FileIconLink' and 'FolderIconLink' are templates of web controls which are copied by my code - such as <% WriteFileLinks(); %> above. How could these templates be permanently removed from the web page at run-time on the server without causing the error:
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
Thanks in advance!
This is because you have <% %> inside the control you're trying to change. Instead of using <% %> in the aspx page, I would modify the code behind to add a literal control or something to the div, like:
DirLinks.Controls.Add(new LiteralControl(WriteFile()));
You should then be able to modify your control form the code behind.
Your inline code is executed during render.
But you probably want to get rid of the templates during Load.
Which means that the two techniques conflict.
Ultimately I realised my approach was wrong, as Cade Roux was alluding to, I needed to make up my mind where the templates were going to be used.
My solution was as follows:
Make controls for containing the results of my (previously inline) code.
Use templates in Page_Load to fill the controls described above.
Delete templates in Page_Load.
Do nothing inline.
The Page object has another function apart from Page_Load function called Page_PreRender, this function gets executed before Page_Load. So please try remove logic in this Page_PreRender function.
Please refer this link http://msdn.microsoft.com/en-us/library/system.web.ui.control.prerender.aspx
I have an issue currently that I can't resolve. I have a user control called "Dashboard" which then has the following markup, containing several subcontrols.
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Dashboard.ascx.cs" Inherits="BlueSEQ.Controls.Dashboard.Dashboard" %>
<%# Register src="Administrator.ascx" tagname="Administrator" tagprefix="uc1" %>
<%# Register src="Provider.ascx" tagname="Provider" tagprefix="uc2" %>
<%# Register src="User.ascx" tagname="User" tagprefix="uc3" %>
<% if (isAdministrator)
{ %>
<uc1:Administrator ID="Administrator1" runat="server" />
<% }
else if (isProvider)
{ %>
<uc2:Provider ID="Provider1" runat="server" />
<% }
else
{ %>
<uc3:User ID="User1" runat="server" />
<% } %>
As you can see, I want it to display some controls or other controls depending on some conditions. However, all of these controls' "Load" event get triggered, even if they are not used.
How can I prevent this?
If you can help it, try to avoid having conditional logic in your markup. It could make the views somewhat more difficult to understand for designers (if you're working with designers) and more difficult to find and refactor this code in the future.
You should also take a look at ASP.NET MVC: Avoiding Tag Soup. Although it's ASP.NET MVC, it's still a good example of how adding logic to your views can quickly make them very difficult and unpleasant to maintain (initial example).
You could use the technique described here: How to: Add Controls to an ASP.NET Web Page Programmatically.
Your markup would look something like this.
<asp:PlaceHolder id="MyPlaceholder" />
and your codebehind would have something along the lines of
private void InitSection()
{
Control c;
if( isAdministrator )
c = Page.LoadControl("~\Administrator.ascx")
else if( isProvider )
c = Page.LoadControl("~\Provider.ascx")
else
c = Page.LoadControl("~\User.ascx");
MyPlaceholder.Controlls.Add(c);
}
The ideal way to do this is to set up asp.net role provider and use a LoginView control, something along the lines of the code below. LoginView only loads the appropriate content.
<asp:LoginView runat="server">
<AnonymousTemplate>
<uc1:User ID="User" runat="server" />
</AnonymousTemplate>
<RoleGroups>
<asp:RoleGroup Roles="Administrator">
<ContentTemplate>
<uc1:Administrator ID="Administrator1" runat="server" />
</ContentTemplate>
</asp:RoleGroup>
<asp:RoleGroup Roles="Provider">
<ContentTemplate>
<uc1:Provider ID="Provider" runat="server" />
</ContentTemplate>
</asp:RoleGroup>
</RoleGroups>
</asp:LoginView>
You have to Load the control on a specific condition instead, so try to set visible/invisible with the usercontrol, that's a much better approach
<% if (isAdministrator)
{ %>
Page.LoadControl(("~\Administrator1.ascx");
<% }
How about using a MultiView control?
MultiView on MSDN