I know that there's always been issues with declaring server-side controls directly in HTML in Visual Studio. Usually, you have to open the page in design mode in order to get them generated in code behind so you can access via your code. But I do have a strange issue here. I added a asp:Label control on a page that inherits a master page.
<asp:Label ID="uxEnteteresultat" runat="server" Text="Test"></asp:Label>
These pages use a framework named Ext.Net that generates Ext.js code. My designer simply doesn't work, so there's no way I can get this control generated in the codebehind file.
And where are those controls declared ??? In older version of .Net (I'm in VS 2010 using 4.0 framework), we could see it in a partial class. Where are they now ? Is there a workaround to declare your control yourself in VS 2010 ?
Thanks !
There is no designer.cs files at all. It is a website, not a project. That might be a part of the problem...
In VS 2010 each Form should have three parts:
MyForm.aspx
MyForm.aspx.cs
MyForm.aspx.designer.cs
You should be able to add a Label like so:
Add this to MyForm.aspx:
<asp:Label ID="lblOrganizer" runat="server" />
Add this to MyForm.aspx.designer.cs (within public partial class):
protected global::System.Web.UI.WebControls.Label lblOrganizer;
Controls declared inside mypage.aspx page are created in a partial class inside mypage.aspx.designer.cs
/// <summary>
/// uxEnteteresultat control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label uxEnteteresultat;
If your controls are not declared in this file will not appear in codebehind mypage.aspx.cs. Remember that all controls should have the property runat = "server" in its declaration in HTML.
Check this similar question:
Asp.net controls are not accessible in code behind
If you're getting intellisense then you're fine because you shouldn't have to explicitly declare your controls in the code-behind since ASP.NET 2.0 (partial classes). If you can't access it, check your designer file and manually add it there per
user1694951.
Related
I need to access multiple textboxes on a master page. The textboxes are declared in a separate web control and it won't let me call the names of the textboxes so I can populate them with data. I have multiple textboxes like I said, but for example, when I try to write to txtName it will not let me even though when I click on the Design View it says it is there.
Can anybody help me out?
Expose the text boxes inside the web control as properties.
In webcontrol.ascx
<asp:TextBox runat="server" id="txtName" />
In webcontrol.ascx.cs
public virtual TextBox TxtName { get {return txtName;} //note capitalization
Then do the same thing in the master page to expose the web control.
In masterpage.master
<uc1:MyWebControl runat="server" id="MyWebControl1" />
In mastermage.master.cs
public virtual MyWebControl myWebControl{get {return myWebControl1;}}
Then make your master page strongly typed from the content page by adding a MasterType directive.
In default.aspx
<%# MasterType TypeName="MyMasterPageClass" />
Then you can access it from your content page code behind. In default.aspx.cs
Master.myWebControl.TxtName.Text="Hello, world!";
The reason it's necessary to do this is that controls declared on .aspx, .ascx, and .master pages are protected instead of public and there's no way (as of right now) to change them that I'm aware of. So we can use properties to expose these controls as public.
Could you explain me the difference between controls and html components with the "runat=server" attribute. What is best practice to use it? Thanks.
<span runat="server" id="myspan"> </span>
maps to the GenericControl class defined in System.Web.UI.HtmlControls. From it's msdn description:
The System.Web.UI.HtmlControls namespace contains classes that allow
you to create HTML server controls on a Web Forms page. HTML server
controls run on the server and map directly to standard HTML tags
supported by most browsers. This allows you to programmatically
control the HTML elements on a Web Forms page.
<asp:Label runat="server" id="asplabel"></asp:Label>
maps to the Label class in System.Web.UI.WebControls. From it's msdn description
The System.Web.UI.WebControls namespace contains classes that allow
you to create Web server controls on a Web page. Web server controls
run on the server and include form controls such as buttons and text
boxes. They also include special-purpose controls such as a calendar.
Because Web server controls run on the server, you can
programmatically control these elements. Although Web server controls
are rendered as HTML, their object model does not necessarily reflect
HTML syntax.
The System.Web.UI.WebControls namespace contains classes that are
rendered as HTML tags, such as the TextBox control and the ListBox
control. The namespace also contains classes that are not rendered on
the Web page, but support data operations, such as the SqlDataSource
and ObjectDataSource classes. Other controls, such as the GridView and
DetailsView controls, support data display and editing. The WebControl
class serves as the base class for many of the classes in the
System.Web.UI.WebControls namespace.
Basically the controls from the WebControl namespace gives you more features where the design aim was to mimic VB6 forms as closely as possible to make the transition (back asnd forth) between winclient and webclient programming seamlessly.
I personally tend to use controls from the WebControls namespace as much as possible and only fallback to the HtmlConttols if I need behavior that is not availbale on a WebControl and not overrideable.
A blog from K. Scot Allen on the same topic
Server controls are tags that are understood by the server.
There are three kinds of server controls:
HTML Server Controls- Traditional HTML tags
Web Server Controls - New ASP.NET tags
Validation Server Controls - For input validation
HTML Server Control:
HTML elements in ASP.NET files are, by default, treated as text. To make these elements programmable, add a runat="server" attribute to the HTML element.
Example:
<form runat="server">
......
</form>
Web Server Controls:
Web server controls are special ASP.NET tags understood by the server.
Like HTML server controls, Web server controls are also created on the server and they require a runat="server" attribute to work. However, Web server controls do not necessarily map to any existing HTML elements and they may represent more complex elements.
Example:
<asp:Button ID="but" runat="server"/>
Validation Server Controls:
Validation server controls are used to validate user-input. If the user-input does not pass validation, it will display an error message to the user.
Example:
<asp:RequiredFieldValidator ID="req1" runat="server" ControlToValidator="TextBox1" ErrorMessage="please enter proper details"/>
If I have a TABLE or a DROPDOWNLIST, with an ID="Test", in a Page Default.aspx, contained in a Master Page, these controllers change their ID's to "MainContent_Test", means any CSS attributed to #Test won't work, and I feel that I'm having problems in C# as well, why does this happen? and How can I prevent it?
If you are using asp.net 4 and above you can set the ClientIdMode of the control
This will ensure that it won't change and can be assessed via css.
<asp:DropDownList ClientIdMode="Static" ID="Test" runat="server"/>
ASP.NET adds name of control containers to generated client ID. But if you use .NET 4.0 or later you can use cotntrol's ClientIdMode property:
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode(v=vs.100).aspx
Just set it to Static and the ID will not change.
Other options may include defining style via class and not attaching it to specific control ID.
Host is SharePoint 2010, Using VS2010 .NET 3.5. I have custom ASCX controls living in Web Parts. The controls inherit from a POCO class that inherits from System.Web.UI.UserControl. This control has the following code:
protected override void OnPreRender(System.EventArgs evArgs)
{
var myliteral = Page.FindControl("myliteral");
base.OnPreRender(evArgs);
}
and every .ascx has this content:
<asp:Literal ID="myliteral" runat="server" Visible="false"><br /></asp:Literal>
I set a breakpoint to see if "myliteral" in the C# code would have a reference to the generated control, but it does not, it is coming out null. Why?
Thanks.
Page.FindControl doesn't look recursively in nested containers, so if myLiteral1 is in ascx used by webpart.
http://msdn.microsoft.com/en-us/library/31hxzsdw(v=vs.90).aspx
I have a user control that has a public property defined in the code-behind (.ascx.cs). Is there a way for this property to be set directly inside the tag of the .aspx file that the control is being used? I can already access the property in the code-behind of the .aspx file.
Unless I'm missing something obvious, then it's easy...
<uc1:MyCtrl runat="server" MyVal="this value" />