Not picking up id of control in code behind - c#

I have a basic webform(Default.aspx) that I created using and master page and bizarrely I am not able to access the id of a control in the code behind.
<ul id="topicsList" runat="server">
<asp:Label ID="labUserName" runat="server"/>
</ul>
IDomainObjectRepository rep;
protected void Page_Load(object sender, EventArgs e)
{
rep = new DomainObjectRepository();
rep.GetDomainObjectsByType("Visuals Product Label");
/*labUserName not picked up by intellisense*/
}
any pointers as to why this could be the case

The problem is likely due to your Code-front not being referenced by code behind, or vice versa. Make sure you have the following (double check the CodeBehind and code-behind class marry up):
public class _Default : Page
{
// Your load event
}
<%# Page Title="" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="_Default" %>

Related

Re-Binding Event-Handlers in ASP.NET 4.0 "Templated Control"?

Edit: Sadly, nobody seems to know. Maybe this will help clarify my dilemma: I'm trying to implement my own DataList type of control that supports switching from ItemTemplate to EditItemTemplate. The problem occurs when clicking on a button inside the EditItemTemplate -- it doesn't trigger the handler unless you click a second time!
Sorry about the lengthy post. The code is complete, but hopefully with nothing distracting.
I'm trying to create my own User Control that accepts multiple templates. I'm partly following techniques 39 and 40 from "ASP.NET 4.0 in Practice" by Manning. It seems to be working, except the button inside the template isn't bound to the handler until the second click (after one extra postback).
There are four files involved. Default.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%# Register Src="~/TheTemplate.ascx" TagPrefix="TT" TagName="TheTemplate" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<TT:TheTemplate ID="tt" runat="server">
<ATemplate>
<p>This is template A</p>
<asp:Button ID="TemplateAButton" OnClick="TemplateAButton_Click" runat="server" Text="Template A Button" />
</ATemplate>
<BTemplate>
<p>This is template B</p>
<asp:Button ID="TemplateBButton" OnClick="TemplateBButton_Click" runat="server" Text="Template B Button" />
</BTemplate>
</TT:TheTemplate>
<br />
<asp:Button ID="ToggleTemplate" Text="Toggle Template" OnClick="ToggleTemplate_Click" runat="server" />
</div>
</form>
</body>
</html>
Default.aspx.cs:
using System;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Trace.IsEnabled = true;
tt.DataBind();
}
protected void ToggleTemplate_Click(object sender, EventArgs e)
{
tt.TemplateName = (tt.TemplateName == "A") ? "B" : "A";
tt.DataBind();
}
public void TemplateAButton_Click(object sender, EventArgs e)
{
Trace.Write("TemplateAButton_Click");
}
public void TemplateBButton_Click(object sender, EventArgs e)
{
Trace.Write("TemplateBButton_Click");
}
}
And the user control, TheTemplate.ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="TheTemplate.ascx.cs" Inherits="TheTemplate" %>
<p>Using template <asp:Literal Text="<%# TemplateName %>" ID="Literal1" runat="server"></asp:Literal></p>
<asp:Placeholder runat="server" ID="PlaceHolder1" />
And finally, TheTemplate.ascx.cs:
using System;
using System.Web.UI;
[ParseChildren(true)]
public class TheTemplateContainer : Control, INamingContainer
{
private TheTemplate parent;
public TheTemplateContainer(TheTemplate parent)
{
this.parent = parent;
}
}
public partial class TheTemplate : System.Web.UI.UserControl, INamingContainer
{
public string TemplateName
{
get { return (string)(ViewState["TemplateName"] ?? "A"); }
set { ViewState["TemplateName"] = value; }
}
[TemplateContainer(typeof(TheTemplateContainer))]
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate ATemplate { get; set; }
[TemplateContainer(typeof(TheTemplateContainer))]
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate BTemplate { get; set; }
protected override void OnDataBinding(EventArgs e)
{
TheTemplateContainer container = new TheTemplateContainer(this);
if (TemplateName == "A")
ATemplate.InstantiateIn(container);
else if (TemplateName == "B")
BTemplate.InstantiateIn(container);
PlaceHolder1.Controls.Clear();
PlaceHolder1.Controls.Add(container);
EnsureChildControls();
base.OnDataBinding(e);
}
}
When you first run it, you will see ATemplate being used:
If you click on the Toggle Template button, all the text is correctly rendered:
But clicking on either "Template A Button" or "Template B Button" will not trigger the OnClick handler on the first try:
It will work on the second click:
Does the problem have to do with where .DataBind() is being called?
I'm not quite sure I understand it, but the problem has to do with how newly-added controls go through the "catch-up" events. Removing PlaceHolder1 and adding it programmatically solves the issue. TheTemplate.ascx becomes:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="TheTemplate.ascx.cs" Inherits="TheTemplate" %>
<p>Using template
<asp:Literal Text="<%# TemplateName %>" ID="Literal1" runat="server"></asp:Literal></p>
... and in TheTemplate.ascx.cs, replace OnDataBinding like this:
protected override void OnDataBinding(EventArgs e)
{
TheTemplateContainer container = new TheTemplateContainer(this);
if (TemplateName == "A")
ATemplate.InstantiateIn(container);
else if (TemplateName == "B")
BTemplate.InstantiateIn(container);
System.Web.UI.WebControls.PlaceHolder PlaceHolder1 = new System.Web.UI.WebControls.PlaceHolder();
//PlaceHolder1.Controls.Clear();
PlaceHolder1.Controls.Add(container);
this.Controls.Clear();
this.Controls.Add(PlaceHolder1);
EnsureChildControls();
base.OnDataBinding(e);
}
In the future, if I ever feel like I need to add controls dynamically, I will also create a PlaceHolder dynamically and use that as the root. When PlaceHolder is populated, I will then add it to the page.

