asp.net关于dropdownlist绑定后赋值的问题
注册信息的时候我的dropdownlist与数据表newsclass绑定了,然后在修改信息的界面同样绑定,但是没法默认为要修改的某条信息的值,比如newclass中有AB...
注册信息的时候我的dropdownlist与数据表newsclass绑定了,然后在修改信息的界面同样绑定,但是没法默认为要修改的某条信息的值,比如newclass中有A B C三类,某条信息是B类,当进入修改界面的时候dropdownlist依然默认为A类,请问怎么给它赋值,而且还可以选择,PS:数据绑定我是在aspx界面直接绑定的,不是在cs文件中用代码绑定的
展开
展开全部
我采用的是重新绑定
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;
namespace GridView练习
{
public partial class _Default : System.Web.UI.Page
{
public static OleDbConnection con;
public static DataSet ds = new DataSet();
public static OleDbDataAdapter sda = new OleDbDataAdapter();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{ data(); }
}
protected void CheckedChanged(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
((CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1")).Checked = ((CheckBox)GridView1.HeaderRow.FindControl("CheckBox2")).Checked;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (((CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1")).Checked == true)
{
string s = GridView1.Rows[i].Cells[1].Text.ToString();
OleDbCommand cmd = new OleDbCommand("delete from stu where id='" + s + "'", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
ds.Tables.Clear();
data();
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
string str = "insert into stu values ('" + TextBox1.Text.ToString() + "','" + TextBox2.Text.ToString() +"')";
OleDbCommand cmd = new OleDbCommand(str, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
ds.Tables.Clear();
data();
}
protected void data()
{
string str = "provider=microsoft.jet.oledb.4.0;data source=";
str += Server.MapPath("/") + "db1.mdb";
con = new OleDbConnection(str);
OleDbCommand cmd = new OleDbCommand("select * from stu", con);
sda.SelectCommand = cmd;
sda.Fill(ds, "stu");
GridView1.DataSource = ds.Tables["stu"].DefaultView;
GridView1.DataBind();
}
protected void Button3_Click(object sender, EventArgs e)
{
try
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (((CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1")).Checked == true)
{
string s = GridView1.Rows[i].Cells[1].Text.ToString();
string str = "update stu set id='" + TextBox1.Text.ToString() + "',stuname='" + TextBox2.Text.ToString() + "' where id='" + s + "'";
OleDbCommand cmd = new OleDbCommand(str, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
catch
{ }
ds.Tables.Clear();
data();
}
}
}
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;
namespace GridView练习
{
public partial class _Default : System.Web.UI.Page
{
public static OleDbConnection con;
public static DataSet ds = new DataSet();
public static OleDbDataAdapter sda = new OleDbDataAdapter();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{ data(); }
}
protected void CheckedChanged(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
((CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1")).Checked = ((CheckBox)GridView1.HeaderRow.FindControl("CheckBox2")).Checked;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (((CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1")).Checked == true)
{
string s = GridView1.Rows[i].Cells[1].Text.ToString();
OleDbCommand cmd = new OleDbCommand("delete from stu where id='" + s + "'", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
ds.Tables.Clear();
data();
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
string str = "insert into stu values ('" + TextBox1.Text.ToString() + "','" + TextBox2.Text.ToString() +"')";
OleDbCommand cmd = new OleDbCommand(str, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
ds.Tables.Clear();
data();
}
protected void data()
{
string str = "provider=microsoft.jet.oledb.4.0;data source=";
str += Server.MapPath("/") + "db1.mdb";
con = new OleDbConnection(str);
OleDbCommand cmd = new OleDbCommand("select * from stu", con);
sda.SelectCommand = cmd;
sda.Fill(ds, "stu");
GridView1.DataSource = ds.Tables["stu"].DefaultView;
GridView1.DataBind();
}
protected void Button3_Click(object sender, EventArgs e)
{
try
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (((CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1")).Checked == true)
{
string s = GridView1.Rows[i].Cells[1].Text.ToString();
string str = "update stu set id='" + TextBox1.Text.ToString() + "',stuname='" + TextBox2.Text.ToString() + "' where id='" + s + "'";
OleDbCommand cmd = new OleDbCommand(str, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
catch
{ }
ds.Tables.Clear();
data();
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
数据绑定怎么到页面文件去绑定操作了?!
最好检查下执行步骤,先绑定数据,后赋选择值
最好检查下执行步骤,先绑定数据,后赋选择值
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不太清楚,帮你关注
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询