cannot access controls in asp.net application - asp.net

I am working on an ASP.NET VB.NET Web Application. I inherited a bunch of forms from another application we have in house. I'm running into a very strange problem when working on the Login page.
This is an abbreviated version of my code:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<table>
<tr>
<td>
<span id="Span1" runat="Server" style="Color: Red"></span>
</td>
</tr>
<tr>
<td>
<asp:Login ID="Login1" runat="server">
<LayoutTemplate>
<table>
<tr>
<td>
<span id="Span1" runat="Server" style="Color: Red"></span>
</td>
</tr>
</table>
</LayoutTemplate>
</asp:login>
</td>
</tr>
</table>
</asp:Content>
I have a <span id="span1"> that is located inside my web form, within the Content part of the page. I can easily access this in my CodeBehind, and do whatever I want to do with it. However, if i move that span and put it inside the <asp:login> part of the page, it doesn't seem to recognize it, it won't let me access it in code behind, it gives me a squiggly blue line and says
span1 is not declared. It may inaccessible due to protection level
This bit is from the top of the webform in designer
<%# Page Language="VB" AutoEventWireup="false" CodeFile="Login.aspx.vb" Inherits="_Default" MasterPageFile="~/Site.master" %>
This bit is from the Login.aspx.vb page
Partial Class _Default
Just to say it again, id="span1" works perfectly fine where it is shown in the code above, but when I move it inside the I cannot reference it anymore. Since I'm talking about this issue, for that matter I cannot add any new controls inside because I am not able to reference any other controls in vb.net. (this form was pretty much copied from another project, everything works properly I'm just not sure why I'm having this strange issue)
I noticed that a lot of people have similar issues, but in my case I'm working with <asp:login> and I'm really not sure how it's affecting my controls.
EDIT: <span id="Span1" runat="Server" style="Color: Red"></span>

You need to use FindControl on the Login1 Control
HtmlGenericControl hgc = Login1.FindControl("Span1") as HtmlGenericControl;
hgc.InnerText = "Span Found";
VB
Dim hgc As HtmlGenericControl = CType(Login1.FindControl("Span1"),HtmlGenericControl)
hgc.InnerText = "Span Found"

As per my comments, and as requested by the OP...
You're hitting a problem with the naming container.
When the <span runat="server"> is outside of the <asp:Login><LayoutTemplate> it exists as an object within the page, which you can reference directly.
As soon as it's moved within that <LayoutTemplate> it becomes a child of the <asp:Login> control instead.
So to access the control, you can use the following...
CType(Logon1.FindControl("span1"), HtmlGenericControl).InnerHtml = "hello"
The FindControl will bring back an object, but it needs to be "boxed" into the correct type before you can access the InnerHtml property

to access a control on server side, you must include the "runat='server'" attribute on a tag. That's what tells .NET that any given control is supposed to be worked with on the server side as well as the front-end.
Do note that it will change the ID produced in the rendered HTML
<span id="span1" runat="server"></span>

Related

Linkbutton in ASP.NET MasterPage and child Webform data control creating extra href=""

EDIT Final: Just in case someone else sees this behavior, I wanted to explain my work around. I was using a placeholder on my Master page for the webform described below. I added a server control to the placeholder in the Master page OnInit event. Through a process of elimination, I figured out that the behavior described below only happens when I add this server control to the Master page.
titlebarPlaceHolder.Controls.Add(sctitlebar)
I re-wrote the Master Page to not need the server control added and the behavior described below went away. I have no idea what caused it. It was a simple server control, but this is my work around.
EDIT 2: The same behavior happens when the container is a table in the repeater control:
<asp:Repeater ID="rptAuditList" runat="server">
<ItemTemplate>
<tr class="odd">
<td><asp:LinkButton ID="lnkOpenAudit" runat="server" Text='<%# Eval("auditname") %>'></asp:LinkButton> </td>
</tr>
</ItemTemplate>
</asp:Repeater>
Here is the HTML output:
<td>Demo PreClose July 2012 </td>
EDIT: In my testing I just noticed that if I run the extact same control OUTSIDE of a MasterPage, it worked correctly, but if I run it inside a MasterPage, it behaves in the way described below.
I have tried this with a Repeater, DataList and Listview and the results are always the same.
Here is the HTML:
<asp:ListView ID="lvwAuditList" runat="server" >
<LayoutTemplate>
<ul><li runat="server" id="itemPlaceholder"></li></ul>
</LayoutTemplate>
<ItemTemplate>
<li><asp:LinkButton ID="lnkAudit" runat="server" Text='<%# Eval("auditname") %>' >
</asp:LinkButton></li>
</ItemTemplate>
</asp:ListView>
Here is the output:
<ul>
<li>Demo PreClose July 2012</li>
<li><a id="contentMain_lvwAuditList_lnkAudit_1" href="javascript:__doPostBack('ctl00$contentMain$lvwAuditList$ctrl1$lnkAudit','')">Demo PostClose Audit June 2012</a></li>
</ul>
The first row always has an extra href="" added. I have never seen this behavior before. I have stripped down the html and code behind to its most basic, yet I still get this extra href="". The code behind just sets the datasource and binds it, nothing else.
Thank you.
If you are using the first list tag as a placeholder and replacing the content in the code behind I would use a slightly different approach adding an ASP.NET PlaceHolder inside the list tag as follows:
<ul><li><asp:PlaceHolder runat="server" ID="itemPlaceholder" /></li></ul>
I have experienced some unusual artefacts appearing in Response code when using ASP.NET tags in a manner for which they were not particularly intended. This may well be causing your problem as well!