Web User Controls in different Project

I put some self made Web User Controls in a seperate Project "WebControls" and now want to reuse them from another Project
My Control consists of:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="TestControl.ascx.cs" Inherits="WebControls.TestControl" %>
<asp:Label ID="lblTest" runat="server" ></asp:Label>
<asp:TextBox ID="textBox" runat="server" Width="" />
<asp:HiddenField ID="hiddenFieldId" runat="server" />
with Code Behind:
namespace WebControls
{
public partial class TestControl : System.Web.UI.UserControl
{
public Unit Width
{
get { return textBox.Width; }
set { textBox.Width = value; }
}
public string SelectedId
{
get { return hiddenFieldId.Value; }
set { hiddenFieldId.Value = value; }
}
public string SelectedText
{
get { return textBox.Text; }
set { textBox.Text = value;}
}
protected void Page_Init(object sender, EventArgs e)
{
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
I bind it into a Webpage in the other project like that:
<%# Page Title="ToDo Serien" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="True" CodeBehind="RecurringToDos.aspx.cs" Inherits="TestAccess.RecurringToDos" %>
<%# Register Assembly="WebControls" Namespace="WebControls" TagPrefix="aci" %>
<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
<section class="featured">
<div class="content-wrapper">
<hgroup class="title">
<h1><%: Title %>.</h1>
<h2>Serienelement</h2>
<aci:TestControl ID="aceRule" runat="server" Width="300" />
<asp:Button ID="btnSend" runat="server" OnClick="btnSend_Click" />
</hgroup>
</div>
....
When I now start the page it throws a Reference Null Exception in following line:
set { textBox.Width = value; }
becuase textBox = NULL
Seems my Control is not properly initiated.
What am I doing wrong?
How can I fix that?
If you want to reuse a ascx user control across multiple projects, you should copy ascx file to the consumer project and register the tag this way:
<%# Register TagPrefix="uc" TagName="UserControl1" Src="~/UserControl1.ascx" %>
As an example, you can follow these steps:
Create a new web project name it WebUserControls
Add a Web Forms User Control and name it UserControl1
<%# Control Language="C#" AutoEventWireup="true"
CodeBehind="UserControl1.ascx.cs" Inherits="WebUserControls.UserControl1" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
and in code behind add:
public string Text
{
get { return TextBox1.Text; }
set { TextBox1.Text = value; }
}
In the consumer project, add a reference to the project containing the ascx user control.
Copy .ascx file of control into the consumer project.
Note: You don't need to add the file to the project, but the physical file should exist in the path which you used as Src in registerting the tag.
In the page which you want to use the control, add this:
<%# Register TagPrefix="uc" TagName="UserControl1" Src="~/UserControl1.ascx" %>
Use the control this way:
<uc:UserControl1 runat="server" Text="UserControl1" />
Note
If you want to not copy ascx file, you can use Web Forms Server Control which doesn't rely on ascx files. Then for using those custom controls, it's enough to register them:
<%# Register Assembly="WebUserControls" Namespace="WebUserControls" TagPrefix="uc" %>
This answer relies on a great post by Scott Guthrie in this topic: Creating and Using User Control Libraries

ASP.NET web forms user control with DropDownList's SelectedIndex cannot be set

I have created a web forms user control with a DropDownList. I want to change SelectedIndex property of DropDownList1 to change the selected index.
WebUserControl1.ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.ControlUI.WebUserControl1" %>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
WebUserControl1.ascx.cs:
using System;
namespace WebApplication1.ControlUI
{
public partial class WebUserControl1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e) {
if (IsPostBack) return;
for (int i = 1; i <= 5; i++) {
DropDownList1.Items.Add("Test: " + i.ToString());
}
}
public void SetSelectedIndex(int index) {
DropDownList1.SelectedIndex = index;
}
}
}
Now I am using the user control in a page.
Default.aspx:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
<%# Register Src="~/ControlUI/WebUserControl1.ascx" TagPrefix="uc1" TagName="WebUserControl1" %>
<asp:Content ID="HeadContent" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<uc1:WebUserControl1 runat="server" id="WebUserControl1" />
</asp:Content>
Default.aspx.cs:
using System;
using System.Web.UI;
namespace WebApplication1
{
public partial class Default : Page
{
protected void Page_Load(object sender, EventArgs e) {
WebUserControl1.SetSelectedIndex(3);
}
}
}
This does not work. It assigns -1 into SelectedIndex property of DropDownList1. But the user control works if I add items into the DropDownList in the markup (WebUserControl1.ascx), rather than in the codebehind file (WebUserControl1.ascx.cs):
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.ControlUI.WebUserControl1" %>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Test: 1</asp:ListItem>
<asp:ListItem>Test: 2</asp:ListItem>
<asp:ListItem>Test: 3</asp:ListItem>
<asp:ListItem>Test: 4</asp:ListItem>
<asp:ListItem>Test: 5</asp:ListItem>
</asp:DropDownList>
But I need to add items using the codebehind file, not in the markup file. Why it is not working? How to solve the problem?
The issue is that Page_Load for the page containing the user control (Default) executes before Page_Load for the user control (WebUserControl1). Therefore, when SetSelectedIndex is invoked from the page, the drop down does not have any list item in it when the page is first built.
You can solve the issue very simply by creating the list item for the drop down in the Init stage of the user control life cycle rather than in the Load stage:
protected void Page_Init(object sender, EventArgs e) {
if (IsPostBack) return;
for (int i = 1; i <= 5; i++) {
DropDownList1.Items.Add("Test: " + i.ToString());
}
}

Pass int from aspx.cs to aspx page

I need to pass an int from aspx.cs page to aspx page and show it there
relevant part of example.aspx.cs page:
public partial class example : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected int Id(){
var Id = 318;
return Id;
}
}
Relevant part of aspx page
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="example.aspx.cs" Inherits="blahblah.example" %>
.
.
.
<body>
<h1>example.Id()</h1>
.
.
.
How can I edit this? It should be straight foward, but I can't seem to figure it out.
Simply do:
<h1><%=Id()%></h1>
This will display the return value of the method.
You may see: Introduction to ASP.NET inline expressions in the .NET Framework
Injecting Code <%= //some code here %> into the HTML it is possible with ASP.NET. However I will recommend to use a literal control instead to always keep the separation between the controller and the UI. Better may be:
Html:
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
Code:
Literal1.Text = "My Value"

