p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 4 General Discussion (http://p2p.wrox.com/forumdisplay.php?f=561)
-   -   Checkbox (http://p2p.wrox.com/showthread.php?t=82424)

123mehran January 29th, 2011 12:19 PM

Checkbox
 
Hi
when i checked the item1 and then item2 ,the item1 repeat
like this:http://i53.tinypic.com/20ushz7.png
what should i do?
Code:

 
      Protected Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles CheckBoxList1.SelectedIndexChanged
      For i As Integer = 0 To CheckBoxList1.Items.Count - 1
      If CheckBoxList1.Items(i).Selected Then
      lbl1.Text &= CheckBoxList1.Items(i).Text + "<br/>"
      End If
      Next
      End Sub


Imar January 29th, 2011 12:52 PM

Hi there,

You can clear the Label's Text before you start the loop:

Code:


Protected Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, _
ByVal e As EventArgs) Handles CheckBoxList1.SelectedIndexChanged

  lbl1.Text = String.Empty
  For i As Integer = 0 To CheckBoxList1.Items.Count - 1
    If CheckBoxList1.Items(i).Selected Then
      lbl1.Text &= CheckBoxList1.Items(i).Text + "<br/>"
      ...
End Sub

Alternatively, you can set the Label control's EnableViewState to False in the Mark Up:

Code:


<asp:Label ID="lbl1" runat="server" EnableViewState="False" .... />

This way, the Label's Text is automatically reset after each post back.

Hope this helps,

Imar


All times are GMT -4. The time now is 01:27 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.