How to implement full selection of datagrid checkbox in asp.net

Source: Internet
Author: User

Copy codeThe Code is as follows: <form runat = "server">
<Asp: DataGrid AutoGenerateColumns = "false" OnItemCreated = "itemcreate" DataKeyField = "link_id" ID = "mydg" runat = "server">
<Columns>
<Asp: TemplateColumn>
<Headertemplate>
<Asp: CheckBox ID = "checkall" OnCheckedChanged = "chkall_onchanged" AutoPostBack = "true" runat = "server"> </asp: CheckBox>
</Headertemplate>
<Itemtemplate>
<Asp: CheckBox ID = "checkdel" runat = "server"> </asp: CheckBox>
</Itemtemplate>
</Asp: TemplateColumn>
<Asp: BoundColumn DataField = "link_id" HeaderText = "Link_Id"> </asp: BoundColumn>
<Asp: BoundColumn HeaderText = "website name" DataField = "sitename"/>
<Asp: BoundColumn HeaderText = "website address" DataField = "url"/>
<Asp: BoundColumn HeaderText = "website Introduction" DataField = "intro"/>
<Asp: BoundColumn HeaderText = "website score" DataField = "grade"/>
<Asp: BoundColumn HeaderText = "submission time" DataField = "submit_date"> </asp: BoundColumn>
</Columns>
</Asp: DataGrid>
<Asp: Label ID = "lb1" runat = "server"> </asp: Label>

<Asp: Button ID = "btn1" Text = "delete" runat = "server" OnClick = "btn1_onclick"> </asp: Button>
<Asp: Button ID = "OK" OnClick = "OK _click" Text = "Save and modify" runat = "server"> </asp: Button>
<Asp: Button ID = "cancel" OnClick = "cancel_click" Text = "unmodify" runat = "server"> </asp: Button>
</Form>


<% @ Import namespace = "system. data" %>
<% @ Import namespace = "system. data. oledb" %>
<Script language = "vb" runat = "server">
Dim conn as oledbconnection
Dim ds as dataset
Sub Page_load (sender as object, e as eventargs)
If not ispostback then
Calldb ()
Else
Ds = session ("ds ")
End if

End sub
Sub calldb ()
Conn = new oledbconnection ("provider = Microsoft. jet. oledb.4.0; data source =" & server. MapPath ("wwwlink. mdb "))
Dim sqlstr = "select * from link"
Conn. open ()
Dim adp as new oledbdataAdapter (sqlstr, conn)
Ds = new dataset ()
Adp. fill (ds, "link ")
Mydg. DataSource = ds. tables ("link"). defaultview
Mydg. databind ()
Conn. close ()
Session ("ds") = ds
End sub
Sub btn1_onclick (sender as object, e as eventargs)
Dim chk as checkbox
Dim id as string
Dim datag as datagriditem
Conn = new oledbconnection ("provider = Microsoft. jet. oledb.4.0; data source =" & server. MapPath ("wwwlink. mdb "))
Dim sqlstr as string
Dim cmd as oledbcommand

Dim I as integer
'For each datag in mydg. items

For I = 0 to mydg. items. count-1
'Chk = ctype (datag. findcontrol ("checkdel"), checkbox)
'Chk = datag. findcontrol ("checkdel ")
Chk = mydg. items (I). cells (0). findcontrol ("checkdel ")
If chk. checked then
Id = cstr (mydg. items (I). cells (1). text)
'Id = cstr (mydg. datakeys (datag. itemindex ))
Conn. open ()
Sqlstr = "delete from link where link_id =" & id
Cmd = new oledbcommand (sqlstr, conn)
Cmd.exe cutenonquery
'Ds. tables ("link"). rows (I). delete ()
Conn. close ()
End if

Next
'Session ("ds") = ds
'Mydg. edititemindex =-1
'Mydg. datasource = ds. tables ("link"). defaultview
'Mydg. databind ()
Calldb ()
End sub
Sub cancel_click (sender as object, e as eventargs)
Calldb ()
End sub

Sub OK _click (sender as object, e as eventargs)
Conn = new oledbconnection ("provider = Microsoft. jet. oledb.4.0; data source =" & server. MapPath ("wwwlink. mdb "))
Dim adp as new oledbdataadapter ("select * from link", conn)
Dim ocb as new oledbcommandbuilder (adp)
Adp. updatecommand = ocb. getupdatecommand ()
Adp. deletecommand = ocb. getdeletecommand ()
Adp. update (ds, "link ")
Calldb ()
End sub
Dim checka as checkbox
Sub itemcreate (sender as object, e as datagriditemeventargs)
If (e. item. itemtype = Listitemtype. Header) then
Checka = e. item. findcontrol ("checkall ")
End if
End sub

Sub chkall_onchanged (sender as object, e as eventargs)

Dim I as integer
If checka. checked = true then
For I = 0 to mydg. items. count-1

Dim chk as checkbox = mydg. items (I). cells (0). findcontrol ("checkdel ")
Chk. checked = true
Next
Else
For I = 0 to mydg. items. count-1
Dim chk as checkbox = mydg. items (I). cells (0). findcontrol ("checkdel ")
Chk. checked = false
Next

End if
End sub
</Script>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.