Repeater应用实例

绑定编辑数据:

 <asp:Repeater id="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound" OnItemCommand="Repeater1_ItemCommand">  
         <ItemTemplate>  
                                             <tr>
                                      </tr>
            <asp:Panel ID="plItem" runat="server">  
            <tr>  
              <td width="18%" rowspan="2" align="center">
                    <asp:Image ID="imgFace" runat="server" Width="100px" Height="120px" ImageUrl='<%# Eval( "imgPath") %>' /></td>
                <td width="14%" height="33" align="center">
                    <asp:Label ID="Label2" runat="server" Text="姓名:"></asp:Label></td>
                <td width="18%" align="center">
                    <asp:Label ID="Label3" runat="server" Text='<%# Eval( "username") %>'></asp:Label></td>
                <td width="25%" align="center">
                    <asp:Label ID="Label4" runat="server" Text="年龄:"></asp:Label></td>
                <td width="25%" align="center">
                    <asp:Label ID="Label5" runat="server" Text='<%# Eval( "age") %>'></asp:Label></td>
              </tr>
              <tr>
                <td height="40" align="center">
                    <asp:Label ID="Label6" runat="server" Text="电话:"></asp:Label></td>
                <td align="center">
                    <asp:Label ID="Label7" runat="server" Text='<%# Eval("telNum") %>'></asp:Label></td>
                <td align="center">
                    <asp:Label ID="Label8" runat="server" Text="地址:"></asp:Label></td>
                <td align="center">
                    <asp:Label ID="Label9" runat="server" Text='<%# Eval("address") %>'></asp:Label></td>
              </tr>
              <tr>
              <td align="center">
                  <asp:Label ID="Label12" runat="server" Text="头像"></asp:Label></td>
                <td>&nbsp;</td>
                <td height="37" align="center" >
                    <asp:Label ID="Label10" runat="server" Text="身份证号:"></asp:Label></td>
                <td align="center"><asp:Label ID="Label11" runat="server" Text='<%# Eval( "cardId") %>'>
                </asp:Label>
                </td>
               
                <td  align="left" style="width:30px">  
                    <asp:LinkButton runat="server" ID="lbtEdit" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "userid")%>' 
                     CommandName="Edit" Text="编辑"></asp:LinkButton>
                </td> 
                 <td>&nbsp;</td> 
            </tr>  
            </asp:Panel>  
            <asp:Panel ID="plEdit" runat="server">  
            <tr>  
               <td width="18%" rowspan="2" align="center">
                    <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval( "imgPath") %>' /></td>
                <td width="1%" height="33px" align="center">
                    <asp:Label ID="Label1" runat="server" Text="姓名:"></asp:Label></td>
                <td width="18%" align="center">
                    <asp:TextBox  ID="username" runat="server" Text='<%# Eval( "username") %>'></asp:TextBox >
                    </td>
            <td width="25%" align="center">
                <asp:Label ID="Label13" runat="server" Text="年龄:"></asp:Label>  </td>
                <td width="25%" align="center">
                    <asp:TextBox ID="age" runat="server" Text='<%# Eval("age") %>'></asp:TextBox></td>
              </tr>
              <tr>
                <td height="40" align="center">
                    <asp:Label  ID="Label16" runat="server" Text="电话:"></asp:Label ></td>
                <td align="center">
                    <asp:TextBox  ID="telNum" runat="server" Text='<%# Eval("telNum") %>'></asp:TextBox ></td>
                <td align="center">
                    <asp:Label ID="Label18" runat="server" Text="地址:"></asp:Label></td>
                <td align="center">
                    <asp:TextBox  ID="address" runat="server" Text='<%# Eval("address") %>'></asp:TextBox ></td>
              </tr>
              <tr>
              <td align="center">
                  <asp:Label ID="Label20" runat="server" Text="头像"></asp:Label></td>
                <td>&nbsp;</td>
                <td height="37" align="center" >
                    <asp:Label ID="Label21" runat="server" Text="身份证号:"></asp:Label></td>
                <td align="center"><asp:TextBox  ID="cardId" runat="server" Text='<%# Eval( "cardId") %>'></asp:TextBox ></td>
                <td>
                    <asp:LinkButton runat="server" ID="lbtUpdate" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "userid")%>' 
                     CommandName="Update" Text="更新"></asp:LinkButton>&nbsp;&nbsp;&nbsp;  
                    <asp:LinkButton runat="server" ID="lbtCancel" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "userid")%>' 
                     CommandName="Cancel" Text="取消"></asp:LinkButton>  
                 </td>  
            </tr>  
            </asp:Panel>             
         </ItemTemplate>  
      </asp:Repeater>

 