Prevent Visual Studio from adding child controls

I have a bit of a strange issue here. I created a custom control as such:
public class Textbox : System.Web.WebControls.Placeholder
{
if( [certain criteria are met])
this.Controls.Add(new System.Web.WebControls.Textbox());
}
When working in Visual Studio's designer mode, anytime I add one of my textboxes to the page:
<myControls:Textbox id="txtTest" runat="server" />
Visual studio has a habit of changing my markup to the following:
<myControls:Textbox id="txtTest" runat="server" >
<asp:Textbox runat="server" />
</myControls:Textbox>
This changes the final output markup so that two textboxes appear. Is there a way to prevent visual studio from doing this?
EDIT:
As per request, here is the page markup
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="TestApp.Test" %>
<%# Register Assembly="MyControls.WebControls" Namespace="MyControls.WebControls" TagPrefix="myControls" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<table>
<tr>
<td>
<asp:Label ID="lblTest" runat="server">Test Label:</asp:Label>
<myControls:TextBox ID="txtTest" runat="server" Width="80px"></myControls:TextBox>
</td>
</tr>
</table>
</asp:Content>
I think that's happening because your class inherits from the PlaceHolder control, which implements the TemplateControl class. I don't know if there was a reason for this, but it seems like it would be more approriate if you inherited from the TextBox control instead.
EDIT
Since you're using logic to render one of several controls, I would inherit from the WebControl class instead. This will allow you render basically anything you want. Although I must admit, what you're trying to do is probably going to require a substantial amount of work.
If I understand the issue correctly, you are having problems at design time and not runtime.
If this is the case, you should wrap your control add code with a test that ensures it is only executed when you are not in design mode. For example:
if ((!this.InDesignMode) && ( [certain criteria are met]))
this.Controls.Add(new System.Web.WebControls.Textbox());

Why server controls are underlined when placed inside Content tags?

