以下代码显示了一些复选框,如果您选择其中任何一个,它将在页面上列出。问题是我想"LinkButton"
在页面上使用,如果我点击链接,则会显示 ckeckbox。当我使用OnLoad="Page_Edit"
in"asp:LinkButton"
时,checkboxlist
工作正常,但我不checkboxlist
希望它一直显示,我希望通过单击“单击以使用复选框!”来显示它。关联。如果我使用OnClick="Page_Edit"
,通过选择任何复选框,checkboxlist
消失。任何帮助,将不胜感激。
<h3> CheckBoxList Constructor Example </h3>
<asp:LinkButton id="myid" runat="server" Text="Click to work with checkbox!" OnLoad="Page_Edit" OnClick="Page_Edit" /><br />
Select items from the CheckBoxList.
<br /><br />
<asp:PlaceHolder id="Place" runat="server"/>
<br /><br />
<asp:label id="Message" runat="server"/>
void Check_Clicked(对象发送者,EventArgs e){
// Retrieve the CheckBoxList control from the Controls collection
// of the PlaceHolder control.
CheckBoxList checklist = (CheckBoxList)Place.FindControl("checkboxlist1");
// Make sure a control was found.
if(checklist != null)
{
Message.Text = "Selected Item(s):<br /><br />";
// Iterate through the Items collection of the CheckBoxList
// control and display the selected items.
for (int i=0; i<checklist.Items.Count; i++)
{
if (checklist.Items[i].Selected)
{
Message.Text += checklist.Items[i].Text + "<br />";
}
}
}
else
{
// Display an error message.
Message.Text = "Unable to find CheckBoxList control.";
}
}
void Page_Edit(Object sender, EventArgs e)
{
// Create a new CheckBoxList control.
CheckBoxList checklist = new CheckBoxList();
// Set the properties of the control.
checklist.ID = "checkboxlist1";
checklist.AutoPostBack = true;
checklist.CellPadding = 5;
checklist.CellSpacing = 5;
checklist.RepeatColumns = 2;
checklist.RepeatDirection = RepeatDirection.Vertical;
checklist.RepeatLayout = RepeatLayout.Flow;
checklist.TextAlign = TextAlign.Right;
// Populate the CheckBoxList control.
checklist.Items.Add(new ListItem("Item 1"));
checklist.Items.Add(new ListItem("Item 2"));
checklist.Items.Add(new ListItem("Item 3"));
checklist.Items.Add(new ListItem("Item 4"));
checklist.Items.Add(new ListItem("Item 5"));
checklist.Items.Add(new ListItem("Item 6"));
// Manually register the event-handling method for the
// SelectedIndexChanged event.
checklist.SelectedIndexChanged += new EventHandler(this.Check_Clicked);
// Add the control to the Controls collection of the
// PlaceHolder control.
Place.Controls.Add(checklist);
}