将复选框绑定到数据库中的位域(AJAX)
本文关键字:位域 AJAX 复选框 绑定 数据库 | 更新日期: 2023-09-27 18:01:22
我有一个复选框,如果我的gridview中的条目已被"处理"(处理意味着它已被用户检查),我想在其中加载一个检查。我认为这样做的最好方法是为每一行都有一个字段,称为processed,并在该行中包含一个复选框。当用户检查它时,我希望它在数据库中存储一个1。此外,当它加载时,我希望复选框加载一个检查或不依赖于数据库中的字段。我真的很难弄清楚如何把这两件事结合在一起……我已经看了语法,但我不能完全分辨出是怎么回事。我也试过了,它似乎没有存储信息-正因为如此,我认为我的OnCheckedChanged事件不能正常工作或根本没有。有什么建议吗?
CS public partial class vieworders : System.Web.UI.Page
{
private string orderByString;
private string fieldString;
private string address;
private DataGrid dataGrid = new DataGrid();
protected void Page_Load(object sender, EventArgs e)
{
orderByString = orderByList.SelectedItem.Value;
fieldString = searchTextBox.Text;
string sqlStatement = "SELECT fName,lName,zip,email,cwaSource,price,length FROM SecureOrders WHERE fName LIKE '%" + fieldString + "%' OR lName LIKE'%" + fieldString + "%' OR zip LIKE'%" + fieldString + "%' OR zip LIKE'%" + fieldString + "%' OR email LIKE'%" + fieldString + "%' OR cwaSource LIKE'%" + fieldString + "%' OR length LIKE'%" + fieldString + "%' OR price LIKE'%" + fieldString + "%' ORDER BY " + orderByString;
////////////////////////////
connectionString = rootWebConfig.ConnectionStrings.ConnectionStrings["secureodb"];
//TEST
for (int i = 0; i < DefaultGrid.Rows.Count; i++)
{
CheckBox chkUpdate = (CheckBox)DefaultGrid.Rows[i].Cells[1].FindControl("CheckBoxProcess");
if (chkUpdate != null)
{
OrderBrowser.Text += "Test";
}
}
// Create an SqlConnection to the database.
using (SqlConnection connection = new SqlConnection(connectionString.ToString()))
{
connection.Open();
SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlStatement, connection);
// create an SqlCommandBuilder - this will automatically generate the
// commands, and set the appropriate properties in the dataAdapter
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
// create the DataSet
DataSet dataSet = new DataSet();
// fill the DataSet using our DataAdapter
dataAdapter.Fill(dataSet, "SecureOrders");
SqlCommand cmd = new SqlCommand("SELECT * FROM SecureOrders", connection); // might not need this
DataView source = new DataView(dataSet.Tables[0]);
DefaultGrid.DataSource = source;
DefaultGrid.DataBind();
}
}
protected void DefaultGrid_SelectedIndexChanged(Object sender, EventArgs e)
{
GridViewRow row = DefaultGrid.SelectedRow;
string name = "Name: " + row.Cells[2].Text + " " + row.Cells[3].Text + "'r'n";
// if (row.Cells[4].Text == " ")
//{
//address = "Address: " + row.Cells[3].Text + "'r'n " + row.Cells[5].Text + ", " + row.Cells[6].Text + " " + row.Cells[7].Text + " " + row.Cells[8].Text + "'r'n";
// }
//else
// {
// address = "Address: " + row.Cells[3].Text + "'r'n " + row.Cells[4].Text + "'r'n " + row.Cells[5].Text + ", " + row.Cells[6].Text + " " + row.Cells[7].Text + " " + row.Cells[8].Text + "'r'n";
//}
string zip = "Zip: " + row.Cells[4].Text + "'r'n";
string email = "Email: " + row.Cells[5].Text + "'r'n";
//string phone = "Phone: " + row.Cells[10].Text + "'r'n";
//string cctype = "Credit Card Type: " + row.Cells[11].Text + "'r'n";
//string ccnum = "Credit Card Number: " + row.Cells[12].Text + "'r'n";
//string ccexp = "Credit Card Expiration: " + row.Cells[13].Text + "'r'n";
string length = "Length: " + row.Cells[8].Text + "'r'n";
//string delivery = "Delivery: " + row.Cells[15].Text + "'r'n";
string price = "Price: " + row.Cells[7].Text + "'r'n";
string source = "Source: " + row.Cells[6].Text + "'r'n";
//string joined = "Joined: " + row.Cells[18].Text + "'r'n";
//string url = "URL: " + row.Cells[19].Text + "'r'n";
OrderBrowser.Text = name + email + length + price + source;
}
protected void CheckBoxProcess_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb = (CheckBox)sender;
GridViewRow gr = (GridViewRow)cb.NamingContainer;
if (cb.Checked)
{
OrderBrowser.Text = "checked";
}
else
{
OrderBrowser.Text = "unchecked";
}
}
}
ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="vieworders.aspx.cs" Inherits="Cabot3.custserv.vieworders" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID = "ScriptManager" runat="server" />
<div>
<div>
<asp:Label runat="server" id = "orderByLabel" Text = "Order By: " />
<asp:DropDownList runat="server" ID="orderByList" AutoPostBack="true">
<asp:ListItem Value="fName" Selected="True">First Name</asp:ListItem>
<asp:ListItem Value="lName">Last Name</asp:ListItem>
<asp:ListItem Value="state">State</asp:ListItem>
<asp:ListItem Value="zip">Zip Code</asp:ListItem>
<asp:ListItem Value="cwaSource">Source</asp:ListItem>
<asp:ListItem Value="cwaJoined">Date Joined</asp:ListItem>
</asp:DropDownList>
</div>
<div>
<asp:Label runat="server" ID="searchLabel" Text="Search For: " />
<asp:TextBox ID="searchTextBox" runat="server" Columns="30" />
<asp:Button ID="searchButton" runat="server" Text="Search" />
</div>
<div>
<asp:UpdatePanel ID = "up" runat="server">
<ContentTemplate>
<div style= "overflow:auto; height:150px; width:700px">
<asp:GridView ID="DefaultGrid" runat = "server" DataKeyNames = "fName, lName, zip"
onselectedindexchanged = "DefaultGrid_SelectedIndexChanged"
autogenerateselectbutton = "true"
selectedindex="0">
<SelectedRowStyle BackColor="Azure"
forecolor="Black"
font-bold="true" />
<Columns>
<asp:TemplateField HeaderText="Processed">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxProcess" OnCheckedChanged="CheckBoxProcess_CheckedChanged" AutoPostBack="true" runat="server" Enabled="true" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<asp:TextBox ID="OrderBrowser" columns="70" Rows="14" runat="server" Wrap="false" TextMode="MultiLine" ReadOnly = "true">
</asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
您可以将值绑定为…
<ItemTemplate>
<asp:CheckBox ID="CheckBoxProcess" OnCheckedChanged="CheckBoxProcess_CheckedChanged"
Checked='<%# Eval("FieldName") %>' AutoPostBack="true" runat="server" Enabled="true" />
</ItemTemplate>
其次更新CheckBox Checked/UnChecked
上的值,你可以使用gridview的RowCommand
Event,在那里你可以更新DB中的Field。例如
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "")
{
}
}