September 2005 - Posts
I am now sitting in the middle of a training given by Microsoft and ACT College on SQL Server 2005 (Yukon) in a very comfortable place in the Great Capital of Lebanon - Beirut.
More on the topics and issues discussed will be available online soon.
Regards
If you are working with either a new custom server control or a normal web user control, this is the proper way of adding an event that can be handled by the parent control of the child control you are develping:
Add the following to the control (Server Control / Web User Control ) being developed:
#region Event Handler
private static readonly object EventBubbleRaised = new object();
public event EventHandler BubbleRaise
{
add
{
Events.AddHandler(EventBubbleRaised, value);
}
remove
{
Events.RemoveHandler(EventBubbleRaised, value);
}
}
protected virtual void OnBubbleClick(EventArgs e)
{
EventHandler BubbleRaised = (EventHandler)Events[EventBubbleRaised];
if (BubbleRaised != null)
BubbleRaised(this, e);
}
#endregion
In the parent page / control, you add this in the Init method:
BubbleControl.BubbleRaise += new EventHandler(ControlName_BubbleRaise);
Then you define you our handler:
private void ControlName_BubbleRaise(object sender, EventArgs e)
{
Response.Write(sender.GetType().ToString());
}
That was a simple example, but at least you know now the steps.
Regards
I was trying to dynamically load a user control in an ASP.NET 2.0 project.
The problem was, the code-file could not be seen in my ASPX page.
To make this work, I did the following steps:
1- I put my usercontrol in a new normal folder called: App_UserControls,
2- Add this to the top of the ASPX page:
<%
@ Register TagPrefix="uc1" TagName="TestControl" Src="App_UserControls/test.ascx" %>
<%
@ Register TagPrefix="uc1" TagName="TestUC" Src="App_UserControls/test.ascx" %>
Then in my code-beside:
Control uc = this.LoadControl("~/App_UserControls/test.ascx");
((test)uc).Name = "Bilal Haidar";
phControls.Controls.Add(uc);
Now things are ok, but ...
The main problem is, why do we need to Register the user control in a time we are dyanamically laoding the user control?
Regards
In my previous post, I talked a problem in ASP.NET 2.0 using Master Pages.
I recommend using the following:
In the BaseClass of your aspx pages in ASP.NET, add the following:
protected Control FindPageControl(string ControlName, string ContentHolderName)
{
// Get the ContentPlaceHolder Control
ContentPlaceHolder _CPH =
(ContentPlaceHolder)this.Page.Controls[0].FindControl(ContentHolderName);
// Get the required control inside ContentPlaceHolder control
Control _ctrl = _CPH.FindControl(ControlName);
return _ctrl;
}
In your aspx page, you can find the control as follows:
((CheckBoxList)this.FindPageControl("chkDays","ContentPlaceHolder1")).Items.Count
Hope that helps,
Regadrs
There is something missing in ASP.NET 2.0 Master Pages Beta 2 in the way we access controls on the content page.
For example, I use the following lines of code to get a reference to the CheckBoxList (CheckDays) on a page placed inside the ContentPlaceHolder:
string ControlName = "ctl00$ContentPlaceHolder1$CheckDays";
CheckBoxList _chkList = (CheckBoxList)Page.FindControl(ControlName);
Shouldn't the ASP.NET 2.0 team find a solution in something like:
CheckBoxList _ckhList = this.FindControl("CheckDays") // this refers to ContentPalceHolder
Thank you,
A great article on ASP.NET 2.0 Master Pages:
Inside ASP.NET 2.0 Master Pages
Regards