Solved

How to update records by Checkbox in ASP.NET

Posted on 2007-03-20
10
268 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:mrong
  • 5
  • 3
  • 2
10 Comments
 
LVL 16

Accepted Solution

by:
Swapnil Piparia earned 70 total points
Comment Utility
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
 
LVL 19

Assisted Solution

by:DreamMaster
DreamMaster earned 55 total points
Comment Utility
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
 

Author Comment

by:mrong
Comment Utility
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
 
LVL 19

Expert Comment

by:DreamMaster
Comment Utility
Can you try changing " & Repeater1.Items(i).proposal(i) & " to " & Repeater1.Items(i).DataKeys(i) & " instead?

Hope it helps,

Regards,
Max.
0
 

Author Comment

by:mrong
Comment Utility
DreamMaster,
Do I need  to set datakey to MyID somewhere?
Thanks.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:mrong
Comment Utility
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
 
LVL 19

Expert Comment

by:DreamMaster
Comment Utility
You should be able to set that in your repeater. DataKeyField="MyID"

Hope it helps,

Regards,
Max.
0
 

Author Comment

by:mrong
Comment Utility
Max,
Where and how can I set the DataKeyField in repeater?
Thank you for your patience.
0
 
LVL 16

Expert Comment

by:Swapnil Piparia
Comment Utility
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
 

Author Comment

by:mrong
Comment Utility
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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now