您的当前位置:首页正文

Repeater实现删除,修改等操作

来源:化拓教育网
Repeater实现删除,修改等操作 2008年07月30日 星期三 15:09

<%# DataBinder.Eval(Container.DataItem, \"Loge_url\")%>

<%# DataBinder.Eval(Container.DataItem, \"link_name\")%>

<%# DataBinder.Eval(Container.DataItem, \"link_url\")%>

OnCommand=\"btnUpdate_Click\" />

OnCommand=\"btnDelete_Click\" OnClientClick=\"return confirm('确认删除吗?');\" />

<%--这是分隔线模板--%>


//修改

protected void btnUpdate_Click(object sender, CommandEventArgs e)

{

type++;

id = Convert.ToInt32(e.CommandName); entity.Link link = new entity.Link(); link = linkdao.SelectById(id);

this.name.Text = link.link_name.ToString(); this.address.Text = link.link_url.ToString(); this.logo.Text = link.Loge_url.ToString(); this.ddlType.ClearSelection(); this.ddlType.Items.FindByText((new

LinkTypeDao().SelectNameByid(link.type_id)).type_name.ToString()).Selected = true;

this.HiddenField1.Value = id.ToString(); }

//删除

protected void btnDelete_Click(object sender, CommandEventArgs e) {

int Id = Convert.ToInt32(e.CommandName); entity.Link link = new entity.Link(); link.link_id = Id; linkdao.Delete(link); Bind();

文章搜索: 【点击打包该文章】 【本站开通在线QQ讨论群】 最近写了个嵌套的例子,做为参考^.^ --------这是后置代码--------- using System; using System.Data; using System.Data.SqlClient; 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;

public partial class _Default : System.Web.UI.Page {

private const string connectionStr = @\"Data Source=.SQLEXPRESS;AttachDbFilename=D:QianTaoApp_Datadb.mdf;Integrated Security=True;User Instance=True\";

protected void Page_Load(object sender, EventArgs e) {

if(!IsPostBack) {

SetBind(); } }

protected void rptClass_ItemCommand(object source, RepeaterCommandEventArgs e)

{

//判断是否添加类别

if (e.CommandName == \"AddClass\") {

TextBox tb = e.Item.FindControl(\"txtClassName\") as TextBox; using(SqlConnection conn = new SqlConnection(connectionStr)) {

conn.Open();

using(SqlCommand cmd = new SqlCommand(\"insert into tb_Class(className) values(@className)\

{

cmd.Parameters.AddWithValue(\"@className\ cmd.ExecuteNonQuery(); SetBind(); } } }

//判断是否删除类别

if (e.CommandName == \"DelClass\") {

using (SqlConnection conn = new SqlConnection(connectionStr)) {

conn.Open();

using (SqlCommand cmd = new SqlCommand(\"delete from tb_Module where classId=@classId;delete from tb_Class where id=@classId\

{

cmd.Parameters.AddWithValue(\"@classId\);

cmd.ExecuteNonQuery(); SetBind(); } } }

//判断是否修改类别

if (e.CommandName == \"UpdateClass\") {

TextBox tb = e.Item.FindControl(\"txtClassName\") as TextBox; using (SqlConnection conn = new SqlConnection(connectionStr)) {

conn.Open();

using (SqlCommand cmd = new SqlCommand(\"update tb_Class set className=@className where id=@id\

{

cmd.Parameters.AddWithValue(\"@className\ cmd.Parameters.AddWithValue(\"@id\ cmd.ExecuteNonQuery(); SetBind(); } } }

//判断是否添加模块

if (e.CommandName == \"AddModule\") {

TextBox tb = e.Item.FindControl(\"txtModuleName\") as TextBox; using (SqlConnection conn = new SqlConnection(connectionStr)) {

conn.Open();

using (SqlCommand cmd = new SqlCommand(\"insert into tb_Module(classId,moduleName)values(@classId,@moduleName)\

{

cmd.Parameters.AddWithValue(\"@moduleName\ cmd.Parameters.AddWithValue(\"@classId\);

cmd.ExecuteNonQuery(); SetBind(); } } } }

/**////

/// 嵌套repeater的ItemCommand事件 ///

protected void rptModule_ItemCommand(object source, RepeaterCommandEventArgs e)

{

//判断是否删除模块

if (e.CommandName == \"DelModule\") {

using (SqlConnection conn = new SqlConnection(connectionStr)) {

conn.Open();

using (SqlCommand cmd = new SqlCommand(\"delete from tb_Module where id=@Id;\

{

cmd.Parameters.AddWithValue(\"@Id\ cmd.ExecuteNonQuery(); SetBind(); } } }

//判断是否修改模块

if (e.CommandName == \"UpdateModule\") {

TextBox tb = e.Item.FindControl(\"txtModuleName\") as TextBox; using (SqlConnection conn = new SqlConnection(connectionStr)) {

conn.Open();

using (SqlCommand cmd = new SqlCommand(\"update tb_Module set moduleName=@Name where id=@id\

{

cmd.Parameters.AddWithValue(\"@Name\ cmd.Parameters.AddWithValue(\"@id\ cmd.ExecuteNonQuery(); SetBind(); } } } }

/**////

/// 绑定Repeater /// private void SetBind() {

DataSet ds = new DataSet();

using(SqlConnection conn = new SqlConnection(connectionStr)) {

SqlDataAdapter adapter =

new SqlDataAdapter(\"select * from tb_Class\ adapter.Fill(ds); }

rptClass.DataSource = ds; rptClass.DataBind(); }

protected void rptClass_ItemDataBound(object sender, RepeaterItemEventArgs e)

{

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

{

Repeater rpt = e.Item.FindControl(\"rptModule\") as Repeater; //找到分类Repeater关联的数据项

DataRowView rowv = (DataRowView)e.Item.DataItem; //提取分类ID

int id = (int)rowv[\"id\"]; DataSet ds = new DataSet();

using (SqlConnection conn = new SqlConnection(connectionStr)) {

SqlDataAdapter adapter =

new SqlDataAdapter(\"select * from tb_Module where classId=\"+ id +\"\

adapter.Fill(ds); }

rpt.DataSource = ds; rpt.DataBind(); }

} }

------这是页面------

<%@ Page Language=\"C#\" AutoEventWireup=\"true\" CodeFile=\"Default.aspx.cs\" Inherits=\"_Default\" %>

嵌套RepeaterDEMO

添加类别:

添加模块:

    -----------------

andArgument='<%# Eval(\"id\") %>' CommandName=\"UpdateModule\" runat=\"server\" Text=\"修改模块\" />

因篇幅问题不能全部显示,请点此查看更多更全内容