读书人

repeater里面控件的一些有关问题

发布时间: 2012-05-03 14:06:56 作者: rapoo

repeater里面控件的一些问题
我在repeater里面放了一个表 表里面有2个Label和一个TextBox 两个Button 这些不是重点
[code=C#][/code] protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
/*DataRowView dv = (DataRowView)e.Item.DataItem; -----这里我注释掉了,本来我向这里连接,下面的操作都可以用着command 但是 update操作可以 但是 只要delete 操作全都是asp.net未将对象引用设置到对象的实例错误

string floor = dv["floor"].ToString();

OleDbCommand cmd = new OleDbCommand();

cmd.Connection = con;

cmd.CommandType = CommandType.Text; */

if (e.CommandName == "edit")
{


TextBox bb = (TextBox)e.Item.FindControl("TextBox1");


Button tt = (Button)e.Item.FindControl("Button1");

if (!bb.Enabled)
{
bb.Enabled = true;



tt.Text = "发布";
}
else
{
DataRowView dv = (DataRowView)e.Item.DataItem;

string floor = dv["floor"].ToString();

OleDbCommand cmd = new OleDbCommand();

cmd.Connection = con;

cmd.CommandText = "update tabl1 set [text] ='" + bb.Text + "' where t_id='" + Session["t_id"].ToString() + "'&&floor=" + floor;

cmd.ExecuteNonQuery();

bb.Enabled = false;

// tt.Text = "编辑";

// con.Close();



}


}

if(e.CommandName == "dele")
{

DataRowView dvv = (DataRowView)e.Item.DataItem;

string floorr = dvv["floor"].ToString(); //asp.net未将对象引用设置到对象的实例错误在这里发生

OleDbCommand cmd = new OleDbCommand();

cmd.Connection = con;

cmd.CommandType = CommandType.Text;

con.Open();

cmd.CommandText = "delete from tabl1 where t_id='" + Session["t_id"].ToString() + "' &&floor=" + floorr;

cmd.CommandType = CommandType.Text;

cmd.ExecuteNonQuery();




}


}


执行delete操作就asp.net未将对象引用设置到对象的实例错误 而update操作就可以


我甚至改了commandText 内容外 其他内容都一样也不行 求教

[解决办法]
系统会给每一种命令按钮列都制定了默认的CommandName属性值,“删除”命令按钮列的CommandName属性值为Delete。

你的这种在Repeater前台通过模板列放置一个LinkButton,绑定其CommandArgument属性,然后在LinkButton的OnCommand事件中通过e.CommandArgument获取该条记录的主键后,然后进行相关的更新和删除操作就方便多了。

读书人网 >C#

热点推荐