我的datalist想写有编辑 删除的功能 但是出问题了 代码如下
//DataList删除对话框
protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
{
((LinkButton)e.Item.Controls[0].FindControl("LibDel")).Attributes.Add("onclick", "return confirm('确定要删除吗?')");
}
//DataList删除对话框
protected void DataList2_ItemCreated(object sender, DataListItemEventArgs e)
{
((LinkButton)e.Item.Controls[0].FindControl("LibDe2")).Attributes.Add("onclick", "return confirm('确定要删除吗?')");//这边出错了 提示错误未将对象引用设置到对象的实例。 } //DataList1删除
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
string strid = this.DataList1.DataKeys[e.Item.ItemIndex].ToString(); //获取当前DataList控件列
string sqlstr = "delete from ReciveSms where ID='" + Convert.ToInt32(strid) + "'";
if (operateData.execSql(sqlstr))
operateMethod.Alert("删除成功!", "Default.aspx", this);
}
//DataList2删除
protected void DataList2_DeleteCommand(object source, DataListCommandEventArgs e)
{
string strid = this.DataList1.DataKeys[e.Item.ItemIndex].ToString(); //获取当前DataList控件列
string sqlstr = "delete from SendSms where ID='" + Convert.ToInt32(strid) + "'";
if (operateData.execSql(sqlstr))
operateMethod.Alert("删除成功!", this);
bindReply(); //这是绑定数据库的方法
} //编辑回复跟帖
protected void DataList2_EditCommand(object source, DataListCommandEventArgs e)
{
this.DataList2.EditItemIndex = e.Item.ItemIndex;
bindReply();//这是绑定数据库的方法 }
//取消
protected void DataList2_CancelCommand(object source, DataListCommandEventArgs e)
{
this.DataList2.EditItemIndex = -1;
bindReply();
string sqlnum = sqlstr.Replace("*", "count(*)");
this.AspNetPager1.RecordCount = operateData.getCount(sqlnum);
}
//编辑帖子
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = e.Item.ItemIndex;
bindMessage();//这是绑定数据库的方法 }
//取消
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = -1;
bindMessage();//这是绑定数据库的方法 }
//更新帖子
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
string id = this.DataList1.DataKeys[e.Item.ItemIndex].ToString(); //使用前需先设置DataList的DataKeyField="id"
string Content = ((WebEditor)e.Item.FindControl("WebEditor1")).Text.Trim();//这里取不到内容?用断点调试后看出 content老是原来的值 取不到更新的内容???WebEditor是 DotNetTextBox控件 string sql = "update ReciveSms set Content ='" + operateMethod.strForm(Content) + "' where id=" + id + "";
if (operateData.execSql(sql))
{
operateMethod.Alert("编辑成功!", this);
Response.Redirect("Reply.aspx?id=" + Request["id"]);
}
else
{
operateMethod.Alert("编辑失败!" , this);
}
}
//更新跟帖回复
protected void DataList2_UpdateCommand(object source, DataListCommandEventArgs e)
{
string id = this.DataList2.DataKeys[e.Item.ItemIndex].ToString(); //使用前需先设置DataList的DataKeyField="id"
string Content = ((WebEditor)e.Item.FindControl("WebEditor2")).Text.Trim();//这里取不到内容?用断点调试后看出 content老是原来的值 取不到更新的内容???WebEditor是 DotNetTextBox控件 string sql = "update SendSms set Content ='" + operateMethod.strForm(Content) + "' where id=" + id + "";
if (operateData.execSql(sql))
{
operateMethod.Alert("编辑成功!", this);
Response.Redirect("Reply.aspx?id=" + Request["id"]);
}
else
{
operateMethod.Alert("编辑失败!", this);
}
}
上面两处错误 哪位大哥帮忙解决下吧 谢过
//DataList删除对话框
protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
{
((LinkButton)e.Item.Controls[0].FindControl("LibDel")).Attributes.Add("onclick", "return confirm('确定要删除吗?')");
}
//DataList删除对话框
protected void DataList2_ItemCreated(object sender, DataListItemEventArgs e)
{
((LinkButton)e.Item.Controls[0].FindControl("LibDe2")).Attributes.Add("onclick", "return confirm('确定要删除吗?')");//这边出错了 提示错误未将对象引用设置到对象的实例。 } //DataList1删除
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
string strid = this.DataList1.DataKeys[e.Item.ItemIndex].ToString(); //获取当前DataList控件列
string sqlstr = "delete from ReciveSms where ID='" + Convert.ToInt32(strid) + "'";
if (operateData.execSql(sqlstr))
operateMethod.Alert("删除成功!", "Default.aspx", this);
}
//DataList2删除
protected void DataList2_DeleteCommand(object source, DataListCommandEventArgs e)
{
string strid = this.DataList1.DataKeys[e.Item.ItemIndex].ToString(); //获取当前DataList控件列
string sqlstr = "delete from SendSms where ID='" + Convert.ToInt32(strid) + "'";
if (operateData.execSql(sqlstr))
operateMethod.Alert("删除成功!", this);
bindReply(); //这是绑定数据库的方法
} //编辑回复跟帖
protected void DataList2_EditCommand(object source, DataListCommandEventArgs e)
{
this.DataList2.EditItemIndex = e.Item.ItemIndex;
bindReply();//这是绑定数据库的方法 }
//取消
protected void DataList2_CancelCommand(object source, DataListCommandEventArgs e)
{
this.DataList2.EditItemIndex = -1;
bindReply();
string sqlnum = sqlstr.Replace("*", "count(*)");
this.AspNetPager1.RecordCount = operateData.getCount(sqlnum);
}
//编辑帖子
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = e.Item.ItemIndex;
bindMessage();//这是绑定数据库的方法 }
//取消
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
this.DataList1.EditItemIndex = -1;
bindMessage();//这是绑定数据库的方法 }
//更新帖子
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
string id = this.DataList1.DataKeys[e.Item.ItemIndex].ToString(); //使用前需先设置DataList的DataKeyField="id"
string Content = ((WebEditor)e.Item.FindControl("WebEditor1")).Text.Trim();//这里取不到内容?用断点调试后看出 content老是原来的值 取不到更新的内容???WebEditor是 DotNetTextBox控件 string sql = "update ReciveSms set Content ='" + operateMethod.strForm(Content) + "' where id=" + id + "";
if (operateData.execSql(sql))
{
operateMethod.Alert("编辑成功!", this);
Response.Redirect("Reply.aspx?id=" + Request["id"]);
}
else
{
operateMethod.Alert("编辑失败!" , this);
}
}
//更新跟帖回复
protected void DataList2_UpdateCommand(object source, DataListCommandEventArgs e)
{
string id = this.DataList2.DataKeys[e.Item.ItemIndex].ToString(); //使用前需先设置DataList的DataKeyField="id"
string Content = ((WebEditor)e.Item.FindControl("WebEditor2")).Text.Trim();//这里取不到内容?用断点调试后看出 content老是原来的值 取不到更新的内容???WebEditor是 DotNetTextBox控件 string sql = "update SendSms set Content ='" + operateMethod.strForm(Content) + "' where id=" + id + "";
if (operateData.execSql(sql))
{
operateMethod.Alert("编辑成功!", this);
Response.Redirect("Reply.aspx?id=" + Request["id"]);
}
else
{
operateMethod.Alert("编辑失败!", this);
}
}
上面两处错误 哪位大哥帮忙解决下吧 谢过
解决方案 »
- 如何在 gridview 中添加信息。启用什么东西就可以实现ADD???????
- 求提取网页中信息的 方法
- 这个js怎么做 判断用户浏览器是否加载完毕
- 求网站Licenses机制编制方法或示例源码,Licenses能与网站数据库版本、功能集合及网站域名捆绑相结合
- asp.net使用datalist到指定页面的问题 在线等呀
- 读取XML报错
- 博客上的日历!
- 为什么不能更新数据库。
- 我通过查询得到的数据集,绑定到DATAGRID里有个删除控件,当我删除一个后怎样绑定剩下的数据集
- asp.net编码问题,小弟做毕设,急!在线等。
- FckEditor官方网站的上网图片功能为什么不好用(哪位有更好的FckEditor)?
- [100分]三层中的ObjectDataSource更新gridview控件数据时提示“未能找到带参数的非泛型方法”,纠结2天了,寻求高手帮忙解决,解决即结帖!
根据你的错误提示,是没有找到LinkButton
==
十之八九是Page_Load绑定数据时没有判断回传页
if(!IsPostBack)
{
//绑定DataList数据
}((LinkButton)e.Item.Controls[0].FindControl("LibDe2")).Attributes.Add("onclick", "return confirm('确定要删除吗?')");//这边出错了 提示错误未将对象引用设置到对象的实例。
==
直接在aspx页面绑定按钮的 OnClientClick属性即可
if(!IsPostBack)
{
BindData();
}
private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
LinkButton lbDelete = (LinkButton)e.Item.FindControl("lbDel"); lbDelete.Attributes.Add("onclick", "return confirm(\"确定删除吗?\");");
}
}
用SQLdatasource连接数据库不是很简单
//编辑
protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
this.DetailsView1.ChangeMode(e.NewMode);
this.DetailsView1.DataBind();
Detailsbind();
}
//更新
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
//string 运用区段 = ((Label)DetailsView1.FindControl("Label5")).Text;
//((DropDownList)DetailsView1.FindControl("运用区段")).Items.Add(运用区段); string 设备名称;
string 型号;
string 编号;
string 运用区段;
string SqlUpdate; 设备名称 = ((TextBox)DetailsView1.Rows[1].Cells[1].Controls[0]).Text;
//tb = (TextBox)(DetailsView1.Rows[5].Cells[1].FindControl("TextBox5"));
型号 = ((TextBox)DetailsView1.Rows[2].Cells[1].Controls[0]).Text;
//tb = (TextBox)(DetailsView1.Rows[1].Cells[1].FindControl("TextBox1"));
编号 = ((TextBox)DetailsView1.Rows[3].Cells[1].Controls[0]).Text;
//tb = (TextBox)(DetailsView1.Rows[6].Cells[1].FindControl("TextBox6"));
//运用区段 = (Me.DetailsView1.FindControl(" 运用区段"), DropDownList).SelectedValue;
//运用区段 = ((DropDownList)DetailsView1.Rows[5].Cells[1].Controls["运用区段"]).SelectedValue;
//运用区段 = ((DropDownList)DetailsView1.Rows[5].FindControl("DropDownList1")).Text;
运用区段 = ((TextBox)DetailsView1.Rows[5].Cells[1].Controls[0]).Text;
string 上线运用日期 = ((TextBox)DetailsView1.Rows[6].Cells[1].Controls[0]).Text;
string 配属单位 = ((TextBox)DetailsView1.Rows[4].Cells[1].Controls[0]).Text;
string 设备厂家 = ((TextBox)DetailsView1.Rows[7].Cells[1].Controls[0]).Text;
string 状态 = ((TextBox)DetailsView1.Rows[8].Cells[1].Controls[0]).Text; string ID号 = GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString();
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "data source=90D57B80B843459;database=Equipment;uid=sa;pwd=;";
SqlUpdate = "update [liewei1] set 设备名称 = '" + 设备名称 + "',型号 = '" + 型号 + "', 编号 = '" + 编号 + "',配属单位 = '" + 配属单位 + "',运用区段 = '" + 运用区段 + "',上线运用日期='" + 上线运用日期 + "',设备厂家 = '" + 设备厂家 + "',状态 = '" + 状态 + "'where ID号=" + ID号; //con.Open();
//SqlCommand cmd = new SqlCommand(SqlUpdate, con);
//cmd.ExecuteNonQuery(); //con.Close();
//try
//{ if (conn.State.ToString() == "Closed")
conn.Open();
SqlCommand comm = new SqlCommand(SqlUpdate, conn);
comm.ExecuteNonQuery(); if (conn.State.ToString() == "Open")
conn.Close();
Detailsbind();
BindGridView();
DetailsView1.Visible = false;
//}
//catch (Exception ex)
//{
// Response.Write("数据库错误,错误原因:" + ex.Message);
// Response.End();
//} }
protected void DetailsView1_ModeChanged(object sender, EventArgs e)
{ }
protected void Detailsbind()
{
SqlConnection sqlcon = new SqlConnection();
sqlcon.ConnectionString = "data source=90D57B80B843459;database=Equipment;uid=sa;pwd=;";
sqlcon.Open();
string ID号 = GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString();
string sqlstr = "select ID号,设备名称,型号,编号,配属单位,运用区段,上线运用日期,设备厂家,状态 from [liewei1] where ID号=" + ID号; SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet(); myda.Fill(myds); DetailsView1.DataSource = myds;
this.DetailsView1.DataBind();
}
//删除
protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "data source=90D57B80B843459;database=Equipment;uid=sa;pwd=;";
string ID号 = GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString();
string SqlStr = "delete from [liewei1] where ID号=" + ID号; try
{ if (conn.State.ToString() == "Closed")
conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
comm.ExecuteNonQuery();
comm.Dispose();
if (conn.State.ToString() == "Open")
conn.Close(); Detailsbind();
BindGridView();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
} }
protected void DataList1_ItemCreated(object sender, DataListItemEventArgs e)
{
((LinkButton)e.Item.Controls[0].FindControl("LibDel")).Attributes.Add("onclick", "return confirm('确定要删除吗?')");//为什么这里老是提示这边出错了 提示错误:未将对象引用设置到对象的实例 }
前台的代码是这样的
<asp:LinkButton ID="LibDel" runat="server" CommandName="Delete">删除</asp:LinkButton>
没有找到你的findControl里的东东
LibDel这个存在吗?
可以去我的博客看一下,我上面有一种获取DATALIST中控件值 的方法
www.blog.sina.com.cn/pywangwei
{
//如果是绑定数据行 if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
LinkButton lbDelete = (LinkButton)e.Item.FindControl("LinkButton2"); lbDelete.Attributes.Add("onclick", "return confirm(\"确定删除吗?\");");
}
}这样可以,经过本人的验证!!