CheckBoxList and postback

G

Guest

I'm finally making the asp to asp.net transition and "newbie" questions.

I have a webform which contains a CheckBoxList element. I use a
sqldatareader as the source for it's data source, everything works fine.
When the form is submitted and the page is reloaded, all the textbox elements
contain all the postback data but the CheckBoxList does not retain the
selected items. Is this because the CheckBoxList is dynamically created? In
classic ASP I'd capture the selected items during the page reload and
programatically set the selected item values after the CheckBoxList is
regenerated. Is there some .netty way of acomplishing this or is the process
still the same.

thanks, Craig
 
M

Martin Dechev

Hi, Craig,

Could you please post a short but complete example of the problematic
behavior. It is impossible to say where is the problem without seeing your
code - how you bind the CheckBoxList, then at which point you are trying to
read the checked/unchecked values, etc.

Greetings
Martin
 
G

Guest

aspx file includes:
<form id="Form1" method="post" runat="server">
<asp:CheckBoxList id="cblApplications"
runat="server"></asp:CheckBoxList>
<asp:Button id="Button1" runat="server" Text="Submit"></asp:Button>
</form>

This is some code within Page_Load()
dim selItems as string
If Page.IsPostBack Then
For Each li As ListItem In cblApplications.Items
If li.Selected Then
' I can get the selected items here
selItems = selItems + " " + li.Text.ToString()
End If
Next
End If

Dim myConn As New SqlConnection(connectionStr)
Dim myCmd As New SqlCommand("select * from AuthApplications")

myConn.Open()
myCmd.CommandType = CommandType.Text
myCmd.Connection = myConn
Dim reader As SqlDataReader = myCmd.ExecuteReader

cblApplications.DataSource = reader
cblApplications.DataTextField = "AppName"
cblApplications.DataValueField = "AppID"
cblApplications.DataBind()

reader.Close()
myConn.Close()

End If

I'm able to capture the selected items in the "IsPostBack" section, I'm
wondering if I need to mark the items "selected" myselft after the
CheckBoxList regenerates. Hope this is enough code to help.

Craig
 
M

Martin Dechev

Hi,

You need to set the DataSource of the CheckBoxList and call DataBind only
once - the common approach is in the Page_Load handler when Page.IsPostBack
is false. Then the DataSource will be persisted (and it will be binded for
you) along with the selected indices. Example:

<form runat="server">
<asp:checkboxlist id="cbl" runat="server"/>
<br/><br/>
<asp:button id="b1" runat="server" text="Test" onclick="b1_Click"/>
<br/><br/>
<asp:literal id="l1" runat="server"/>
</form>

Protected Sub Page_Load(ByVal s As Object, ByVal e As EventArgs)
If Not (IsPostBack) Then
Dim array1() As String = _
New String() {"One", "Two", "Three", "Four", "Five"}
cbl.DataSource = array1
cbl.DataBind()
End If
End Sub

Protected Sub b1_Click(ByVal s As Object, ByVal e As EventArgs)
l1.Text = "Selected Items:"
For Each li As ListItem In cbl.Items
If li.Selected Then l1.Text += "<br/>" + li.Text
Next
End Sub

Hope this helps
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top