<%# Eval("ArticleName") %>绑定后台数据的方法

来源:互联网 发布:富士通扫描仪软件收费 编辑:程序博客网 时间:2024/05/18 12:30

前台:

<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="UserManage.aspx.cs"Inherits="SPDMWebApp.UserManage"%>

 

<formid="Form1" method="post" runat="server"style="width: 570px">

<%--<center>--%>

<asp:TextBoxrunat="server" ID="TextBoxSearch"></asp:TextBox>

<asp:Buttonrunat="server" ID="Search" Text="搜索用户"onclick="Search_Click"></asp:Button>

<asp:Labelrunat="server" Width="40px"></asp:Label>

<asp:checkboxrunat="server" Text="显示系统管理用户"></asp:checkbox>

<divstyle="height: 20px"></div>

<div>

    <asp:buttonrunat="server" text="创建用户"Width="80px" onclick="Unnamed5_Click" />

    <asp:Labelrunat="server" Width="40px"></asp:Label>

    <asp:buttonrunat="server" text="删除用户"Width="80px" ID="BatchDelete" onclick="Unnamed7_Click"/>

    <asp:Labelrunat="server" Width="40px"></asp:Label>

    <asp:buttonrunat="server" text="LDAP同步"Width="80px" />

    <asp:Labelrunat="server" Width="40px"></asp:Label>

    <asp:buttonrunat="server" text="批量导入用"Width="80px" />

</div>

<br/>

<asp:panelrunat="server" BorderStyle="Groove" Width="560px" >

 

<asp:GridViewID="GridView1" runat="server" AutoGenerateColumns="False"Font-Size="X-Small"

        CellPadding="4"ForeColor="#333333"GridLines="Vertical"Width="550px"

        AllowPaging="True"onpageindexchanging="GridView1_PageIndexChanging"

        onrowdatabound="GridView1_RowDataBound"onrowdeleting="GridView1_RowDeleting"

        PageSize="5">

   

<AlternatingRowStyleBackColor="White" />

 

 

<Columns>

<asp:TemplateField>

     <HeaderTemplate>选择

     <%--<asp:Buttonrunat="server" Text="全选"onclick="Edit"></asp:Button>--%>

     </HeaderTemplate>

<ItemTemplate>

<asp:CheckBoxID="chkSelect" runat="server" />

</ItemTemplate>

</asp:TemplateField>

 

<asp:TemplateFieldHeaderText="用户名">

<ItemTemplate>

<asp:LiteralID="Literal1" runat="server" Text='<%# Eval("UserName")%>' />

</ItemTemplate>

</asp:TemplateField>

 

   

 

 

<asp:TemplateFieldHeaderText="用户全名">

        <ItemTemplate>

            <asp:LiteralID="Literal2" runat="server" Text='<%# Eval("UserChineseName")%>' />

       

</ItemTemplate>

   

 

 

</asp:TemplateField>

 

   

 

 

<asp:TemplateFieldHeaderText="部门">

        <ItemTemplate>

            <asp:LiteralID="Literal3" runat="server" Text='<%# Eval("DepartmentID")%>' />

       

</ItemTemplate>

   

 

 

</asp:TemplateField>

 

   

 

 

<asp:TemplateFieldHeaderText="电子邮件">

         <ItemTemplate>

            <asp:LiteralID="Literal4" runat="server" Text='<%# Eval("Email")%>' />       

</ItemTemplate>

</asp:TemplateField>

 

<asp:TemplateFieldHeaderText="编辑">   

        <ItemTemplate>

            <asp:LinkButtonID="edit" runat="server" onclick="Edit"CommandArgument='<%# Eval("UID")%>'>编辑</asp:LinkButton>

        </ItemTemplate>   

    </asp:TemplateField>

 

    <asp:CommandFieldDeleteText="&lt;div id=&quot;de&quot;onclick=&quot;JavaScript:return confirm('确定删除吗?')&quot;&gt;删除&lt;/div&gt; "

        HeaderText="删除"ShowDeleteButton="True">

    <ItemStyleForeColor="#0066FF"/>

    </asp:CommandField>

 

</Columns>

   

 

<EditRowStyleBackColor="#2461BF"/>

<FooterStyleBackColor="#507CD1"Font-Bold="True" ForeColor="White" />

<HeaderStyleBackColor="#507CD1"Font-Bold="True" ForeColor="White" />

<PagerStyleBackColor="#2461BF"ForeColor="White" HorizontalAlign="Center" />

<RowStyleBackColor="#EFF3FB"HorizontalAlign="Center"/>

