Repeater控件的

it2026-09-28  8

http://blog.csdn.net/zhang_xinxiu/article/details/21872433

想起来,公司的aspx页面前台数据展示除了datagrid以为还有Repeater控件,现在温习温习这个控件

1:// 从一个数据项中获得相应的控件        TextBox txtTitle = (TextBox)e.Item.FindControl("txtTitle");     

得记住这样获取值的方式,(控件类型)的转换

 2:CheckBox chkInTheaters = (CheckBox)e.Item.FindControl("chkInTheaters");

 <asp:Repeater ID="userRepeat" runat="server" OnItemCommand="userRepeat_ItemCommand" OnItemDataBound="userRepeat_ItemDataBound">  

标红的是 Repeater控件命令事件<ItemTemplate><tr><td><%#Eval("ID") %></td><td><%#Eval("Title") %></td><td><%#Eval("Cont") %></td><td><%#Eval("Keys") %></td><td><%#Eval("Des") %></td><td><%#Eval("AddTime") %></td><td>

<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Del" CommandArgument='<%#Eval("ID") %>'>删除</asp:LinkButton><asp:LinkButton ID="LinkButton2" runat="server" CommandName="Update" CommandArgument='<%#Eval("ID") %>'>修改</asp:LinkButton>

<asp:Label ID="Label1" runat="server" Text='<%#Eval("NewID") %>' Visible="false"></asp:Label><asp:LinkButton ID="LinkButton1" runat="server" CssClass="btn btn82 btn_del" CommandName="del" OnClientClick="return confirm('要删除吗?')">删除</asp:LinkButton>&nbsp; &nbsp; &nbsp; &nbsp;<asp:Label ID="Label2" runat="server" Text='<%#Eval("NewID") %>' Visible="false"></asp:Label><asp:LinkButton ID="LinkButton2" runat="server" CommandName="Update" CommandArgument='<%#Eval("NewID")%>'>修改</asp:LinkButton>

 

前台代码通过设置控件的CommandArgument属性来传递后台所需要判断的id号。

两种不同的颜色相互对应,当点击删除时,获取"Del"的指令  然后获取到要删除数据的ID值

if (e.CommandName == "Del"){//string strid = ((Label)e.Item.FindControl("Label1")).Text;// int Id = Convert.ToInt32(strid);int id = Convert.ToInt32(e.CommandArgument.ToString());

 

绑定数据:

/// <summary>          /// 将数据源绑定Repeater控件上          /// </summary>          private void DataBindToRepeater() {              //使用using语句进行数据库连接              using (SqlConnection sqlCon=new SqlConnection("server=.;database=MyBlog;uid=sa;pwd=1"))              {                  sqlCon.Open();  //打开数据库连接                    SqlCommand sqlcom = new SqlCommand();   //创建数据库命令对象                  sqlcom.CommandText = "select * from match"; //为命令对象指定执行语句                  sqlcom.Connection = sqlCon; //为命令对象指定连接对象                    this.userRepeat.DataSource = sqlcom.ExecuteReader();    //为Repeater对象指定数据源                  this.userRepeat.DataBind(); //绑定数据源              }          }   protected void userRepeat_ItemCommand(object source, RepeaterCommandEventArgs e)          {              //获取命令文本,判断发出的命令为何种类型,根据命令类型调用事件              if (e.CommandName=="Edit")  //编辑命令              {                  id = int.Parse(e.CommandArgument.ToString());   //获取命令ID号              }              else if (e.CommandName=="Cancel")   //取消更新命令              {                  id = -1;              }              else if(e.CommandName=="Delete")    //删除行内容命令              {                  id = int.Parse(e.CommandArgument.ToString());   //获取删除行的ID号                  //删除选定的行,并重新指定绑定操作                  this.DeleteRepeater(id);              }              else if (e.CommandName == "Update") //更新行内容命令              {                  //获取更新行的内容和ID号                  string strText = ((TextBox)e.Item.FindControl("txtName")).Text.Trim();                  int intId=int.Parse(((Label)e.Item.FindControl("lblID")).Text);                  //更新Repeater控件的内容                  this.UpdateRepeater(strText,intId);              }                //重新绑定控件上的内容              this.DataBindToRepeater();          }     SqlCommand sqlcom = new SqlCommand();   //创建数据库命令对象                  sqlcom.CommandText = "update match set name=@str where id=@id"; //为命令对象指定执行语句                  sqlcom.Connection = sqlCon; //为命令对象指定连接对象                    //创建参数集合,并向sqlcom中添加参数集合                  SqlParameter[] sqlParam = { new SqlParameter("@str", strText), new SqlParameter("@id", intId) };                  sqlcom.Parameters.AddRange(sqlParam);  

转载于:https://www.cnblogs.com/ZkbFighting/p/8409874.html

最新回复(0)