Reputation: 11
IDE: Visual Studio 2019 Framework: .net Framework 4.7.2
Hello everyone, for the work project, I made a CompositeControl, which will contain a TextBox, and a WebControl.
My code as follows, and will explain my problem at the end.
1.MemberDialog.cs
This is a control used to select people or groups.
In design mode, a DesignMode.gif image will be displayed for developers.
On the web page, ViewUser.gif or ViewGroup.gif will be displayed according to PickStyle.
public class MemberDialog : WebControl
{
public enum Mtype
{
Person,
Group
}
private Mtype otypes;
public Mtype PickerStyle
{
get
{
return this.otypes;
}
set
{
this.otypes = value;
}
}
protected override void RenderContents(HtmlTextWriter output)
{
var strHtml = string.Empty;
if (base.Site == null || !base.Site.DesignMode)
{
if (PickerStyle == Mtype.Person)
{
strHtml = "<img src='ViewUser.gif'> ←Image For Picker User.";
}
else
{
strHtml = "<img src='ViewGroup.gif'> ←Image For Picker Group.";
}
}
else
{
strHtml = "<img src='DesignMode.gif'> ←Image For DesignMode.";
}
output.Write(strHtml);
}
}
2.MyWebCustomControl.cs
This CompositeControl contains a DropDownList and a MemberDialog.
The DropDownList control is used to determine the PickStyle of the MemberDialog.
public class MyWebCustomControl : CompositeControl
{
private DropDownList _ddlPickStyle = new DropDownList();
private MemberDialog _memberDialog = new MemberDialog();
public MyWebCustomControl()
{
_ddlPickStyle.Items.Add("Person");
_ddlPickStyle.Items.Add("Group");
_memberDialog.PickerStyle = MemberDialog.Mtype.Person;
this.PreRender += MyWebCustomControl_PreRender;
}
private void MyWebCustomControl_PreRender(object sender, EventArgs e)
{
if (_ddlPickStyle.SelectedValue == "Group")
{
_memberDialog.PickerStyle = MemberDialog.Mtype.Group;
}
}
protected override void CreateChildControls()
{
Controls.Clear();
Controls.Add(_ddlPickStyle);
Controls.Add(_memberDialog);
}
}
3.WebForm.aspx
Drag MyWebCustomControl into the editing area.
<body>
<form id="form1" runat="server">
<div>
<cc1:MyWebCustomControl ID="MyWebCustomControl1" runat="server" />
</div>
</form>
</body>
In design mode, MemberDialog does not display the content of the design mode (DesignMode.gif ←Image For DesignMode.), Instead it shows PageMode (ViewUser.gif ←Image For Picker User.).
Through Debugger, function RenderContents of MemberDialog, base.Site return null, so the web page mode is displayed.
At present, my solution is to set the value of this.Site to MemberDialog when MyWebCustomControl Renders.
MyWebCustomControl.cs
public class MyWebCustomControl : CompositeControl
{
...
protected override void Render(HtmlTextWriter writer)
{
_memberDialog.Site = this.Site;
base.Render(writer);
}
}
This solution solves my problem temporarily,
but is it correct? Is there any other correct way to do it?
Upvotes: 1
Views: 28