Can I pass a class instance to a custom user control via the front end of a page/control in ASP.Net?

I have a custom user control that I am adding to a page via the front end (not back). That customer user control has a property that is of the type of a custom class. I would like to set that property via the declaration of the user control on the front end.
Here's some code to help you understand what I'm talking about:
In the custom user control .cs:
public BaseTUDSectionControl parentSectionControl
{
get;
set;
}
And at the declaration of an instance of that user control:
<tud:TUDDropDown ID="tddAccountExecutive" runat="server" />
I would like to set the parentSectionControl property in the same line, like this:
<tud:TUDDropDown parentSectionControl=someClassInstance
ID="tddAccountExecutive" runat="server" />
where 'someClassInstance' is a class instance that does exist... yeah. I think I am going about this the wrong way, but I don't know how else to accomplish this. I don't want to do it on the back end for maintenance reasons as I'll have hundreds of similar user controls added all throughout my project.
Is anything like this possible, or am I smoking something?
No that's not possible. You can only set property of basic types (int, string, bool) etc. from the markup (what you call front-end). If you have a property which is to be set to an instance of a class, this has to be done in the code-behind (what you call back-end).
I'm not sure if/when Microsoft added this functionality to ASP.NET, but it is in fact possible to set a non-basic-type property in code-front:
TestPage.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="TestPage.aspx.cs" Inherits="TestPage" %>
<%# Register Src="~/Control.ascx" TagPrefix="custom" TagName="Control" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Test Page</title></head>
<body>
<custom:Control ID="TestControl" Items='<%# Items %>' runat="server" />
</body>
</html>
TestPage.aspx.cs:
using System;
using System.Collections.Generic;
using System.Web.UI;
public partial class TestPage : Page
{
protected void Page_Load(object sender, EventArgs e)
{
TestControl.DataBind();
}
protected List<string> Items = new List<string>() { "Hello", "world!", };
}
Control.ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="Control.ascx.cs" Inherits="Control" %>
<asp:Repeater ID="Repeater" ItemType="System.string" runat="server">
<ItemTemplate>
<p><%# Item %></p>
</ItemTemplate>
</asp:Repeater>
Control.ascx.cs:
using System;
using System.Collections.Generic;
using System.Web.UI;
public partial class Control : UserControl
{
public List<string> Items { get; set; }
protected override void OnPreRender(EventArgs e)
{
Repeater.DataSource = Items;
Repeater.DataBind();
}
}
Note: it's important to bind the control from the page which sets the property. So you still need a line in code-behind, but you still reap the readability benefits of setting the property in code-front.

Categories