您的位置:首页 > 编程语言 > ASP

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

2011-03-24 16:20 513 查看
PS: 数据库中 选中为4,否则为1

Cs Code

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Sigil.Common;
using SigilSoft.Data.SqlClient;

namespace Sigil.OutCall
{
public partial class ProductUpdate : System.Web.UI.Page
{
private Gladiator g = new Gladiator();

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGirls();
}
}

private void BindGirls()
{

string sql = @"SELECT productid,productname,av1.attributevalue as s1,av2.attributevalue as s2 ,productstate as s3 FROM product p
INNER JOIN attribute_value av1 ON p.productid=av1.id AND av1.attributeid=9
INNER JOIN attribute_value av2 ON p.productid=av2.id AND av2.attributeid=14
GROUP BY p.productid;";

GrilList.DataSource = g.GetDataTable(sql, SqlStringFrom.Program);
GrilList.DataBind();

}

protected void qx_Click(object sender, EventArgs e)
{
foreach (DataGridItem i in GrilList.Items)
{
CheckBox s3 = (CheckBox)i.FindControl("s3");
s3.Checked = true;
}
}
protected void fx_Click(object sender, EventArgs e)
{
foreach (DataGridItem i in GrilList.Items)
{
CheckBox s3 = (CheckBox)i.FindControl("s3");
s3.Checked = false;
}
}

protected void Submit_Click(object sender, EventArgs e)
{
bool s = false;

foreach (DataGridItem i in GrilList.Items)
{
string a = "";
string id = i.Cells[0].Text;

TextBox s1 = i.Cells[2].FindControl("s1") as TextBox;

TextBox s2 = i.Cells[2].FindControl("s2") as TextBox;

CheckBox s3 = i.Cells[2].FindControl("s3") as CheckBox;

if (s3.Checked)
{
a = "4";
}
else
{
a = "1";
}

s = UpdateGirls(id, s1.Text, s2.Text, a);

}
if (s)
MessageBox.Show("状态更新成功。", "", 0, MessageBoxType.Alert);
else
MessageBox.Show("更新失败,请稍候再试。", "", 0, MessageBoxType.Alert);
}

private bool UpdateGirls(string id, string s1, string s2, string s3)
{
try
{

string sql1 = "update attribute_value set attributevalue='" + s1 + "' where id=" + id + " and attributeid=9";

string sql2 = "update attribute_value set attributevalue='" + s2 + "' where id=" + id + " and attributeid=14";

string sql3 = "update product set productstate='" + s3 + "' where productid=" + id + "";

g.Save(sql1, SqlStringFrom.Program);

g.Save(sql2, SqlStringFrom.Program);

g.Save(sql3, SqlStringFrom.Program);

return true;
}
catch
{
return false;
}

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: