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

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");

Related

asp:hyperlink href won't show up even though i populate navigateurl

Does anyone know what is wrong with the following two lines of code? In both cases, there is no href in the anchor links when i view it in the browser:
<asp:HyperLink runat="server" NavigateUrl='<%# Eval(Request.QueryString["conferenceId"], "~/Cms/schedule-edit.aspx?conferenceId={0}&type=workshopStream") %>' Text="Create Workshop Stream"></asp:HyperLink>
<asp:HyperLink runat="server" NavigateUrl='<%# String.Format("~/Cms/schedule-edit.aspx?conferenceId={0}&type=scheduleItem", Request.QueryString["conferenceId"]) %>' Text="Create Schedule Item"></asp:HyperLink>
This exact same code seems to work fine when I put it into the ItemTemplate of a Listview though. But it doesn't work when used on it's own in an aspx file.
What's wrong with it?
Also if i replace the navigateUrl with a hardcoded string ~/cms/schedule-edit.aspx?conferenceId=2&type=stuff then the href shows up. It just doesn't work when i have the Eval or String.Format in there.
If those HyperLink server controls are located outside of DataBound controls like GridView, there are two problems in our code -
You want to use <%= %> instead of <%# %> which is used in DataBound controls.
You cannot use <%= %> to set property of a server control. Basically, you cannot mix runat="server" with <%= %>.
Solution
<a href='<%= String.Format("~/Cms/schedule-edit.aspx?conferenceId={0}&type=scheduleItem",
Request.QueryString["conferenceId"]) %>'>Create Workshop Stream</a>
The anchor syntax NavigateUrl='<%#...%> is only valid inside a GridView, ListView, etc. When it is not inside such controls, you can set its NavigateUrl property via code. Obviously, you also need to give an ID to your HyperLink.
The markup:
<asp:HyperLink ID="HyperLink1" runat="server" Text="Create Schedule Item"></asp:HyperLink>
The code behind:
HyperLink1.NavigateUrl = String.Format("~/Cms/schedule-edit.aspx?conferenceId={0}&type=scheduleItem", Request.QueryString["conferenceId"])
You are using a data-binding expression here. This is denoted in the following syntax:
<%# [code] %>
The code inside is evaluated only when the containing control, or any of its ancestors have their .DataBind() method called.
To work this around, you can:
Call Page.DataBind()
This could have some unwanted consequences if you have other data-bound controls on the page, as this method will cause all of them to have their data-binding events fired. Usually, this approach is applied if you have minimalistic code-behind and the entire page relies on data-binding expressions.
Give each HyperLink an ID and call HyperLinkID.DataBind();
Stick to the approach in codingstill's answer by setting the NavigateUrl property in the code behind of your page/user control.

ASP.NET - Hyperlink relative path to current URL on ASPX page not in code behind

I tried this one but the href code is not generated
<asp:HyperLink ID="hlPrev" NavigateUrl="<%# this.Request.Url %>" runat="server" />
Is there any way to do this on aspx page not in the code behind?
If hlPrev is located outside of DataBound controls like GridView, there are two problems in our code -
You want to use <%= %> instead of <%# %> which is used in DataBound controls.
You cannot use <%= %> to set property of a server control. Basically, you cannot mix runat="server" with <%= %>.
Solution:
Click Me

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 does Page.ParseControl not create control inherited from the correct base class?

I dynamically create a User Control from XML via XSLT. Output is a string with a content like this:
<%# Control Language="C#" AutoEventWireup="true" Inherits="Library.Web.UI.GeneratedFormBase, MyAssembly" %>
<div class="myCssClass">
<asp:TextBox ID="d" runat="server" OnTextChanged="OnTextChanged" />
<asp:Label runat="server" AssociatedControlID="SomeName" AccessKey="n">Label Text</asp:Label>
<asp:TextBox ID="SomeName" runat="server" OnTextChanged="OnTextChanged" />
<asp:Label runat="server" AssociatedControlID="SomeOtherName">Welcome</asp:Label>
<asp:TextBox ID="SomeOtherName" runat="server" OnTextChanged="OnTextChanged" />
<asp:Button ID="OK" runat="server" OnClick="ButtonClick" Text="Save" />
</div>
I now use Page.ParseControl(theGeneratedString) to create this control dynamically.
The type that is declared in Inherits is existing and can be found. If I declare another (i.e. non-existing) type there, a Parser Error Exception is thrown, so I am totally convinced that the parser looks for this type and finds it.
Nevertheless, the Control that is generated from the ParseControl is of type System.Web.UI.Control and not of the control that is stated (and obviously also parsed and located) in the Inherits-declaration.
Why is that and how can I ensure that the control is of the correct type?
Okay, after using a bit reflector it seems obvious why the Control is of the 'wrong' class. So ParseControl is simply the wrong method to do this. The correct one is LoadControl, but to use it I need to provide the generated form via a VirtualPathProvider. So it's a lot more work to get the control to parse correctly, but when using this approach the control is loaded, parsed, compiled and derived from the correct type.

Aspx Property Interpolation

I'm a bit new to .Net development, been working in Java for some time now. I have an aspx page and we need to externalize some strings to make it more flexible.
If I have a table somewhere and there is just a string sitting outside an asp tag, I can replace it so that
<th> Specific Foo String </th>
becomes
<th> <%= Strings.foo %> </th>
and everything is fine, the problem I'm running into is how do you do this kind of interpolation on an asp tag property
I tried changing
<asp:Label runat="server" ID="lblFoo" Text="Specific Foo String Entry" />
to
<asp:Label runat="server" ID="lblFoo" Text='<%= Strings.foo %> Entry' />
and
<asp:Label runat="server" ID="lblFoo" Text='<%#Eval("Strings.foo") %> Entry' />
but neither worked. Is what I'm doing not possible in the aspx file, I know that I can simulate this by rewriting their properties in the code behind, but that's a level of overhead I'd rather not deal with.
Thanks
I think you are looking to do this:
<asp:Label runat="server" id="label1" Text='<%# Strings.Foo + " Entry"%>' />
Then in your code behind (most likely in your OnPageLoad) you need to call
if(!Page.IsPostBack) Page.DataBind();
You need to be cautious however as calling DataBind on controls like textboxes or any labels that may have changed due to logic in the code behind will have their values overwritten with the bound values. Checking that you are not on a post back can help with this, but there are still gotchas.
Also note that I had to move the " Entry" text into the binding statement. If it is placed outside the last '%>' then the binding does not work and it will spit out:
<%# Strings.foo %> Entry
In the codebehind of the page you would do this:
lblFoo.Text = Strings.foo + " Entry";
A good place to put this code would be in the overriden OnLoad method but that is simply a suggestion as I am unfamiliar with your application and the life cycle needs of your page.
If you want to do all this in the aspx page then simply do this:
<span><%= Strings.foo %> Entry</span>
as a Label renders as a span anyhow.
If your objective is an HTML table of strings, then you can create either a ListView or a GridView and DataBind to that. It would save you the trouble of writing out all of your properties and will also produce the correct table tags for the data.
Without knowing more about your data, I cannot provide a detailed code snippet.
You're talking about resources. Read Basic Instincts Resources and Localization in ASP.NET 2.0 which shows you the built in resource editor, and how to use the "<%$ ... %>"-binding, or using meta:resourceKey attribute.

Resources