Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Datagrid selected items with checkbox

Reply
Thread Tools

Datagrid selected items with checkbox

 
 
Bnob
Guest
Posts: n/a
 
      08-11-2004
I read some articles about the way to find the rows having his checkbox
checked.

Here is the code

Private Sub btnModifier_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnModifier.Click
Dim dgItem As DataGridItem
Dim chkSelected As CheckBox

For Each dgItem In Me.DataGrid1.Items
chkSelected = dgItem.FindControl("chkSelect")
If chkSelected.Checked Then
'--- my code
End If

Next

end sub

In my asp page I have this for my datagrid:
<Columns>
<asp:TemplateColumn>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:CheckBox id="chkSelect" Runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>

My problem is when I click on my btnModifier button, my code never
execute. The if statment always return false.

Any idea?

--
Ceci est une signature automatique de MesNews.
Site : http://mesnews.no-ip.com

 
Reply With Quote
 
 
 
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      08-11-2004
Hi,

Here's a sample that seems to do what you want. Perhaps you could try it and
let us know?

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Private Sub btnModifier_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnModifier.Click
Dim dgItem As DataGridItem
Dim chkSelected As CheckBox
Label2.Text = ""
For Each dgItem In Me.DataGrid1.Items
chkSelected = dgItem.FindControl("chkSelect")
If chkSelected.Checked Then
Label2.Text = Label2.Text & _
chkSelected.UniqueID & "<br>"
End If
Next
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add _
(New DataColumn("Boolean", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource



<aspataGrid id="DataGrid1" runat="server">
<Columns>
<asp:TemplateColumn HeaderText="Boolean Value">
<ItemTemplate>
<asp:CheckBox id=chkSelect runat="server" Checked='<%#
DataBinder.Eval(Container, "DataItem.Boolean") %>' >
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Boolean Value">
<ItemTemplate>
<asp:Label id="Label1" runat="server">
<%# IIF(DataBinder.Eval(Container, "DataItem.Boolean"),"Yes","No")
%>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</aspataGrid>
<P>
<asp:Label id="Label2" runat="server"></asp:Label></P>
<P>
<asp:Button id="btnModifier" runat="server"
Text="Button"></asp:Button></P>

Does this help?

Ken
Microsoft MVP [ASP.NET]
Toronto



"Bnob" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I read some articles about the way to find the rows having his checkbox
>checked.
>
> Here is the code
>
> Private Sub btnModifier_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles btnModifier.Click
> Dim dgItem As DataGridItem
> Dim chkSelected As CheckBox
>
> For Each dgItem In Me.DataGrid1.Items
> chkSelected = dgItem.FindControl("chkSelect")
> If chkSelected.Checked Then
> '--- my code
> End If
>
> Next
>
> end sub
>
> In my asp page I have this for my datagrid:
> <Columns>
> <asp:TemplateColumn>
> <ItemStyle HorizontalAlign="Center"></ItemStyle>
> <ItemTemplate>
> <asp:CheckBox id="chkSelect" Runat="server"></asp:CheckBox>
> </ItemTemplate>
> </asp:TemplateColumn>
> </Columns>
>
> My problem is when I click on my btnModifier button, my code never
> execute. The if statment always return false.
>
> Any idea?
>
> --
> Ceci est une signature automatique de MesNews.
> Site : http://mesnews.no-ip.com
>


 
Reply With Quote
 
 
 
 
Bnob
Guest
Posts: n/a
 
      08-11-2004
Ken Cox [Microsoft MVP] avait écrit le 11.08.2004 :
> chkSelected.Checked


Sorry
But the chkSelected.Checked statment is always FALSE!

--
Ceci est une signature automatique de MesNews.
Site : http://mesnews.no-ip.com

 
Reply With Quote
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      08-11-2004
Sorry, I guess I can't help you then because the code I provided works on my
system.


"Bnob" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Ken Cox [Microsoft MVP] avait écrit le 11.08.2004 :
>> chkSelected.Checked

>
> Sorry
> But the chkSelected.Checked statment is always FALSE!
>
> --
> Ceci est une signature automatique de MesNews.
> Site : http://mesnews.no-ip.com
>


 
Reply With Quote
 
Bnob
Guest
Posts: n/a
 
      08-12-2004
Ken Cox [Microsoft MVP] vient de nous annoncer :
> Sorry, I guess I can't help you then because the code I provided works on my
> system.
>
>
> "Bnob" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Ken Cox [Microsoft MVP] avait écrit le 11.08.2004 :
>>> chkSelected.Checked

>>
>> Sorry
>> But the chkSelected.Checked statment is always FALSE!
>>
>> -- Ceci est une signature automatique de MesNews.
>> Site : http://mesnews.no-ip.com
>>


All is ok

Thanks

--
Ceci est une signature automatique de MesNews.
Site : http://mesnews.no-ip.com

 
Reply With Quote
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      08-13-2004
What was it?


>
> All is ok
>
> Thanks
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
need help to fill textboxes from a selected item in datagrid-selected index changed. mldardy ASP .Net 0 09-28-2010 02:59 PM
DropDownList 2 always returns Selected = 0 for all items - even selected item Iain ASP .Net 3 12-11-2006 11:07 AM
Looping through CheckBoxList items doesn't pick up selected ones when items were disabled Bart Van Hemelen ASP .Net 2 09-04-2006 08:35 AM
Copy checkbox selected datagrid items into new dataset =?Utf-8?B?ZWNvZGVy?= ASP .Net 2 10-14-2005 04:27 AM
Getting selected items in dynamically loaded checkbox list Joel Reinford ASP .Net 1 01-13-2004 10:18 PM



Advertisments