积极答复者
为什么在VS设计器里,我设置我的用户控件的属性后,不能看到改变?

问题
-
已知.net framework提供的asp.net控件,我一设置它的一项属性,在VS设计器里就能看到效果。例如设置Button.Text="hello world",在VS设计器里就能看到写有hello world的按钮。现在我做了一个用户控件,它是一个搜索框,外加一个用于选择搜索范围的单选框组。
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SearchBox.ascx.cs" Inherits="Components_SearchBox" %> <div> <asp:RadioButtonList ID="searchScopeRadioButtons" runat="server" RepeatDirection="Horizontal"> <asp:ListItem>Shop</asp:ListItem> <asp:ListItem>Product</asp:ListItem> </asp:RadioButtonList> </div> <div> <asp:TextBox ID="keywordTextBox" runat="server" /> <asp:Button ID="searchButton" runat="server" Text="Search" onclick="searchButton_Click" /> </div>
后台代码如下public partial class Components_SearchBox : System.Web.UI.UserControl { public SearchScope SearchScope { get; set; } protected void Page_Load(object sender, EventArgs e) { if (SearchScope == SearchScope.Shop) searchScopeRadioButtons.Items[0].Selected = true; else searchScopeRadioButtons.Items[1].Selected = true; } }
现在我要使用这个用户控件,设定<uc1:SearchBox ID="SearchBox1" runat="server" SearchScope="Product" />在VS设计器里,Product单选框并没有呈现为选中的状态。(当然了,运行了以后,在网页中是选中的)请问我应该做什么样的修改,使得在VS设计器里,Product单选框能呈现为选中的状态??
答案
-
[Designer(typeof(Class1))]
public class ClassControl:System.Web.UI.Control
public class Class1: ControlDesigner
{
}
自定义控件需要实现ControlDesigner,才能正常的显示出想要的效果,用户控件的话,没有尝试过,你可以试试
- 已建议为答案 gsralexModerator 2012年3月10日 2:29
- 已标记为答案 BU XI - MSFTModerator 2012年3月20日 3:19
-
我想告诉你的是控件下面有个属性叫做DesignMode,用于表示现在是设计时的设计器模式,还是运行时模式,有些控件生命周期的事件是不会被执行的
所以要想拓展设计时的设计器模式,需要重写ControlDesigner,用户控件也也是可以使用
具体如下http://msdn.microsoft.com/zh-cn/library/system.windows.forms.design.controldesigner%28v=vs.110%29.aspx
- 已标记为答案 BU XI - MSFTModerator 2012年3月20日 3:19
全部回复
-
請問你的SearchScope是做什麼用的?我用下列的程式碼來模擬你的狀況,若有會錯你的意思,再請你指教。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class SearchBox : System.Web.UI.UserControl { public string searchScope { get; set; } protected void Page_Load(object sender, EventArgs e) { if (searchScope == searchScope) searchScopeRadioButtons.Items[0].Selected = true; else searchScopeRadioButtons.Items[1].Selected = true; } protected void searchButton_Click(object sender, EventArgs e) { } }
以上說明若有錯誤請指教,謝謝。
http://www.dotblogs.com.tw/terrychuang/ -
請問是想要做這樣的效果嗎?
程式碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class SearchBox : System.Web.UI.UserControl { public SearchScope searchScope { get; set; } protected void Page_Load(object sender, EventArgs e) { if (searchScope == SearchScope.Product) searchScopeRadioButtons.Items[0].Selected = true; else searchScopeRadioButtons.Items[1].Selected = true; } protected void searchButton_Click(object sender, EventArgs e) { } public enum SearchScope { Shop, Product } }
以上說明若有錯誤請指教,謝謝。
http://www.dotblogs.com.tw/terrychuang/ -
[Designer(typeof(Class1))]
public class ClassControl:System.Web.UI.Control
public class Class1: ControlDesigner
{
}
自定义控件需要实现ControlDesigner,才能正常的显示出想要的效果,用户控件的话,没有尝试过,你可以试试
- 已建议为答案 gsralexModerator 2012年3月10日 2:29
- 已标记为答案 BU XI - MSFTModerator 2012年3月20日 3:19
-
我想告诉你的是控件下面有个属性叫做DesignMode,用于表示现在是设计时的设计器模式,还是运行时模式,有些控件生命周期的事件是不会被执行的
所以要想拓展设计时的设计器模式,需要重写ControlDesigner,用户控件也也是可以使用
具体如下http://msdn.microsoft.com/zh-cn/library/system.windows.forms.design.controldesigner%28v=vs.110%29.aspx
- 已标记为答案 BU XI - MSFTModerator 2012年3月20日 3:19