当前位置: 代码迷 >> ASP.NET >> 请问:关于gridview在页面回发后变形
  详细解决方案

请问:关于gridview在页面回发后变形

热度:5798   发布时间:2013-02-25 00:00:00.0
请教:关于gridview在页面回发后变形
前台界面如下:
C# code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication_GridView._Default" %><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">    <title>GridView变形</title></head><body>    <form id="form1" runat="server">    <div>        <table>            <tr>                <td>用户名:<asp:TextBox ID="TextBoxUserName" runat="server"></asp:TextBox>                    <asp:Button ID="ButtonSelect" runat="server"  Text="查询" OnClick="ButtonSelect_Click"/></td>            </tr>        </table>        <table>            <tr>                <td>用户列表:</td>            </tr>            <tr>                <td>                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="UserID">                        <Columns>                            <asp:TemplateField HeaderText="全选">                                <HeaderTemplate>                                    <asp:CheckBox ID="CheckBoxSelectALL" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBoxSelectALL_CheckedChanged" />                                </HeaderTemplate>                                <ItemTemplate>                                    <asp:CheckBox ID="CheckBoxSelect" runat="server" />                                </ItemTemplate>                                <ItemStyle Width="100px" />                            </asp:TemplateField>                            <asp:TemplateField HeaderText="序号">                                <ItemStyle Width="100px" />                            </asp:TemplateField>                            <asp:BoundField DataField="UserName" HeaderText="用户名" >                                <ItemStyle Width="100px" />                            </asp:BoundField>                                                        <asp:BoundField DataField="UserAge" HeaderText="年龄">                                <ItemStyle Width="100px" />                            </asp:BoundField>                            <asp:TemplateField HeaderText="操作">                                <ItemStyle Width="100px" />                            </asp:TemplateField>                        </Columns>                    </asp:GridView>                </td>            </tr>        </table>        <table>            <tr>                <td><asp:Button ID="ButtonDeleteSelected" runat="server" Text="删除所选" /></td>            </tr>        </table>    </div>    </form></body></html>


后台代码如下:
C# code
    public partial class _Default : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            if (!IsPostBack)            {                GridViewBind("select * from Users");            }        }        //GridView绑定        private void GridViewBind(string sqlString)        {            //            string sqlConnString = @"server=WWW-F30969C25B8\LOCALHOST;database=test;uid=sa;pwd=momaer";            SqlConnection conn = new SqlConnection(sqlConnString);            SqlCommand cmd = new SqlCommand(sqlString, conn);            SqlDataAdapter adapter = new SqlDataAdapter(cmd);            DataSet myDataSet = new DataSet();            adapter.Fill(myDataSet);            GridView1.DataSource = myDataSet.Tables[0];            GridView1.DataBind();            if (myDataSet.Tables[0].Rows.Count <= 0)            {                //GridView2.Enabled = true;                DataRow row = myDataSet.Tables[0].NewRow();                myDataSet.Tables[0].Rows.Add(row);                GridView1.DataSource = myDataSet.Tables[0];                GridView1.DataBind();                int count = GridView1.HeaderRow.Cells.Count;                GridView1.Rows[0].Cells.Clear();                GridView1.Rows[0].Cells.Add(new TableCell());                GridView1.Rows[0].Cells[0].Text = "没有找到符合条件的数据!";                GridView1.Rows[0].Cells[0].ColumnSpan = count;                                return;            }                }        //全选        protected void CheckBoxSelectALL_CheckedChanged(object sender, EventArgs e)        {            //全选            if ((GridView1.Rows.Count <= 1) && (GridView1.Rows[0].Cells.Count <= 1))            {                ((CheckBox)sender).Checked = false;                return;            }            bool isSelectAll = ((CheckBox)sender).Checked;            CheckBox temp;            foreach (GridViewRow row in GridView1.Rows)            {                temp = (CheckBox)row.Cells[0].FindControl("CheckBoxSelect");                temp.Checked = isSelectAll;            }        }        //查询        protected void ButtonSelect_Click(object sender, EventArgs e)        {            string userName = TextBoxUserName.Text.Trim();            string sqlString = "select * from Users where UserName like '%"+userName+"%'";            GridViewBind(sqlString);        }    }