asp.net CheckBox从数据库读取数据设定是否选中 并更新

PS: 数据库中 选中为4,否则为1

View Code

1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Product-Update.aspx.cs"
2 Inherits="Sigil.OutCall.ProductUpdate" %>
3
4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5 <html xmlns="http://www.w3.org/1999/xhtml">
6 <head runat="server">
7 <title></title>
8 </head>
9 <body>
10 <form id="form" runat="server">
11 <div>
12 <asp:DataGrid ID="GrilList" runat="server" AutoGenerateColumns="False" BorderColor="#DEDFDE"
13 Width="72%" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4"
14 GridLines="Vertical" ForeColor="Black">
15 <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#CE5D5A"></SelectedItemStyle>
16 <AlternatingItemStyle BackColor="White"></AlternatingItemStyle>
17 <ItemStyle BackColor="#F7F7DE"></ItemStyle>
18 <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#6B696B"></HeaderStyle>
19 <FooterStyle BackColor="#CCCC99"></FooterStyle>
20 <Columns>
21 <asp:BoundColumn DataField="ProductId" HeaderText="" Visible="false"></asp:BoundColumn>
22 <asp:TemplateColumn>
23 <HeaderTemplate>
24 <div style="text-align: center;">
25 名称</div>
26 </HeaderTemplate>
27 <ItemTemplate>
28 <div style="text-align: center;">
29 <%# DataBinder.Eval(Container.DataItem,"productname")%></div>
30 </ItemTemplate>
31 </asp:TemplateColumn>
32 <asp:TemplateColumn>
33 <HeaderTemplate>
34 <div style="text-align: center;">
35 状态1</div>
36 </HeaderTemplate>
37 <ItemTemplate>
38 <div style="text-align: center;">
39 <asp:TextBox ID="s1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"s1")%>'></asp:TextBox></div>
40 </ItemTemplate>
41 </asp:TemplateColumn>
42 <asp:TemplateColumn>
43 <HeaderTemplate>
44 <div style="text-align: center;">
45 状态2</div>
46 </HeaderTemplate>
47 <ItemTemplate>
48 <div style="text-align: center;">
49 <asp:TextBox ID="s2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"s2")%>'></asp:TextBox></div>
50 </ItemTemplate>
51 </asp:TemplateColumn>
52 <asp:TemplateColumn>
53 <HeaderTemplate>
54 <div style="text-align: center;">
55 状态3</div>
56 </HeaderTemplate>
57 <ItemTemplate>
58 <div style="text-align: center;">
59 <asp:CheckBox ID="s3" runat="server" Checked='<%# DataBinder.Eval(Container.DataItem,"s3").ToString()=="4"%>' /></div>
60 </ItemTemplate>
61 </asp:TemplateColumn>
62 </Columns>
63 <PagerStyle HorizontalAlign="Right" ForeColor="Black" BackColor="#F7F7DE" Mode="NumericPages">
64 </PagerStyle>
65 </asp:DataGrid>
66 <p>
67 <asp:Button ID="Submit" runat="server" Text="更新状态..." OnClick="Submit_Click" Style="height: 26px" />
68 <asp:Button ID="qx" runat="server" Text="全选" OnClick="qx_Click" Style="height: 26px" />
69 <asp:Button ID="fx" runat="server" Text="反选" OnClick="fx_Click" Style="height: 26px" /></p>
70 </div>
71 </form>
72 </body>
73 </html>
Cs Code
1 using System;
2 using System.Web;
3 using System.Web.UI;
4 using System.Web.UI.WebControls;
5
6 using Sigil.Common;
7 using SigilSoft.Data.SqlClient;
8
9 namespace Sigil.OutCall
10 {
11 public partial class ProductUpdate : System.Web.UI.Page
12 {
13 private Gladiator g = new Gladiator();
14
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 if (!IsPostBack)
18 {
19 BindGirls();
20 }
21 }
22
23 private void BindGirls()
24 {
25
26 string sql = @"SELECT productid,productname,av1.attributevalue as s1,av2.attributevalue as s2 ,productstate as s3 FROM product p
27 INNER JOIN attribute_value av1 ON p.productid=av1.id AND av1.attributeid=9
28 INNER JOIN attribute_value av2 ON p.productid=av2.id AND av2.attributeid=14
29 GROUP BY p.productid;";
30
31 GrilList.DataSource = g.GetDataTable(sql, SqlStringFrom.Program);
32 GrilList.DataBind();
33
34 }
35
36 protected void qx_Click(object sender, EventArgs e)
37 {
38 foreach (DataGridItem i in GrilList.Items)
39 {
40 CheckBox s3 = (CheckBox)i.FindControl("s3");
41 s3.Checked = true;
42 }
43 }
44 protected void fx_Click(object sender, EventArgs e)
45 {
46 foreach (DataGridItem i in GrilList.Items)
47 {
48 CheckBox s3 = (CheckBox)i.FindControl("s3");
49 s3.Checked = false;
50 }
51 }
52
53
54 protected void Submit_Click(object sender, EventArgs e)
55 {
56 bool s = false;
57
58 foreach (DataGridItem i in GrilList.Items)
59 {
60 string a = "";
61 string id = i.Cells[0].Text;
62
63 TextBox s1 = i.Cells[2].FindControl("s1") as TextBox;
64
65 TextBox s2 = i.Cells[2].FindControl("s2") as TextBox;
66
67 CheckBox s3 = i.Cells[2].FindControl("s3") as CheckBox;
68
69
70 if (s3.Checked)
71 {
72 a = "4";
73 }
74 else
75 {
76 a = "1";
77 }
78
79
80 s = UpdateGirls(id, s1.Text, s2.Text, a);
81
82 }
83 if (s)
84 MessageBox.Show("状态更新成功。", "", 0, MessageBoxType.Alert);
85 else
86 MessageBox.Show("更新失败,请稍候再试。", "", 0, MessageBoxType.Alert);
87 }
88
89
90 private bool UpdateGirls(string id, string s1, string s2, string s3)
91 {
92 try
93 {
94
95 string sql1 = "update attribute_value set attributevalue='" + s1 + "' where id=" + id + " and attributeid=9";
96
97 string sql2 = "update attribute_value set attributevalue='" + s2 + "' where id=" + id + " and attributeid=14";
98
99 string sql3 = "update product set productstate='" + s3 + "' where productid=" + id + "";
100
101 g.Save(sql1, SqlStringFrom.Program);
102
103 g.Save(sql2, SqlStringFrom.Program);
104
105 g.Save(sql3, SqlStringFrom.Program);
106
107 return true;
108 }
109 catch
110 {
111 return false;
112 }
113
114
115
116 }
117 }
118 }
posted @ 2011-03-24 16:20  Ryo  阅读(529)  评论(0编辑  收藏  举报