后台,包含treeview的绑定节点及子节点

 

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.SqlClient;
public partial class manage_empolyeeMessage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //bindMessage();
        }
       
    }
    protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
        //e.Node.Depth是获取节点的深度.Web.UI.webcontrols;
        switch (e.Node.Depth)
        {
            case 0:
                parentTree(e.Node);
                break;
            case 1:
                childTree(e.Node);
                break;
            default:
                break;
        }
    }
    //动态填充节点
    public void parentTree(TreeNode node)
    {
        DataSet ds = DBHelper.Valueset("SELECT departmentId, departmentName  FROM Department  where parenteid=0");
        if (ds.Tables.Count > 0)
        {
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                TreeNode Newnode = new TreeNode(row["departmentName"].ToString(), row["departmentId"].ToString());
                Newnode.PopulateOnDemand = true;
                Newnode.SelectAction = TreeNodeSelectAction.Expand;
                node.ChildNodes.Add(Newnode);
            }
        }
    }
    public void childTree(TreeNode node)
    {
        DataSet ds = DBHelper.Valueset("select userId,userName from userinfo where departmentId=" + node.Value);
        if (ds.Tables.Count > 0)
        {
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                TreeNode Newnode = new TreeNode(row["userName"].ToString());
                Newnode.PopulateOnDemand = true;
                Newnode.SelectAction = TreeNodeSelectAction.SelectExpand;
                //Newnode.NavigateUrl =
                node.ChildNodes.Add(Newnode);
            }
        }
    }
    protected void TreeView1_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
    {
    }
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        this.TreeView1.SelectedNode.Selected = true;
        bindMessage();
    }
    public void bindMessage()
    {
        string sql = "select * from userinfo where userName='" + TreeView1.SelectedNode.Text.ToString() + "'";
        DataSet ds = DBHelper.Valueset(sql);
        this.Repeater1.DataSource = ds.Tables[0];
        this.Repeater1.DataBind();
    }
     public int id = 0;
     //private void BindGrid()
     //{
     //    string sql = "select top 5 * from userinfo order by userid desc";
     //    DataSet ds = DBHelper.Valueset(sql);
     //    Repeater1.DataSource = ds.Tables[0];
     //    Repeater1.DataBind();
     //}
    protected void Repeater1_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DataRowView rowv = (DataRowView)e.Item.DataItem;
            string userid = rowv["userid"].ToString();
            if (userid != id.ToString())
            {
                ((Panel)e.Item.FindControl("plItem")).Visible = true;
                ((Panel)e.Item.FindControl("plEdit")).Visible = false;
            }
            else
            {
                ((Panel)e.Item.FindControl("plItem")).Visible = false;
                ((Panel)e.Item.FindControl("plEdit")).Visible = true;
            }
        }
    }
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            id = int.Parse(e.CommandArgument.ToString());
            bindMessage();
        }
        else if (e.CommandName == "Cancel")
        {
            ((Panel)e.Item.FindControl("plItem")).Visible = true;
            ((Panel)e.Item.FindControl("plEdit")).Visible = false;
        }
        else if (e.CommandName == "Update")
        {
            //Update. 
            string username = ((TextBox)this.Repeater1.Items[e.Item.ItemIndex].FindControl("username")).Text.Trim();
            string telNum = ((TextBox)this.Repeater1.Items[e.Item.ItemIndex].FindControl("telNum")).Text.Trim();
            string age = ((TextBox)this.Repeater1.Items[e.Item.ItemIndex].FindControl("age")).Text.Trim();
            string address = ((TextBox)this.Repeater1.Items[e.Item.ItemIndex].FindControl("address")).Text.Trim();
            string cardId = ((TextBox)this.Repeater1.Items[e.Item.ItemIndex].FindControl("cardId")).Text.Trim();
            this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "key", "alert('更新ID:" + e.CommandArgument + ";页面值:姓名=" +username+ "');", true);
            string sql_update = "update userinfo set telNum='" + telNum + "',username='" + username + "',age='" + age + "',address='" + address + "',cardId='" + cardId + "' where userid='" + int.Parse(e.CommandArgument.ToString()) + "'";
            DBHelper.Execunoquery(sql_update);
            bindMessage();
        }
        else if (e.CommandName == "Delete")
        { //Delete.
            this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "key", "alert('删除ID:" + e.CommandArgument + "');", true);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值