.net,页面有几个dropdownlist控件,默认是空的,页面加载完毕我通过jquery来赋值,可是保存不到数据库!

.net,页面有几个dropdownlist控件,默认是空的,页面加载完毕我通过jquery来赋值,可是保存不到数据库!,第1张

客户端javascript对DropDownList的动态添加,服务器端是无法知道的(这和视图状态有关系)

通用的做法,给保存按钮添加客户端事件,当点击保存按钮的时候,把DropDownList的值保存在一个Hidden控件上,然后在服务器端获取这个Hidden控件的值并分析

database

using System

using System.Data

using System.Configuration

using System.Web

using System.Web.Security

using System.Web.UI

using System.Web.UI.WebControls

using System.Web.UI.WebControls.WebParts

using System.Web.UI.HtmlControls

using System.Data.SqlClient

/// <summary>

/// database 的摘要说明

/// </summary>

public class database

{

private SqlConnection conn

public database()

{

//

// TODO: 在此处添加构造函数逻辑

//

string sqlcon = "Server=localhostInitial catalog=mydbuser=sapassword=123456"

conn = new SqlConnection(sqlcon)

}

public int executenon(string sqlstr)

{

conn.Open()

SqlCommand cmd = new SqlCommand(sqlstr, conn)

int num = cmd.ExecuteNonQuery()

return num

conn.Close()

}

public int executesal(string sqlstr)

{

conn.Open()

SqlCommand cmd = new SqlCommand(sqlstr, conn)

int num = Convert.ToInt32(cmd.ExecuteScalar())

return num

conn.Close()

}

public SqlDataReader executeread(string sqlstr)

{

SqlDataReader dr = null

conn.Open()

SqlCommand cmd = new SqlCommand(sqlstr, conn)

dr=cmd.ExecuteReader()

return dr

conn.Close()

}

public DataSet executedataset(string sqlstr)

{

DataSet ds=null

try

{

conn.Open()

SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn)

ds = new DataSet("userDataset")

da.Fill(ds, "userTable")

return ds

}

catch

{

return ds

}

}

}

order

using System

using System.Data

using System.Configuration

using System.Web

using System.Web.Security

using System.Web.UI

using System.Web.UI.WebControls

using System.Web.UI.WebControls.WebParts

using System.Web.UI.HtmlControls

using System.Text

using System.Collections

/// <summary>

/// order 的摘要说明

/// </summary>

public class order

{

public order()

{

//

// TODO: 在此处添加构造函数逻辑

//

}

}

public class OrderItemInfo

{

public int ProductID

public int Number

public double Price

public string Name

}

public class OrderInfo

{

public int TotalNumber

public double TotalMoney

public ArrayList OrderItemList = new ArrayList()

}

注册页面

database db = new database()

String xb

if (RadioButton1.Checked == true)

xb = "男"

else

xb = "女"

string sqlstr = "insert memberlist(memberid,memberpwd,membersex,membere_mail) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + xb + "','" + TextBox4.Text + "')"

int num = db.executenon(sqlstr)

if (num >0)

{

Response.Write("<script language='javascript'>alter('注册成功')</script>")

Response.Redirect("Default.aspx")

}

else

