C# ASP.NET Custom Control not showing up - c#

I'm trying to construct a custom control for ASP.NET
I started by creating a Web Application in VS2010 and creating a new .ascx page. The page is called "TestBox" and it's just a single TextBox control with "This is a test" as the text.
I built the project and then included the DLL in another website in order to make sure I would be able to move controls. Based on a tutorial I found here I added the following line of code to the top of the page:
<%# Register TagPrefix="TestControl" Namespace="TestControl" Assembly="TestControl" %>
Then I added this to the page itself:
<TestControl:TestBox ID="TestBox1" runat="server" />
The code compiles and the page loads without throwing up any errors, but when it loads it's completely blank. By introducing a deliberate runtime error, I determined that the TextBox is definitely being loaded, but the control itself still isn't showing up.
Am I missing something?
Code for the TestControl:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="TestBox.ascx.cs" Inherits="TestControl.TestBox" %>
<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged">This is a test</asp:TextBox>
I haven't touched the Designer code or the .cs code in any way.
EDIT: Figured it out. I had declared a namespace for the .CS file but not the .ASPX file itself.

The answer was that I had to add a namespace to the ASPX file itself and not just the underlying code file. I forgot to add Class="TestControl.TestBox" to the page declaration.

Related

Double Clicking Asp button in Designer mode creates function in unwanted aspx.cs fle

On double clicking the asp:Button in Design mode, a button click function is generated in .aspx.cs file of some other .aspx file. How do I make it such that on double clicking the button, it opens in the same page's cs file?
Note: I am using Visual Studio 2022 (free)
Example :-
I have 2 web pages:-
web.aspx (web.aspx.cs)
web1.aspx (web1.aspx.cs)
Now if I double click on a button in design mode of web.aspx file, then a button_click function is being created in web1.aspx.cs file, but Onclick event is being added to the button in web.aspx.
How do I make it such that on double clicking, the function is created in web.aspx.cs itself ?
Most likly the issue is that new page was NOT created by Visual Studio, but you made a copy of the page.
Check the topmost line in the markup page.
so, say the page is called MyUpLoadTest2.aspx
(the "2" kind of gives away the fact that I made a copy of this page).
Note this part:
<%# Page Language="C#" AutoEventWireup="true"
CodeBehind="MyUpLoadTest2.aspx.cs"
Inherits="CSharpWebApp.MyUpLoadTest2" %>
Check both codeBehind, and make sure it matches the page name (MyUpLoadTest2).
And also check the Inherits in above - again make sure it matches the code behind.
Also, view the code page, and make sure the class name for the page matches the page name.
eg this:
public partial class MyUpLoadTest2 : System.Web.UI.Page
So, make sure all 3 match.
if the page directive in the markup does not match the correct name, or is pointing to a different code file, then you see the effect you note - the code will be created in the wrong page.
Another issue/thing to avoid?
Don't name the page the SAME as any class you have - including built in ones, as that can say mess things up.
So, for example, you might try to call a page GridView, but there is a gridview control, and thus you don't want your page to have the same name, since then the code behind page class will wind up with the same name as that existing class (built in ones, or even control names).
So, check your page directive.
In many cases, you are better off to create a new blank page, and then cut + paste in the markup between the "form" tags, and leave the rest of the page alone.

User Control - (An entry with the same key already exists)

I am working on a SharePoint 2016 Project which uses SharePoint Application pages. We have a separate user control to which is used to capture address and it is used in other parts of the application.
In order to use this, I have added the user control in to the SharePoint application Page as follows.
<%# Register TagPrefix="uc" TagName="StreetAndPostalAddress" Src="~/_controltemplates/15/Test.Features.WebParts/QAS/QASAddressStreetAndPostal.ascx" %>
And tried to use this control in the page as below.
<uc:StreetAndPostalAddress id="locationAndPostalAddress" runat="Server" />
As soon as I add this control to the page it gives me an error as below. I have tried but was unable to figure out a clue on this. Main reason was same control was working on a another page. Please share your thoughts to resolve this.
Thank you.
I noticed the page used a update panel as follows
<asp:UpdatePanel ID="UP_Accordion" runat="server" UpdateMode="Conditional" ClientIDMode="Static" >
Any Idea on what is the use of it?
First Solution
It was defined <%# Control ClientIDMode="static" > in the page and once I change it to <%# Control ClientIDMode="Predictable" > the issue got resolved. But the functionality of the page is not working. Because JavaScript/Jquery is not working. Which Still did not resolve my issue :(
Finally I figured out why the page is not working. It consists of a accordion and a Grid. And I had a requirement to repeat a same user control. Due to the ClientIDMode which was set to static I had the above error. I read this areticle to understand the concept of ClientIDMode and removed it from the page. Use below link to learn it.
1) https://weblog.west-wind.com/posts/2009/nov/07/clientidmode-in-aspnet-40
Once I removed the ClientIDMode it seems my jquery in the page is not working. I had to change the jquery selector for ID and had select using a pattern match. Once changed the page rendered correctly.
2) How to stop ASP.NET from changing IDs in order to use jQuery

ASP/C# code behind can not reach control from markup

