首页 新闻 会员 周边 捐助

请问我的这段代码为什么没有排序的功能效果呢?

0
悬赏园豆:10 [已关闭问题]

这是源代码:

<form id="form1" runat="server">
    <div>
        &nbsp;
        <div style="z-index: 101; left: 123px; width: 615px; position: absolute; top: 152px;
            height: 146px">
        <asp:DataGrid ID="DGInfo" runat="server" AutoGenerateColumns="False" OnDeleteCommand="DGInfo_DeleteCommand" OnItemDataBound="DGInfo_ItemDataBound" CellPadding="4" ForeColor="#333333" GridLines="None">
            <Columns>
                <asp:BoundColumn DataField="UserName" HeaderText="用户名字"></asp:BoundColumn>
                <asp:BoundColumn DataField="PassWord" HeaderText="密 码"></asp:BoundColumn>
                <asp:BoundColumn DataField="FirstPass" HeaderText="确认密码"></asp:BoundColumn>
                <asp:BoundColumn DataField="Age" HeaderText="年 龄"></asp:BoundColumn>
                <asp:BoundColumn DataField="Birthday" HeaderText="生 日"></asp:BoundColumn>
                <asp:BoundColumn DataField="Email" HeaderText="Email"></asp:BoundColumn>
                <asp:BoundColumn DataField="Thone" HeaderText="手机号"></asp:BoundColumn>
                <asp:ButtonColumn CommandName="Delete" HeaderText="删除" Text="删除"></asp:ButtonColumn>
                <asp:HyperLinkColumn  HeaderText="修改" DataNavigateUrlField="id" DataNavigateUrlFormatString="UpdateInfo.aspx?id={0}" Text="修改" ></asp:HyperLinkColumn>
            </Columns>
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditItemStyle BackColor="#2461BF" />
            <SelectedItemStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <AlternatingItemStyle BackColor="White" />
            <ItemStyle BackColor="#EFF3FB" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        </asp:DataGrid></div>
        <div style="z-index: 102; left: 129px; width: 435px; position: absolute; top: 63px;
            height: 1px">
            <table style="width: 601px; height: 90px">
                <tr>
                    <td style="width: 76px; height: 24px;">
                        <asp:Label ID="Label1" runat="server" Text="编 号:"></asp:Label>
                        <div style="z-index: 101; left: 63px; width: 77px; position: absolute; top: 5px;
                            height: 22px">
                            <asp:TextBox ID="txtId" runat="server" Width="74px"></asp:TextBox>
                            <div style="z-index: 101; left: 0px; width: 79px; position: absolute; top: 32px;
                                height: 18px">
                                <asp:DropDownList ID="DDLBirthday" runat="server" Width="89px" OnSelectedIndexChanged="DDLBirthday_SelectedIndexChanged">
                                </asp:DropDownList></div>
                        </div>
                    </td>
                    <td style="width: 97px; height: 24px;">
                        <asp:Label ID="Label2" runat="server" Text="用户名:"></asp:Label></td>
                </tr>
                <tr>
                    <td style="width: 76px; height: 25px;">
                        <asp:Label ID="Label3" runat="server" Text="生 日:"></asp:Label>
                    </td>
                    <td style="width: 97px; height: 25px;">
                        <asp:Label ID="Label4" runat="server" Text="排 序:"></asp:Label>
                        <div style="z-index: 102; left: 331px; width: 268px; position: absolute; top: 36px;
                            height: 22px">
                            <asp:RadioButtonList ID="rblarray" runat="server" AutoPostBack="True" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged"
                                RepeatDirection="Horizontal" Width="247px">
                                <asp:ListItem Selected="True" Value="id">编号</asp:ListItem>
                                <asp:ListItem Value="Birthday">生日</asp:ListItem>

                            </asp:RadioButtonList></div>
                    </td>
                </tr>
                <tr>
                    <td style="width: 76px; height: 21px;">
                        &nbsp;</td>
                    <td style="width: 97px; height: 21px;">
                        <asp:Button ID="btnShuxin" runat="server" Text="刷 新" OnClick="btnShuxin_Click" /></td>
                </tr>
            </table>
        </div>
        <div style="z-index: 103; left: 461px; width: 105px; position: absolute; top: 69px;
            height: 23px">
            <asp:TextBox ID="txtUserName" runat="server" Width="97px"></asp:TextBox></div>
        <div style="z-index: 104; left: 138px; width: 52px; position: absolute; top: 128px;
            height: 22px">
            <asp:Button ID="btnSelect" runat="server" Text="查 询" OnClick="btnSelect_Click" /></div>
    </div>
    </form>