I have a Web Content Form containing a Div and various Server controls like DropDownList. When I run the application, it runs well without any errors, but when I view the HTML source, the Server controls are red underlined. On bringing the mouse over, say, DropDownList, a tooltip warning is displayed:
DropDownList is not a known element. This can occur if there is a compilation error in a website.
Edited
<%# Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="contentReportSchemesMenu.aspx.cs" Inherits="contentReportMenu" Title="Reports Menu" %>
<asp:Content ID="ContentReportMenu" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div id="divMenu" class="divMenu" runat="server">
<table id="tblMenuLayout" class="Options" runat="server">
<tr>
<td colspan="2" class="Top">Scheme Reports Menu</td>
<td></td>
</tr>
<tr>
<td class="Left">Report Type</td>
<td class="Right">
<asp:DropDownList ID="ddlReportType" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlReportType_SelectedIndexChanged"></asp:DropDownList>
</td>
</tr>
<tr>
<td class="Left">Select District</td>
<td class="Right">
<asp:DropDownList ID="ddlDistrict" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlDistrict_SelectedIndexChanged" Enabled="False"></asp:DropDownList>
</td>
</tr>
<tr>
<td class="Left">Select Block</td>
<td class="Right">
<asp:DropDownList ID="ddlBlock" runat="server" AutoPostBack="true" Enabled="False" OnSelectedIndexChanged="ddlBlock_SelectedIndexChanged"></asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" Enabled="False" />
</td>
<td></td>
</tr>
</table>
</div>
</asp:Content>
A quick Google search soon found the solution: Delete the files from “C:\Documents and Settings[Username]\Application Data\Microsoft\VisualStudio\9.0\ReflectedSchemas” folder (or “…\VisualStudio\8.0\…” if running Visual Studio 2005) in Windows XP. In Windows 7 it is under "C:\Users{User Profile}\AppData\Roaming\Microsoft...etc". Remember also the "VisualStudio" part of the path will be different depending on the version installed.
I closed Visual Studio (always a good ideas for changes that will affect the IDE), deleted the files then re-opened the project. The warnings were gone.
I found references to this solution at:
http://forums.asp.net/t/1205528.aspx
http://blogs.msdn.com/mikhailarkhipov/archive/2005/04/21/410557.aspx
FYI, the search term I used in Google was “element is not supported”.
I don't know why this happens but I do know there are some flakey domain profile things happening in the network environmnet.
You're receiving this error because the table is running at the server, but the tr and td elements are not. When you specify runat="server" on a table element, it expects child elements to run at the server as well.
There are two easy ways to verify this:
Remove runat="server" from the table declaration;
Take the DropDownList(s) outside of the table
Try one of these two options, and see if it fixes the problem.
EDIT
Make sure that the ContentPlaceHolderID on the content form matches the ID of the corresponding content area in the master page. If that doesn't fix your problem, try creating a new content form, applying the advice above, and add a control to the form in the content area. If there are no errors, then you know the issue is somewhere in your markup.
Try deleting the schema cache. To do this, close Visual Studio and delete all of the files in the following directory:
C:\Users\USERNAME\AppData\Roaming\Microsoft\VisualStudio\10.0\ReflectedSchemas
Once the files have been deleted, open Visual Studio again and the problem should be fixed.
You said that "when I view the HTML source, the Server controls are red underlined", but HTML source can not contains such elements like DropDownList because this is the ASP.NET control which is generated as HTML Select tag. Considering that common solution with schema cleanup is not helped you perhaps a problem is in an other place... I'll try to assume that you are opening ASPX/ASCX file itself using some third party editor which is not aware of ASP.NET controls, am I right?

asp:Repeater within asp:Content

I have the following piece of code:
<asp:Content id="Content1" runat="server" contentplaceholderid="PlaceHolderPageDescription">
<table class="custom-table">
<asp:Repeater ID="oRepeater" runat="server" >
<ItemTemplate>
<tr onclick="javascript:location.href='/nuovoTema/viewIdea.aspx?ID='">
<td><%# ((SPListItem)Container.DataItem)["ID"] %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</asp:Content>
The compiler complains that In content pages, content is not allowed outside <script> or <asp:Content> regions.
How can I use asp:Content and asp:Repeater together? I have a list to display.
Thanks
Doesn't look like the error is in the part of the page that you posted. Typically this pops up when you either attempt to place content outside of an asp:content block while in a web content page, or b) have some sort of markup error - lack of a closing tag or similiar - that makes the parser think that you're placing code outside of a content block.
Check your tags to make sure everything that needs to be closed is, in fact, closed. A good way to start is to simply delete everything inside of the content block - well, copy it somewhere - and see if the page still complains. (You'll probably get errors in the code behind).

ASP.NET Controls are not coming in Code-behind IntelliSense

I am having an aspx page where I have added one asp.net text box control with ID and RUNAT attribute. But in Code-behind I am not seeing this control's name in the intellisense.
My page directive in aspx is as follows
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MyProject_UI._Default" %>
I am using VS 2008. Any idea how to get rid of this?
Try using CodeFile instead of CodeBehind. The latter is a hold-over from .NET 1.1.
Also, make sure the namespaces match up between the markup and the code. Do a test compile to be sure.
I have seen this on occasion when I edit a page. When it happens to me, I close the files and open them again and it seems to fix itself.
This will happen if you are trying to include your control in LayoutTemplate. For example if you are using an asp label in a login control you have converted to a LayoutTemplate.
<asp:Login ID="userLogin" runat="server">
<LayoutTemplate>
<!--Username and password controls-->
<asp:Button ID="btnLogin" CommandName="Login" runat="server" Text="Login" />
<asp:Label ID="lblAlert" runat="server"></asp:Label>
</LayoutTemplate>
So your lblAlert will not show up on the code behind take it out of the layouttemplate or use a loop to find the control within the layout object.
var mylabel = (Label)userLogin.FindControl("lblAlert");

Resources