Issue:
I have a markup like this (only the important lines):
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="RTDeluxe.ascx.cs"
Inherits="MainSolution.CONTROLTEMPLATES.Kunde.RTDeluxe" %>
<ul id="linkUl" class="teaserLinksUL" runat="server"/>
The code-behind:
namespace MainSolution.CONTROLTEMPLATES.Kunde
public partial class RTDeluxe : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
linkUl.InnerHtml = string.Empty;
}
}
I can access the ul inside the code-behind and get no compilation error. But, when I debug the code I get a NullReferenceException because linkUl is NULL.
First I thought that the namespaces are the reason. But, after several tries, I'm sure that they're correct. The FileLocation seems to be correct and the controltemplates folder of my iis has a "Kunde" folder with the corresponding ascx files in it.
I have other .ascx files with the same structure -> they're working like a charm.
Question:
Are there any other reasons than the namespace for such behaviour? Do you have any hints where I can look at?
Edit:
The RTDeluxe.ascx.designer.cs file exists, the generated linkUl looks like this:
protected global::System.Web.UI.HtmlControls.HtmlGenericControl linkUl;
Edit2:
Ok, I will try to answer all your questions. Thanks for your time and feedback!
I have restarted Visual Studio -> The Problem persists.
I also have cleaned up the solution and deployed a new one. -> The problem persists.
When I debug and check the control hierachy I can see that the label is NOT there.
When I change the ID the compiler throws an error in the code-behind (which is right). If i change the ID there two I get the same behavoiur as before.
I also restarted my IIS and the whole pc -> No changes.
I have added a Name attribute to the linkul-definition -> No changes.
When I try to use FindControl it returns NULL.
The target-framework is .NET 3.5
The linkul is NOT inside a repeater or any other controls.
Removing/changing the web.config does also not lead to a solution.
Adding EnsureChildControls before accessing the linkUl doesnt change anything.
Moving the code into Page_PreRender does also not work.
I will try out your suggestions not listed here and add them soon.
Edit3:
Here the full markup:
<%# Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%# Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="RTDeluxe.ascx.cs" Inherits="MainSolution.CONTROLTEMPLATES.Kunde.RTDeluxe" %>
<ul id="linkUl" class="teaserLinksUL" runat="server"/>
*Edit4:
Ok here some additional info I found out:
When I change something in the markup, like adding plain html text it's NOT recognized by or shown in the browser. When i do something like this:
Label label1 = new Label();
label1.Text = "hugo lives!";
Controls.Add(label1);
It is shown. It seems like in visual studio everything is fine... But "live" at the server the code-behind speaks to some weird different markup...
This might help You a bit:
On the code-behind file of the user-control at class level, add this code:
protected global::System.Web.UI.HtmlControls.HtmlGenericControl linkUl = new System.Web.UI.HtmlControls.HtmlGenericControl();
and remove the protected global::System.Web.UI.HtmlControls.HtmlGenericControl linkUl; from RTDeluxe.ascx.designer.cs file
It might be because its Object was just declared not created.
Hope this helps..
Have you tried calling EnsureChildControls before accessing the control or moving the code into OnPreRender?
Sometimes in situations such as this it may just be that Visual Studio has got into a bad state with regard to this file. I have found that deleting the files and recreating them will often resolve the issue. Make sure to copy the code somewhere so that you can paste it back into the newly created files.
Are you deploying using the visual studio publish functionality?
If so, try deleting the .ascx file on your destination server. I have had visual studio not recognize that the file has changed and then doesn't copy the new file over.
I've had a problem similar to this before in an .aspx file. It was caused by having optimizeCompilations set to true in my web.config file.
<system.web>
<compilation optimizeCompilations="true" />
</system.web>
When optimizeCompilations is set to true ASP.Net only rebuilds pages when it feels it is necessary. Sometimes it gets confused and doesn't realize you've made a change that needs a page rebuild resulting in a run-time error that the compiler doesn't catch. See here for more information about this setting http://msdn.microsoft.com/en-us/library/ms366723.aspx.
To fix the problem I had to temporarily set optimizeCompilations to false, rebuild my website, recycle my app pool, and then set it back to true again.
Hope this helps.

Editing Aspx Website Template with C#

I have a template for a website, and I want to edit the aspx files with C#,
thats means that I want each of the aspx files have a code behind file, which is .aspx.cs file for each .aspx exist file.
I opened a new ASP.NET AJAX Website Template and copied the .aspx files, the webconfig and the css to the new website I created.
when I add control and double-click on it in order to create a .aspx.cs file for this page,
it brings me to the source code.
I've added this line as the first line of my aspx file in order to create a .aspx.cs file:
<%# Page Language="C#" AutoEventWireup="true"
CodeFile="Login.aspx.cs" Inherits="Login" %>
but still it dont let me create an aspx.cs file. Does someone know how can I do that?
Right click the folder your ASPX page is in (or the root project if it's in the root folder) and pick "Add New Item..."
Choose "Class", name it Login.aspx.cs, and click "Add"
If you're using a Website Project, it will ask you if you wanted to put the code file in app_code. Answer no.
Change the class declaration to match the Inherits property in your page directive, be a partial, and inherit from WebForms' Page base class:
public partial class Login : System.Web.UI.Page
{
}

Dynamically adding control to master page

I have a problem with dynamically loading control's into master page's ContentPlaceHolder.
Name of the ContentPlaceHolder and path of the UserControl is loaded from db and send to this little code:
Control c = this.Page.LoadControl(uc-path-from-db);
this.Page.Master.FindControl(cph-name-from-db).Controls.Add(c);
When I run it, I get this error:
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
What am I doing wrong?
Its not totally clear without your markup, but it is telling you that you can't Add the control in there because some of your markup contains a <% ... %> block which tells ASP.NET to execute whatever is in there to create your markup. Try removing your <% ... %> block and it should let you Add the control. Or post your markup for a more complete answer.
Depending on the type of Web Project you're working with, you have to be sure to <%# Register%> your UserControl on both the MasterPage you're loading it to, and the Page you're loading it from. If you're working with a web site and not a web application it compiles each page into it's on little assembly and won't reference the usercontrol's assembly unless it sees that Reference tag in the markup.
I'm not 100% on this, but it's worth a look.

Categories

Resources