后台代码:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.Data.SqlClient;
using MyMode;

public partial class InfoDetail : System.Web.UI.Page
{
    public static readonly string connectionString = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;
    SqlConnection conn = new SqlConnection(connectionString);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
              DDlBirtdayByBind();
              DGbindInfo();
        }
    }

    /// <summary>
    /// 绑定所有数据
    /// </summary>
    public void DGbindInfo()
    {
        conn.Open();
        string sql = "select [id],[UserName],[PassWord],[Age],[Email],[Thone],[Birthday],[FirstPass] from MyInfoTable";

        SqlDataAdapter sdr = new SqlDataAdapter(sql,conn);
        DataSet ds = new DataSet();
        sdr.Fill(ds, "MyInfoTable");

        DGInfo.DataKeyField = "id";
        DGInfo.DataSource = ds;
        DGInfo.DataBind();
        conn.Close();
    }

    /// <summary>
    /// 根据id来排序
    /// </summary>
    public void DGbindById()
    {
        conn.Open();
        string sql = "select [id],[UserName],[PassWord],[Age],[Email],[Thone],[Birthday],[FirstPass] from MyInfoTable order by id";
        SqlDataAdapter sdr = new SqlDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        sdr.Fill(ds, "MyInfoTable");

        DGInfo.DataKeyField = "id";
        DGInfo.DataSource = ds;
        DGInfo.DataBind();
        conn.Close();
    }
    /// <summary>
    /// 根据用户名排序
    /// </summary>
    public void DGbindByName()
    {
        conn.Open();
        string sql = "select [id],[UserName],[PassWord],[Age],[Email],[Thone],[Birthday],[FirstPass] from MyInfoTable order by UserName";
        SqlDataAdapter sdr = new SqlDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        sdr.Fill(ds, "MyInfoTable");

        DGInfo.DataKeyField = "id";
        DGInfo.DataSource = ds;
        DGInfo.DataBind();
        conn.Close();
    }
    /// <summary>
    /// 绑定DropDownList控件
    /// </summary>
    public void DDlBirtdayByBind()
    {
        conn.Open();
        string sql = "select [UserName],[PassWord],[Age],[Email],[Thone],[Birthday],[FirstPass] from MyInfoTable";
        sql += " order by Birthday";

        SqlDataAdapter sdr = new SqlDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        sdr.Fill(ds, "MyInfoTable");
    
        //DDLBirthday.DataTextField = "Birthday";
        DDLBirthday.DataValueField = "Birthday";
        DDLBirthday.DataSource = ds;
        DDLBirthday.DataBind();
        conn.Close();

    }


    protected void DGInfo_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='#f1f1f1';");
            e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff';");
        }
    }

    protected void DGInfo_DeleteCommand(object source, DataGridCommandEventArgs e)
    {

        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            e.Item.Cells[7].Attributes.Add("onclick", "return confirm('你确认删除吗?')");
        }

            int id = Convert.ToInt16(DGInfo.DataKeys[e.Item.ItemIndex]);

            string sql = "delete from MyInfoTable where id ='" + id + "'";
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
            DGbindInfo();
      
    }
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.DGInfo.CurrentPageIndex = 0;
        DGbindInfo(); 
    }
    protected void DDLBirthday_SelectedIndexChanged(object sender, EventArgs e)
    {
        DDlBirtdayByBind();       
    }
    protected void btnSelect_Click(object sender, EventArgs e)
    {  
        DGbindById(); 
    }
    protected void btnShuxin_Click(object sender, EventArgs e)
    {
        txtId.Text = "";
        txtUserName.Text = "";

        DDLBirthday.SelectedIndex = 0;
        DGbindInfo(); 
    }
}

请各位大虾指点其中的问题,谢谢

boboisboy的主页 boboisboy | 菜鸟二级 | 园豆:346
提问于:2009-12-01 17:37
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册