<SelectedRowStyleBackColor="#D1DDF1"Font-Bold="True" ForeColor="#333333" />

<SortedAscendingCellStyleBackColor="#F5F7FB"/>

<SortedAscendingHeaderStyleBackColor="#6D95E1"/>

<SortedDescendingCellStyleBackColor="#E9EBEF"/>

<SortedDescendingHeaderStyleBackColor="#4870BE"/>

</asp:GridView>

<br/>

<asp:Buttonrunat="server" Text="全选"ID="Button_SelectAll"

        onclick="Button_SelectAll_Click"></asp:Button>

    <asp:Labelrunat="server" Width="10px"></asp:Label>

<asp:Buttonrunat="server" Text="全不选"ID="Button_NoSelectAll"

        onclick="Button_NoSelectAll_Click"></asp:Button>

</asp:panel>

<%--</center>--%>

 

</form>

 

后台:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Collections;

using BLL;

 

namespace SPDMWebApp

{

    public partial class UserManage : System.Web.UI.Page

    {

        public UserMap map = new UserMap();

       

   

 

        protectedvoid Page_Load(objectsender,EventArgs e)

        {

            if(!IsPostBack)

            {

                bind();

            }

        }

 

        protectedvoid Unnamed5_Click(objectsender,EventArgs e)

        {

            Response.Redirect("UserEdit.aspx?id=-1");

        }

 

        protectedvoid GridView1_PageIndexChanging(object sender,GridViewPageEventArgse)

        {

            try

            {

                RememberOldValues();

                GridView1.PageIndex =e.NewPageIndex;

                bind();

                RePopulateValues();

            }

            catch

            { }

        }

 

        protectedvoid GridView1_RowDeleting(object sender,GridViewDeleteEventArgse)

        {

            try

            {

                stringuserID = GridView1.DataKeys[e.RowIndex].Value.ToString();

                //*****************************LDAP模式下,用户无法在PDM系统中删除**************************

                //如何判断?????

                boolisSuccessful = map.Delete(userID);

                if(isSuccessful)

                    Alert("删除成功!");

                else

                    Alert("删除失败!");

            }

            catch(Exception ex)

            {

                stringerror = ex.ToString();

                Alert("删除失败!");

            }

            if(0 == TextBoxSearch.Text.Trim().Length)

                bind();

            else

                bindBySearch();

        }

 

 

        protectedvoid GridView1_RowDataBound(object sender,GridViewRowEventArgse)

        {

            inti;

            for(i = 0; i < GridView1.Rows.Count + 1; i++)// 网上代码这里没有加1,发现当移入移出第一行时颜色未变。

            {

                if(e.Row.RowType ==DataControlRowType.DataRow)  //判断当前行是否是数据行

                {

                    //当鼠标停留时更改背景色

                    e.Row.Attributes.Add("onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#FD2F00'");

                    //当鼠标移开时还原背景色

                    e.Row.Attributes.Add("onmouseout","this.style.backgroundColor=c");

                }

            }

        }

 

 

        protectedvoid Edit(objectsender,EventArgs e)

        {

            try

            {

                LinkButtonlb = (LinkButton)sender;

                stringid = lb.CommandArgument;

                Response.Redirect("UserEdit.aspx?id=" + id);

            }

            catch {}

        }

 

        protectedvoid Button_SelectAll_Click(object sender,EventArgse)

        {

            try

            {

                foreach(GridViewRow rowinGridView1.Rows)

                {

                    CheckBoxmyCheckBox = (CheckBox)row.FindControl("chkSelect");

                    myCheckBox.Checked = true;

                }

            }

            catch{ }

        }

 

        protectedvoid Button_NoSelectAll_Click(object sender,EventArgse)

        {

            try

            {

                foreach(GridViewRow rowinGridView1.Rows)

                {

                    CheckBoxmyCheckBox = (CheckBox)row.FindControl("chkSelect");

                    myCheckBox.Checked = false;

                }

            }

            catch{ }

        }

 

        // 删除Checkbox选中的数据

        protectedvoid Unnamed7_Click(objectsender,EventArgs e)

        {

            try

            {

                List<string> idList =newList<string>();

                foreach(GridViewRow rowinGridView1.Rows)

                {

                    CheckBoxmyCheckBox = (CheckBox)row.FindControl("chkSelect");

                    if(myCheckBox.Checked)

                    {

                        string id = GridView1.DataKeys[row.RowIndex].Value.ToString();

                        if (!idList.Contains(id))

                            idList.Add(id);

                    }

                }

                if(0 == idList.Count)

                {

                    Alert("请选择要删除的用户");

                    return;

                }

                BatchDelete.Attributes.Add("onclick","returnconfirm('确认要删除该用户吗?')");

 

                boolisSuccessful = map.Delete(idList);

                if(isSuccessful)

                    Alert("删除成功!");

                else

                    Alert("删除失败!");

            }

            catch(Exception ex)

            {

                stringerror = ex.ToString();

                Alert("删除失败!");

            }

            bind();

        }

 

