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
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.
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.
I created a server control which consist only of fews buttons.
CWNavigation.vb
<ToolboxData("<{0}:CWNavigation runat=""server""></{0}:CWNavigation>")> _
<DefaultProperty("Id")> _
Public Class CWNavigation
Inherits WebControl
I then referenced it in my ASPX page. Take note that the control are in the same solution, same project located in Commun/Navigation/CWNavigation.vb.
<%# Register TagPrefix="NAV" TagName="CWNavigation" Src="~/Commun/Navigation/CWNavigation.vb" %>
I added it to the page.
<NAV:CWNavigation ID="CWNavigationService" runat="server" />
But the designer file along with the code-behind generate it as.
Protected WithEvents CWNavigationService As Global.System.Web.UI.UserControl
But this is wrong.. it must be CWNavigation. Is there anything ive done wrong ?
Thanks!
Since its a custom Server Control you should register it as an assembly. Something like this...
<%# Register Assembly="Control.Assembly.CWNavigation" TagPrefix="NAV" TagName="CWNavigation" Namespace="Namespace.Of.Control.Assembly" %>
Or add it to your ToolBox (Context Menu->Choose Items) and then drag and drop (which will have Visual Studio wire it up for you).
Is it possible to create an ASP.NET server control that packages some javascript that will be emitted to the aspx page at design-time rather than run time? I am trying to create a control that will have "default" javascript. I can add jvascript using RegisterClientScriptBlock, but then a web developer can't modifiy the javascript - it is unavailable a design-time in this scenario. Is there a way to change the ToolBox properties so that when a web developer drops the control onto the page, the javascript is added in a separate script tag as well?
When I need to do this I make a property on the control that will inject the tags, and then the web dev makes a asp:literal tag that has visibility none and viewstate disabled, that has all the JS they need.
then in the page's code behind they inject the literal's text into the server controls properties.
<asp:Literal ID="Literal_HtmlHeader" runat="server" Visible="false" EnableViewState="false">
<script></script>
<style></style>
</asp:Literal>
there are probably better ways... but this is simple and effective for me.
protected void Page_Load(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(this.Literal_HtmlHeader.Text.Trim()))
{
//inject css and js into header.
Page.Header.Controls.Add(new LiteralControl(this.Literal_HtmlHeader.Text));
// or add to your control cause it knows how to add the tags so there is no duplication.
ServerControl c = new ServerControl();
c.HtmlHeaderCode = this.Literal_HtmlHeader.Text.Trim();
}
}
I have an extremely simple control i'm trying to render which looks like this:
using System;
using System.Web;
using System.Web.UI;
namespace CORE.BusinessObjects.Web.Controls
{
public class TestControl : Control
{
protected override void Render(HtmlTextWriter writer)
{
writer.Write("Hello from TestControl!");
}
}
}
I'm calling the control in the following manner:
<%# Register TagPrefix="Custom"
Namespace="CORE.BusinessObjects.Web.Controls" %>
<Custom:TestControl ID="testControl" runat="server" Visible="true">
</Custom:TestControl>
Am i doing something wrong? I have also failed to run ANY control samples i found online. Nothing executes. I can only execute the constructor of the control. Every other method i tried to override like Render() or CreateChildControls() doesn't get executed.
Thanks.
EDIT: I forgot to mention that the control is included on a page with a Master page. The control actually runs fine in the Master page, but not outside of it.
You should try the following:
a. Inherit from the System.Web.UI.Control.WebControl class.
b. Make sure that you create a control library and your custom control library is compiled and referenced in the relevant web project. Optionally, add it to the toolbox and drag it onto the form from there. That should also add the reference in one step.
c. Make sure that your control declaration has the Assembly attribute which points to the name of the referenced control assembly:
<%# Register TagPrefix="Custom" Assembly="CORE.BusinessObjects" Namespace="CORE.BusinessObjects.Web.Controls" %>
<Custom:TestControl ID="testControl" runat="server" Visible="true" />
Trying inheriting from CompositeControl or WebControl