{

Response.Write("<script language='javascript'>alert('注册不成功')</script>")

Response.Redirect("loginpage.aspx")

登陆按钮

string meid = TextBox1.Text

string mepwd = TextBox2.Text

string sqlstr = null

if (RadioButton1.Checked)

{

sqlstr = "select count(memberid) from guanglilist where memberid='" + meid + "' and memberpwd='" + mepwd + "'"

}

else

{

sqlstr = "select count(memberid) from memberlist where memberid='" + meid + "' and memberpwd='" + mepwd + "' and checked='已审核'"

}

database db = new database()

int num = db.executesal(sqlstr)

if (num >0)

{

Response.Write("<script language='javascript'>alter('登陆成功')</script>")

MultiView1.ActiveViewIndex = 1

if (RadioButton1.Checked)

{

HyperLink1.Visible = true

}

else

{

HyperLink1.Visible = false

}

}

else

Response.Write("<script language='javascript'>alert('登录失败')</script>")

HttpCookie cookie = new HttpCookie("loginMessage")

cookie.Values.Add("meid", meid)

Response.Cookies.Add(cookie)

Label3.Text = meid

密码修改

protected void Page_Load(object sender, EventArgs e)

{

if (null != Request.Cookies["loginMessage"])

{

HttpCookie cookie = Request.Cookies["loginMessage"]

Label2.Text = Request.Cookies["loginMessage"]["meid"].ToString()

}

}

protected void Button1_Click(object sender, EventArgs e)

{

string opwd = TextBox1.Text

string npwd = TextBox2.Text

string usid = Label2.Text

string sqlstr = "update guanglilist set memberpwd='" + npwd + "' where memberid='" + usid + "' and memberpwd='" + opwd + "'"

database db = new database()

int num = db.executenon(sqlstr)

if (num >0)

{

Response.Write("<script language='javascript'>alert('密码修改成功')</script>")

}

else

{

Response.Write("<script language='javascript'>alert('原密码错误')</script>")

}

TextBox1.Text = ""

TextBox2.Text = ""

TextBox3.Text = ""

}

成员管理

屏蔽,审核:

string id = Convert.ToString(GridView1.SelectedDataKey.Value)

Response.Redirect("detail.aspx?id=" + id)

}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

{

if (e.CommandName.Equals("check"))

{

string id = Convert.ToString(GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value)

string sqlstr = "update memberlist set checked='已审核' where id='" + id + "'"

SqlDataSource1.UpdateCommand = sqlstr

SqlDataSource1.Update()

}

if (e.CommandName.Equals("uncheck"))

{

string id = Convert.ToString(GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value)

string sqlstr = "update memberlist set checked='未审核' where id='" + id + "'"

SqlDataSource1.UpdateCommand = sqlstr

SqlDataSource1.Update()

}

新闻管理

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

ViewState["model"] = Request["model"]

ViewState["id"] = Request["id"]

if (ViewState["model"].ToString().Equals("edit"))

{

database db = new database()

string sqlstr = "select * from newlise where id=" + Convert.ToInt32(ViewState["id"])

SqlDataReader dr = db.executeread(sqlstr)

if (dr.Read())

{

TextBox1.Text = dr[1].ToString()

TextBox2.Text = dr[2].ToString()

}

}

}

}

“添加”

protected void Button1_Click(object sender, EventArgs e)

{

string tle = TextBox1.Text

string cont = TextBox2.Text

string sql

database db = new database()

if (ViewState["model"].ToString().Equals("add"))

sql = "insert into newlist(title,content) values('" + tle + "','" + cont + "')"

else

sql = "update newlise set title='" + tle + "',content='" + cont + "' where id =" + Convert.ToInt32(ViewState["id"])

int num = db.executenon(sql)

if (num >0)

Response.Write("<script language='javascript'>alert ('提交成功!')</script>")

else

Response.Write("<script language='javascript'>alert (提交不成功!')</script>")

}

“编辑”

Response.Redirect("newsmanage.aspx?model=edit")

新书添加:

string lb_name = DropDownList1.SelectedValue.ToString()

string bk_name = TextBox2.Text

string bk_auth = TextBox3.Text

string bk_cbs = TextBox4.Text

Double bk_pri = Convert.ToDouble(TextBox6.Text)

string bk_jj = TextBox1.Text

string bk_ofile = File1.Value.ToString()

string[] tpfile = bk_ofile.Split('.')

string ext = "." + tpfile[tpfile.Length - 1]

Random r = new Random()

string bk_img = DateTime.Now.Year.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Second.ToString() + r.Next(100, 1000).ToString()

bk_img = bk_img + ext

File1.PostedFile.SaveAs(Server.MapPath("image/") + bk_img)

bk_img = "image" + "/" + bk_img

string sqlstr = "insert into booklist(bookname,autler,cbs,price,info,img,categary) values('" + bk_name + "','" + bk_auth + "','" + bk_cbs + "'," + bk_pri + ",'" + bk_jj + "','" + bk_img + "','" + lb_name + "')"

database db = new database()

