0

The following code shows some checkboxes that if you choose any of them, it will be listed on the page. The problem is that I want to use the "LinkButton" on the page that if I click on the link then the ckeckbox will be shown. When I use OnLoad="Page_Edit" in "asp:LinkButton", the checkboxlist works fine but I do not want the checkboxlist shows up all the time and I want it to show up by clicking on the "Click to work with checkbox!" link. if I use OnClick="Page_Edit", by choosing any of the checkboxes, the checkboxlist disappears. Any help would be appreciated.

  <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(Object sender, 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);

  }
Hugh
  • 450
  • 3
  • 13
Shab
  • 5
  • 8
  • I have trouble understanding exactly what you want the code to do. Can you specify what the expected behavior is and how it differs from what you have now? – sara Aug 12 '15 at 16:04
  • May be using client side scripting would be a better and easier option as you don't need to make server calls to show and hide dom elements. – user1 Aug 12 '15 at 16:11
  • I have a "Click to work with checkbox" link on the page that if I click on it, several checkboxxes has to show up and if I choose any of them, the checkbox's name will appear on the page too. Right now my code can show this check boxes all the time and not just when I click on the link. The second problem is that when I can make it to work in a way that I want using "onclick=Page-Edit" on "linkbutton", then if I choose one of the checkboxes, it makes the checkboxlist disppear. – Shab Aug 12 '15 at 16:32

3 Answers3

1

Get rid of OnLoad on the checkbox and linkbutton; OnLoad runs everytime and does not meet your condition of showing only when linkbutton clicked. Now if the items are always in the list everytime, I'd recommend just add the control to the markup:

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

Notice the visible property; in linkbutton onclick, then you set checkboxlistID.Visible = true, and it will appear to the user.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
0

Better idea is to create a new CheckBoxList control on PageLoad method with IsPostBack checking.

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

After that you should add only OnClick="Page_Edit" to link button and inside Page_Edit method try to change only Visible properties for example:

void Page_Edit(Object sender, EventArgs e)
{
   if(yourCondition)
      yourCheckBoxList.Visible = true;
   else
      yourCheckBoxList.Visible = false;
}
ElConrado
  • 1,477
  • 4
  • 20
  • 46
0

The solution would be to have both OnLoad and OnClick on "asp:linkButton" and in OnClick event we make the checklist visible. This way we have made the checkbox to be loaded to the page but invisible unless someone clicks on the link that makes it visible. Thank you all.

Shab
  • 5
  • 8