updating database with checkboxes
i have a datagrid and every row has a checkbox on it. also every field in every row can be updated
the user can update multiple rows and check any checkboxes.
at the click of the SUBMIT bu开发者_运维技巧tton all the data should be updated. i need the checkboxes to update a boolean (or what ever the bool type is for sql server) in a database.
- how do implement the checkboxes to be able to update the table in the DB?
- with the click of the submit button how do i get all the data to be updated?
If you are using a GridView, building on the example from a previous example you got, you can do this. ** this is semi-pseudo code, beware**
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="false"
autogenerateeditbutton="true"
allowpaging="true"
datakeynames="CustomerID"
runat="server">
<columns>
<asp:templatefield>
<itemtemplate> <%-- This is itemtemplate so they are visible by default --%>
<asp:CheckBox ID="cbVerify" runat="server"></asp:CheckBox>
<asp:HiddenField ID="hidID" runat="server" Value='<%# Bind("CustomerID") %>'></asp:HiddenField>
</itemtemplate>
</asp:templatefield>
<asp:boundfield datafield="CustomerID" readonly="true" headertext="Customer ID"/>
<asp:boundfield datafield="CompanyName" readonly="true" headertext="Customer Name"/>
<asp:boundfield datafield="Address" headertext="Address"/>
<asp:boundfield datafield="City" headertext="City"/>
<asp:boundfield datafield="PostalCode" headertext="ZIP Code"/>
</columns>
</asp:gridview>
<asp:Button ID="btSubmit" runat="server" OnClick="btSubmit_Click"></asp:Button>
Code behind to process this
public void btSubmit_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in CustomersGridView.Rows)
{
CheckBox cbVerify = (CheckBox)row.FindControl("cbVerify");
HiddenField hidID = (HiddenField)row.FindControl("hidID");
// Do your validation of the data here
..
if (cbVerify != null)
{
// Add fields and update
sqlRecord.UpdateParameters["ID"].DefaultValue = hidID.Value;
sqlRecord.UpdateParameters["Valid"].DefaultValue = cbVerify.Checked.ToString();
sqlRecord.Update();
}
}
This should get you in a specific direction to look.
Go through the datagrid and store all of the data that you want to update in arrays or the like. Sql server has a bit
datatype and you can set it to 0
if the checkbox is off or 1
if it is checked
. Once you collect all the data pass it to your data layer for a sql update
You can use the DataGrid
object to iterate through its cells/controls. For example, using a nested loop you can do:
myDG.Items[index1].Cells[index2].Controls[0]
Edit: It depends on the input controls you have in the columns because you have to cast them. Say you have a datagrid with 10 columns and TextBoxes in all columns except the last, which is CheckBox, you would do:
CheckBox cb = null;
TextBox tb = null;
List<string> myList = new List<string>();
for(int row = 0; row < myDG.Items.Count; row++)
{
for(int col = 0; col < myDG.Columns.Count; col++)
{
if(col < 9){
tb = myDG.Items[row].Cells[col].Controls[0] as TextBox;
myList.Add(tb.Text);
}
else{
cb = myDG.Items[row].Cells[col].Controls[0] as CheckBox;
myList.Add(((cb.Checked) ? "1" : "0"));
}
}
}
You could use a 2D array/list to store if you wanted too. HTH
精彩评论