darkcloudbird
darkcloudbird

Reputation: 23

asp.net VB gridview checkbox always show false on serverside code behind

I am trying to implement a gridview with checkbox column on asp.net (VB). when the user checks checkbox and click delete button, it should access database and delete all checked item. I have already tried numerous googled solution and never worked for me. here is short scenario on my aspx page: 1)search by id text box, when user enter ID and click search button, the below table is displayed

<asp:GridView ID="MyGridView" runat="server">
<Columns>
<asp:TemplateField headertext="Name">
<ItemTemplate>
<asp:Label id="namelbl" 
text='<%# Eval("name")%>' runat="server"/>
</ItemTemplate>   
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete Now?">
<ItemTemplate>
 <asp:CheckBox Enabled="true"   ID="chkStatus" runat="server" />

</ItemTemplate>      
</asp:TemplateField>
 </Columns>
 </asp:GridView>

after data is displayed, user checks the desired checkbox and click delete button, then the code behind is run

Protected Sub DelSelected(ByVal sender As Object, ByVal e As System.EventArgs)
Dim idList as ArrayList = new ArrayList()
For Each row As GridViewRow In MyGridView.Rows
    Dim selectcb As CheckBox = CType(row.FindControl("chkStatus"), CheckBox)
     If (selectcb.Checked) Then
     'put into delete list

Trouble starts here, selectcb checkbox is always false How could that happen, any idea would be appreciated Thanks

Upvotes: 0

Views: 3890

Answers (1)

Aghilas Yakoub
Aghilas Yakoub

Reputation: 29000

you must bind your data only when no IsPostback in order to persist your checkbox selected, and no reset

If(! IsPostBack)
{
  Bind();
}

Upvotes: 1

Related Questions