如何使 CheckBoxList 与 LinkBut​​ton 事件一起使用?

How to make CheckBoxList works with LinkButton event?

以下代码显示了一些复选框,如果您选择其中任何一个,它将在页面上列出。 问题是我想在页面上使用 "LinkButton",如果我单击 link,则会显示 ckeckbox。当我在 "asp:LinkButton" 中使用 OnLoad="Page_Edit" 时,checkboxlist 工作正常,但我不希望 checkboxlist 一直显示,我希望通过单击"Click to work with checkbox!"link。如果我使用 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);

  }

取消复选框和链接按钮上的 OnLoad; OnLoad 每次都运行,不符合您仅在单击链接按钮时显示的条件。现在,如果项目每次都在列表中,我建议只将控件添加到标记中:

<asp:CheckboxList .. Visible="false">
  <Items>
    <asp:ListItem Text="Item 1" />
  </Items>
</asp:CheckBoxList>

注意可见的属性;在 linkbutton onclick 中,然后设置 checkboxlistID.Visible = true,它会显示给用户。

更好的想法是在具有 IsPostBack 检查的 PageLoad 方法上创建一个新的 CheckBoxList 控件。

protected void Page_Load(object sender, EventArgs e)
{                        
if(!IsPostBack)
    //create checkbox list
}

之后,您应该仅将 OnClick="Page_Edit" 添加到 link 按钮,并在 Page_Edit 方法内尝试仅更改 Visible 属性,例如:

void Page_Edit(Object sender, EventArgs e)
{
   if(yourCondition)
      yourCheckBoxList.Visible = true;
   else
      yourCheckBoxList.Visible = false;
}

解决方案是在 "asp:linkButton" 上同时启用 OnLoad 和 OnClick,并在 OnClick 事件中使清单可见。通过这种方式,我们使复选框加载到页面但不可见,除非有人单击 link 使其可见。谢谢大家