int num = db.executenon(sqlstr)

if (num >0)

{

Response.Write("<script language='javascript'>alter('插入成功')</script>")

Response.Redirect("bookmanage.aspx")

}

else

{

Response.Write("<script language='javascript'>alter('插入失败')</script>")

}

购物

public partial class bookshop2 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (Request["name"] != null)

{

ViewState["name"] = Request["name"].ToString()

}

else

{

ViewState["name"] = "%"

}

if (!IsPostBack)

{

ViewState["pageindex"] = "0"

BindData(ViewState["name"].ToString())

}

}

private void BindData(string categoryName)

{

database db = new database()

string sqlstr = "SELECT * From booklist where categary like '%" + categoryName + "%'"

DataSet ds = db.executedataset(sqlstr)

DataTable dataTable = ds.Tables[0]

if (dataTable != null &&dataTable.Rows.Count >0)

{

DataView dataView = dataTable.DefaultView

PagedDataSource pds = new PagedDataSource()

pds.DataSource = dataView

pds.AllowPaging = true

pds.PageSize = 2

pds.CurrentPageIndex = Convert.ToInt32(ViewState["pageindex"].ToString())

lb_pageCount.Text = (Convert.ToInt32(ViewState["pageindex"]) + 1).ToString() + "/" + pds.PageCount.ToString() +

"页"

if (!pds.IsFirstPage)

{

lkPre.Visible = true

}

else

{

lkPre.Visible = false

}

if (!pds.IsLastPage)

{

lkNext.Visible = true

}

else

{

lkNext.Visible = false

}

DataList2.DataSource = pds

DataList2.DataBind()

}

else

{

lkNext.Visible = false

lkPre.Visible = false

lb_pageCount.Visible = false

}

}

protected void IndexChanging(object sender, EventArgs e)

{

string strCommand = ((LinkButton)sender).CommandArgument.ToString()

int pageindex = Convert.ToInt32(ViewState["pageindex"].ToString())

if (strCommand == "pre")

{

pageindex = pageindex - 1

}

else

{

pageindex = pageindex + 1

}

ViewState["pageindex"] = pageindex

BindData(ViewState["aname"].ToString())

}

protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)

{

OrderItemInfo item = null

OrderInfo order = null

string cmdName = null

int bookid = 0

string sqlstr = null

cmdName = e.CommandName.ToString()

if (cmdName.Equals("buyIt"))

{

bookid = Convert.ToInt32(e.CommandArgument)

sqlstr = "select bookname,price from booklist where id=" + bookid

}

if (Request.Cookies["loginMessage"] != null)

{

if (Session[Session.SessionID] == null)

{

item = new OrderItemInfo()

order = new OrderInfo()

database db = new database()

SqlDataReader dr = db.executeread(sqlstr)

dr.Read()

item.ProductID = bookid

item.Name = dr[0].ToString()

item.Number = 1

item.Price = Convert.ToDouble(dr[1])

order.OrderItemList.Add(item)

order.TotalMoney = item.Price

order.TotalNumber = item.Number

Session[Session.SessionID] = order

}

else

{

order = (OrderInfo)Session[Session.SessionID]

item = new OrderItemInfo()

OrderItemInfo tempItem = null

int i = 0

for (i = 0i <order.OrderItemList.Counti++)

{

tempItem = (OrderItemInfo)order.OrderItemList[i]

if (tempItem.ProductID == bookid)

{

tempItem.Number++

order.TotalMoney += tempItem.Price

break

}

}

if (i == order.OrderItemList.Count)

{

database db = new database()

SqlDataReader dr = db.executeread(sqlstr)

dr.Read()

item.ProductID = bookid

item.Name = dr[0].ToString()

item.Number = 1

item.Price = Convert.ToDouble(dr[1])

order.OrderItemList.Add(item)

order.TotalMoney += item.Price

}

order.TotalNumber++

Session[Session.SessionID] = order

}

Response.Write("<script>window.alert('恭喜您,添加该商品到购物车成功!')</script>")

}

else

{

Response.Write("<script>window.alert('未登陆用户不允许购买商品!')</script>")

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9423144.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存