我已经创建了一个服务器控件并在页面上调用了两次。
下面是代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace DNWebComponent {
[DefaultProperty("Text")]
[ToolboxData("<{0}:DNdropdown runat=server></{0}:DNdropdown>")]
public class DNdropdown : WebControl, IDNComponent {
private string _label;
private TextBox _txtBox;
public String _ConnectedField;
private string _connectedField;
private string _MultiSeekField;
private List<String> _selectedValue;
private string _InputTable;
private CheckBoxList _Checkbox;
Panel div;
public String ConnectedField {
get { return _connectedField; }
set { _connectedField = value; }
}
public DNdropdown(string label)
: this() {
this._label = label;
}
public String Label {
get { return _label; }
set { _label = value; }
}
public DNdropdown() {
_selectedValue = new List<string>();
_txtBox = new TextBox();
_Checkbox = new CheckBoxList();
div = new Panel();
}
public String InputTable {
get { return _InputTable; }
set { _InputTable = value; }
}
public String MultiSeekField {
get { return _MultiSeekField; }
set { _MultiSeekField = value; }
}
public CheckBoxList AspChekcBoxes {
get { return _Checkbox; }
set { _Checkbox = value; }
}
public List<String> SelectedValue {
get {
foreach (ListItem item in _Checkbox.Items) {
if (item.Selected)
_selectedValue.Add(item.Value);
}
return _selectedValue;
}
set {
if (value != null) {
_selectedValue = value;
}
}
}
public string DivCss { get; set; }
protected List<String> GetPropertyValue<v>(string SelectedValue, List<String> nullValue) {
if (ViewState[SelectedValue] == null) {
return nullValue;
}
return (List<String>)ViewState[SelectedValue];
}
protected override void OnInit(EventArgs e) {
div = new Panel { CssClass = DivCss };
FillControl();
div.Controls.Add(new Literal { Text = Label + ": " });
Panel dvTxtBox = new Panel { CssClass = "dt" };
_txtBox.Text = "Select";
_txtBox.ReadOnly = true;
dvTxtBox.Controls.Add(_txtBox);
Panel dvChkBox = new Panel { CssClass = "divco" };
dvTxtBox.Style.Add("float", "right;");
dvTxtBox.Style.Add("margin-right", "700px;");
dvChkBox.Style.Add("margin-right", "700px;");
dvChkBox.Style.Add("float", "right;");
dvChkBox.Controls.Add(_Checkbox);
div.Controls.Add(dvTxtBox);
div.Controls.Add(dvChkBox);
Controls.Add(div);
}
protected override void RenderContents(HtmlTextWriter output) {
div.RenderControl(output);
}
public bool FillControl() {
_Checkbox.Items.Add("a");
_Checkbox.Items.Add("b");
return true;
}
}
}
<asp:PlaceHolder ID="Place1" runat="server">
<div id="DivDrop" runat="server">
</div>
<div id="divbtn">
<div><cc:DNdropdown ID="dropNew" runat="server" Label="DropDown" />
<cc:DNdropdown ID="newcontrol" runat="server" Label="New Dropdown"/></div>
<div ><asp:Button ID="btnSave" Text="Save" runat="server" OnClick="btnSave_Clicked" /></div> </div>
</asp:PlaceHolder>
我在上面的Sample.aspx页面中注册了这个控件,我在那里使用这个控件.dropNew控件工作正常,但每当我向Sample.aspx页面添加一个新控件时问题开始。我点击一个下拉列表第二个自动打开