I am having an issue with ASP .NET user controls - c#

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

Related

How to cache user controls that are inside code blocks?

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?

Getting Error in using user control(.ascx) on asp.net mvc layout page

I am integrating Asp.net User control(.ascx) in my Mvc Layout page i.e is master page and i am using razor engine.
Error:Error executing child request for handler
'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerWrapper ascx.
Control '1_hdfData' of type 'HiddenField' must be placed inside a form tag with runat=server.
This is my code:
_Layout.cshtml:
#Html.Partial("~/UserControls/Data.ascx")
Data.ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Data.ascx.cs" Inherits="MyNameSpace.Data" %>
<asp:HiddenField ID="hdfData" runat="server" />
--Rest all other asp.net server side controls---.
Data.ascx.cs:
namespace MyNameSpace.Data
{
public partial class Data : ViewUserControl
{
//Page_Load events and other code
}
}
Is it like if i am inheriting User Control(.ascx) from ViewUserControl so i cant use asp.net control?
Can anybody help me with this?
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Data.ascx.cs" Inherits="MyNameSpace.Data" %>
<form runat="server">
<asp:HiddenField ID="hdfData" runat="server" />
--Rest all other asp.net server side controls---.
</form>
I am sharing one idea, but it's not good practice.
#Html.Partial("_ASCX_FILE")
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<%# Register Assembly="SomeAssembly" Namespace="SomeNs" TagName="ASCX_FILE" %>
<ASCX_FILE:SomeControl runat="server" ID="fooControl" />
Possibly above solution can sort out your issue.
Suggesting again, try using pure MVC flavor. :)

UserControl in asp .net is not working?

I have created a two usercontrols in asp.net and one webform . Now i want these two usercontrols to show in form in webform but it say that there must be one head with runat="server"
this is webform where i am using UserControl!
<%# Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Light.master" CodeBehind="AdministrationPage.aspx.cs" Inherits="DXApplication5.AdministrationPage" %>
<%# Register src="~/AdminPage/AssignmentTab.ascx" tagname="AssignmentUC" tagprefix="uc1" %>
<asp:Content ID="MainContent" ContentPlaceHolderID="MainContent" runat="server">
<table border="0">
<tr>
<td>
<div class="accountHeader">
<uc1:AssignmentUC ID="CalendarUserControl1" runat="server" />
</div>
</td>
</tr>
</table>
</asp:Content>
This is UserControl below:
<%# Control Language="C#" ClassName="AssignmentUC" AutoEventWireup="true" CodeBehind="AssignmentTab.ascx.cs" Inherits="DXApplication5.AdminPage.AssignmentTab" %>
I would add a single form to your masterpage, this may be the cause of your error.
I would also remove all other form server controls from your user controls and pages.
Try these steps:
Go to Light.master master page file and make sure that this is in there somewhere <form id="form1" runat="server"> and a closing tag, the id may be different.
Go to the following files AssignmentTab.ascx and AdministrationPage.aspx and remove any <form id="form1" runat="server"> and closing tags </form>
Check user control code if it contains a head element. Also check all dependencies to see if there are head elements present in them.
I think you have use <form id="formID" runat="server"> in both of your page and user Control .
Just remove runat="server" from your user Control Form tag .
Make sure you have in your user control cs file:
public partial class WebUserControl1 : System.Web.UI.UserControl
In ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebUserControl1" %>
In aspx parent:
<%# Register Src="WebUserControl1.ascx" TagPrefix="uc" TagName="WebUserControl1 " %>
<uc:WebUserControl1 runat="server" ID="mycontrol" />

Lost events inside ascx in some cases

I have some strange behavior.
I have a control (InformationalPanel.ascx) who is put inside two diferent aspx pages. In one of them (SendSmsFile.aspx ) everythings works fine but in other (SendMessage.aspx), although the control is correctly render, the events inside doesn't work.
The control is equal inside aspx pages who have equal asp header and same masterpage
<%# Page Language="C#" MasterPageFile="~/Default.Master" AutoEventWireup="True" CodeBehind="SendMessage.aspx.cs" Inherits="Messages.SendMessage" ValidateRequest="false"%>
...
<asp:Panel ID="panelSubmissionScheduler" runat="server" Visible="false">
<uc2:InformationalPanel ID="submissionSchedulerInformationalPanel" runat="server" />
</asp:Panel>
The control has a repeater with a LinkButton who in some cases lost Click event
<asp:Repeater ID="repeaterPartsToSchedule" runat="server" OnItemCommand="repeaterPartsToSchedule_ItemCommand"
OnItemDataBound="repeaterPartsToSchedule_ItemDataBound">
<ItemTemplate>
...
<asp:LinkButton ID="scheduleLinkButton" runat="server" Text="<%$ Resources:ResourcesDefault, Schedule %>"
CssClass="button_serv" CommandName="schedule" OnClick="scheduleLinkButton_Click" />
...
</ItemTemplate>
</asp:Repeater>
When visibility is changed (because some business rules) I call ascx's Refresh method who populates the repeater (DataBind).
I run debug in the to aspx pages but I cannot discover why in one everything works and in another the events (scheduleLinkButton_Click or repeaterPartsToSchedule_ItemCommand) isn't fire!
Hope I was explicit enough and sorry for my english
Thanks
Verify that you don't bind your control every time,
You can use in your bind this bloc
If(! IsPostBack)
{
}

Why are my ASP.NET webforms controls not rendering?

I have a feeling I'm missing one small thing. I have very simple page, created from the ASP.NET templates in VS2010. My Default.aspx consists of simply the following code. The Site.Master page is doing what it's supposed to.
<%#Page
Title="Home Page"
Language="C#"
MasterPageFile="~/Site.master"
AutoEventWireup="true"
CodeBehind="Default.aspx.cs"
Inherits="UserControlTest._Default" %>
<%#Register
TagPrefix="tsi"
Namespace="UserControlTest.Controls"
Assembly="UserControlTest" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"></asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<!-- HERE BE DRAGONS -->
<tsi:BigHelloBanner runat="server" />
<tsi:SmallHelloBanner runat="server" />
</asp:Content>
BigHelloBanner contains this:
<%#Control
Language="C#"
AutoEventWireup="true"
Visible="true"
CodeBehind="BigHelloBanner.ascx.cs"
Inherits="UserControlTest.Controls.BigHelloBanner" %>
<h1>HI!</h1>
Both the codebehind files in both objects are empty, and inherit from UserControl. The behavior is the same inheriting from Control. When I view-source on the rendered output, nothing from the HelloBanners is output, except some newlines. The HERE BE DRAGONS comment is visible, which indicates to me that the master page and all that works fine. I am expecting to see the <h1>HI!</h1> markup in the output as well. What am I missing? This seems really basic.
It looks like that you're referring to the empty code-behind class instead of the ASCX file with the output. Use the src attribute in your #Register directive:
<%#Register
TagPrefix="tsi"
TagName="BigHelloBanner"
Src="BigHelloBanner.ascx" %>
I can't see the src attribute here where is your control held ?
<%#Register
TagPrefix="tsi"
Namespace="UserControlTest.Controls"
Assembly="UserControlTest"
src="?" %>
Since BigHelloBanner is a web user control, you should try registering it like this:
<%#Register TagPrefix="tsi" TagName="BigHelloBanner" Src="~/pathToUserControls/BigHelloBanner.ascx" %>
Don't you still need to give an ID to each control instance?

Categories

Resources