How to update records by Checkbox in ASP.NET

Greeting,

I use Repeater to display data on asp.net form. I added a checkbox field(not from the DB) in the repeater and a 'update' button outside of the repeater. Here is what I want, the user can click on the checkboxes next to each record, then click on 'update' button to run a update sql stmt for those check marked records.

the update sql stmt will always be the same like the following:
update tbl1
set field1='completed'
where ID= MyID

MyID is a field on the repeater which is the primary field(unique).

Thanks in advance.
mrongAsked:
Who is Participating?
 
SwapnilSoftware ArchitectCommented:
Hi mrong,
you can write the code as following

<asp:reapeater...>
<ItemTemplate>
<asp:checkbox id="chkId" ToolTip = '<%# DataBinder.Eval(container.DataItem, "MyId") %' >
</ItemTemplate>
</asp:reapeater...>

and in your update button click

looping through repeater
find check box control through repeater.findcontrol("chkId")
check that wheather checkbox is check or not and base on that
take the value of tooltip property as Id parameter for your update procedure

Regards,
NetSwap.
0
 
DreamMasterCommented:
You will have to check whether or not the checkbox was set. Did you use HTML checkboxes or the ASP:Checkbox versions?

I've done something similar where I was able to check the rights users had to certain pages in my application.

I used asp:Checkbox and then checked like this:

For i = 0 To PageRights.Items.Count - 1
   Dim attControl As CheckBox
   attControl = PageRights.Items(i).Cells(0).FindControl("viewable")
   If attControl.Checked = True Then
      viewable = 1
   Else
      viewable = 0
   End If
   attControl = PageRights.Items(i).Cells(1).FindControl("editable")
   If attControl.Checked = True Then
      editable = 1
   Else
      editable = 0
   End If
   _strSQLQuery = "INSERT INTO group_rights (pageID, groupID, [view], edit) VALUES (" & PageRights.DataKeys(i) & ", " & Request.QueryString("id") & ", " & viewable & ", " & editable & ")"
   _objDatabase.InsertQuery(_strSQLQuery, "group_rights")
Next

In my case I used a datagrind named PageRights..however the rest will be quite similar. You should set the datakey to the MyID field to get this to work.

Hope it helps,

Regards,
Max.
0
 
mrongAuthor Commented:
netswap,DreamMaster:
Thank you for your post. Below is what I have in my code-behind. Repeater1 is the name of my repeater and closeCase is the name of the checkbox.  "&Repeater1.Items(i).proposal(i) &" line gave me error. Could you guys kindly take a look at my code and let me know what is worng?

Protected Sub UpdateButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles UpdateButton.Click
        Dim i
        For i = 0 To Repeater1.Items.Count - 1
            Dim attControl As CheckBox
            Dim closeCase
            attControl = Repeater1.Items(i).FindControl("closeCase")
            If attControl.Checked = True Then
                closeCase = 1
            Else
                closeCase = 0
            End If

            Dim sqlText6 = "Update ae_p_phs_e set status_code='65-COMP' where proposal ='" & Repeater1.Items(i).proposal(i) & "' "
            Dim MyOracleConn6 As New System.Data.OracleClient.OracleConnection("Data Source=MyDB;Persist Security Info=True;User ID=ROOT;Password=MyPWD;Unicode=True")

            Dim MyOracleCMD6 As New System.Data.OracleClient.OracleCommand(sqlText6, MyOracleConn6)
            MyOracleConn6.Open()
            MyOracleCMD6.ExecuteNonQuery()
            MyOracleConn6.Close()
            Response.Redirect("Report.aspx?shop=" & Session("shop"))


        Next
    End Sub
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
DreamMasterCommented:
Can you try changing " & Repeater1.Items(i).proposal(i) & " to " & Repeater1.Items(i).DataKeys(i) & " instead?

Hope it helps,

Regards,
Max.
0
 
mrongAuthor Commented:
DreamMaster,
Do I need  to set datakey to MyID somewhere?
Thanks.
0
 
mrongAuthor Commented:
I use the following sql stmt, it works. Now I need the sub. number '00161' with field name proposal in the repeater. Thanks.

Dim sqlText6 = "Update ae_p_pro_e set status_code='65-C' where proposal ='00161' "
0
 
DreamMasterCommented:
You should be able to set that in your repeater. DataKeyField="MyID"

Hope it helps,

Regards,
Max.
0
 
mrongAuthor Commented:
Max,
Where and how can I set the DataKeyField in repeater?
Thank you for your patience.
0
 
SwapnilSoftware ArchitectCommented:
Hi mrong,

Dreammaster,
       There is no such property in repeater, its property of datagrid.

mrong,
       you can try my way by binding any property of checkbox with myid column and later on take id from checkbox's property and update your row, code guideline is given in my first comment.

Regards,
Netswap.
0
 
mrongAuthor Commented:
netswap,
in the form I have the code below as you recommended.
<td> <asp:CheckBox id="closeCase" Visible='<%# Trim(DataBinder.Eval(Container.DataItem,"status_code1"))= "65-C" OR  Trim(DataBinder.Eval(Container.DataItem,"status_code1")) = "70-F" OR Trim(DataBinder.Eval(Container.DataItem,"status_code1"))= "75-C"%>' runat="server" ToolTip = '<%# DataBinder.Eval(container.DataItem, "proposal") %>' ></asp:CheckBox></td>

in the code-behind I have sql below.How to take the value of tooltip property as parameter for my update procedure in the sql?

                Dim sqlText6 = "Update ae_p_pro_e set status_code='65-C' where proposal ='<proposal>?' "
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.