        //搜索用户

        protectedvoid Search_Click(objectsender,EventArgs e)

        {

            DataSetmyds = new DataSet();

            try

            {

                stringword = TextBoxSearch.Text.Trim();

                if(0 == word.Length)

                {

                    Alert("请输入要搜索的用户!");

                    bind();

                    return;

                }

                myds = map.Search(word);

                if(0 == myds.Tables[0].Rows.Count)

                {

                    //this.Button_SelectAll.Visible= false;

                    //this.Button_NoSelectAll.Visible= false;

                    Alert("没有找到相关用户!");

                    bind();

                    return;

                }

                GridView1.DataSource = myds;

                GridView1.DataKeyNames = new string[] { "UID" };//设定主键默认不显示该列

                GridView1.DataBind();

            }

            catch(Exception ex)

            {

                stringerror = ex.ToString();

                Alert("没有找到相关用户!");

                bind();

            }

        }

 

      

 

        //绑定

        public void bind()

        {

            TextBoxSearch.Text = "";

            DataSetmyds = new DataSet();

            try

            {

                myds = map.GetDataSet();

                if(0 == myds.Tables[0].Rows.Count)

                {

                    this.Button_SelectAll.Visible=false;

                    this.Button_NoSelectAll.Visible=false;

                }

                GridView1.DataSource =myds;               

                GridView1.DataKeyNames = new string[] { "UID" };//设定主键默认不显示该列

                GridView1.DataBind();

            }

            catch(Exception ex)

            {

                stringerror = ex.ToString();

            }

 

        }

 

        //绑定搜索词

        public void bindBySearch()

        {

            DataSetmyds = new DataSet();

            try

            {

                stringword = TextBoxSearch.Text.Trim();

                if(0 == word.Length)

                {

                    bind();

                    return;

                }

                myds = map.Search(word);

                if(0 == myds.Tables[0].Rows.Count)

                {

                    //this.Button_SelectAll.Visible= false;

                    //this.Button_NoSelectAll.Visible= false;

                    bind();

                    return;

                }

                GridView1.DataSource = myds;

                GridView1.DataKeyNames = new string[] { "UID" };//设定主键默认不显示该列

                GridView1.DataBind();

            }

            catch(Exception ex)

            {

                stringerror = ex.ToString();

                bind();

            }

        }

 

        #region  分页时Checkbox的状态处理

        //记录checkbox的状态

        privatevoid RememberOldValues()

        {

            ArrayListcategoryIDList =new ArrayList();

            intindex = -1;

            foreach(GridViewRow rowinGridView1.Rows)

            {

                index = (int)GridView1.DataKeys[row.RowIndex].Value;

                boolresult = ((CheckBox)row.FindControl("chkSelect")).Checked;

 

                //Check in the Session

                if(Session["CHECKED_ITEMS"] !=null)

                    categoryIDList = (ArrayList)Session["CHECKED_ITEMS"];

                if(result)

                {

                    if(!categoryIDList.Contains(index))

                       categoryIDList.Add(index);

                }

                else

                   categoryIDList.Remove(index);

            }

            if(categoryIDList != null &&categoryIDList.Count > 0)

                Session["CHECKED_ITEMS"] = categoryIDList;

        }

 

        //从记录的状态恢复Checkbox的状态

        privatevoid RePopulateValues()

        {

            ArrayListcategoryIDList = (ArrayList)Session["CHECKED_ITEMS"];

            if(categoryIDList != null &&categoryIDList.Count > 0)

            {

                foreach(GridViewRow rowinGridView1.Rows)

                {

                    intindex = (int)GridView1.DataKeys[row.RowIndex].Value;

                    if(categoryIDList.Contains(index))

                    {

                        CheckBox myCheckBox = (CheckBox)row.FindControl("chkSelect");

                        myCheckBox.Checked = true;

                    }

                }

            }

        }       

         #endregion

 

 

 

        ///<summary>

        ///弹出对话框

        ///</summary>

        ///<paramname="message">提示语</param>

        public void Alert(stringmessage)

        {

            stringnewMessage = String.Format("<script language=javascript>alert('{0}');</script>", message);

            Response.Write(newMessage);

 

        }

 

    }

}

 



原创粉丝点击