• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • DataGrid和DropDownList的一些配合以及使用css定制DataG

    發表于:2007-04-28來源:作者:點擊數: 標簽:DropDownList配合datagrid一些
    有的時候我們需要 (1)在編輯的時候用下拉框選擇,并且默認為 數據庫 的內容 (2)使用下拉框過濾數據 (3)使用css統一定制DataGrid 下面給出代碼: 數據結構: 表dep:depid(標識主鍵),depname(學院名字) 表stu:stuid(標識主鍵),stuname(學生名
    22.jpg
    有的時候我們需要
    (1)在編輯的時候用下拉框選擇,并且默認為數據庫的內容
    (2)使用下拉框過濾數據
    (3)使用css統一定制DataGrid
    下面給出代碼:
    數據結構:
    表dep:depid(標識主鍵),depname(學院名字)
    表stu:stuid(標識主鍵),stuname(學生名字),studepid(學院id=表dep.depid)

    前臺:
    <%@ Page language="c#" Codebehind="WebForm28.aspx.cs" AutoEventWireup="false" Inherits="csdn.WebForm28" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
     <HEAD>
      <title>WebForm28</title>
      <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
      <meta name="CODE_LANGUAGE" Content="C#">
      <meta name="vs_defaultClientScript" content="JavaScript">
      <link href="css.css" rel="stylesheet" type="text/css">
      <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5
    ">
     </HEAD>
     <body>
      <form id="Form1" method="post" runat="server">
       <asp:DropDownList id="DropDownList1" runat="server" AutoPostBack="True"></asp:DropDownList>
       <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" CellSpacing="1" BorderWidth="0px"
        CellPadding="5" CssClass="border" OnEditCommand="edit" OnCancelCommand="cancel" OnUpdateCommand="update"
        DataKeyField="stuid">
        <ItemStyle CssClass="item"></ItemStyle>
        <HeaderStyle CssClass="header"></HeaderStyle>
        <Columns>
         <asp:TemplateColumn HeaderText="姓名">
          <ItemTemplate>
           <%# DataBinder.Eval(Container.DataItem,"stuname") %>
          </ItemTemplate>
          <EditItemTemplate>
           <asp:TextBox id="name" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"stuname") %>' Width="88px">
           </asp:TextBox>
          </EditItemTemplate>
         </asp:TemplateColumn>
         <asp:TemplateColumn HeaderText="學院">
          <ItemTemplate>
           <%# DataBinder.Eval(Container.DataItem,"depname") %>
          </ItemTemplate>
          <EditItemTemplate>
           <asp:DropDownList ID="dep" Runat="server"></asp:DropDownList>
          </EditItemTemplate>
         </asp:TemplateColumn>
         <asp:EditCommandColumn ButtonType="PushButton" UpdateText="更新" CancelText="取消" EditText="編輯"></asp:EditCommandColumn>
        </Columns>
       </asp:DataGrid>
      </form>
     </body>
    </HTML>
    后臺:
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    namespace csdn
    {
     /// <summary>
     /// WebForm28 的摘要說明。
     /// </summary>
     public class WebForm28 : System.Web.UI.Page
     {
      protected System.Web.UI.WebControls.DropDownList DropDownList1;
      protected System.Web.UI.WebControls.DataGrid DataGrid1;
     
      private void Page_Load(object sender, System.EventArgs e)
      {
       // 在此處放置用戶代碼以初始化頁面
       if(!IsPostBack)
       {
        SetBind();
        SetBind2();
       }
      }

      protected void SetBind()
      {

       SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
       SqlDataAdapter da=new SqlDataAdapter("select * from stu,dep where stu.studepid=dep.depid",conn);
       DataSet ds=new DataSet();
       da.Fill(ds,"table1");
       this.DataGrid1.DataSource=ds.Tables["table1"];
       this.DataGrid1.DataBind();
       
      }

      protected void SetBind2()
      {

       SqlConnection conn2=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
       SqlDataAdapter da2=new SqlDataAdapter("select * from dep",conn2);
       DataSet ds2=new DataSet();
       da2.Fill(ds2,"table1");
       this.DropDownList1.DataSource=ds2.Tables["table1"];
       this.DropDownList1.DataTextField="depname";
       this.DropDownList1.DataValueField="depid";
       this.DropDownList1.DataBind();
       this.DropDownList1.Items.Insert(0,new ListItem("請選擇",""));
       
      }

      protected void SetBind3()
      {
       string s=this.DropDownList1.SelectedValue;
       SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
       SqlCommand comm=new SqlCommand();
       comm.Connection=conn;
       if(s!="")
       {
        comm.CommandText="select * from stu,dep where stu.studepid=dep.depid and
    depid=@depid";
        SqlParameter parm1=new SqlParameter("@depid",SqlDbType.Int);
        parm1.Value=s;
        comm.Parameters.Add(parm1);
       }
       else
        comm.CommandText="select * from stu,dep where stu.studepid=dep.depid";
       SqlDataAdapter da=new SqlDataAdapter();
       da.SelectCommand=comm;
       DataSet ds=new DataSet();
       da.Fill(ds,"table1");
       this.DataGrid1.DataSource=ds.Tables["table1"];
       this.DataGrid1.DataBind();
       
      }
      #region Web 窗體設計器生成的代碼
      override protected void OnInit(EventArgs e)
      {
       //
       // CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。
       //
       InitializeComponent();
       base.OnInit(e);
      }
      
      /// <summary>
      /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
      /// 此方法的內容。
      /// </summary>
      private void InitializeComponent()
      {   
       this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
       this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
       this.Load += new System.EventHandler(this.Page_Load);

      }
      #endregion

      private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
      {
       SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
       SqlDataAdapter da=new SqlDataAdapter("select * from dep",conn);
       DataSet ds=new DataSet();
       da.Fill(ds,"table1");
       if(e.Item.ItemType==ListItemType.EditItem)
       {
        DropDownList ddl=(DropDownList)e.Item.FindControl("dep");
        ddl.DataSource=ds.Tables["table1"];
        ddl.DataTextField="depname";
        ddl.DataValueField="depid";
        ddl.DataBind();
        ddl.Items.FindByValue(Convert.ToString(DataBinder.Eval(e.Item.DataItem,"depid"))).Selected=true;//選擇數據庫內的作為默認
       }
      }

      protected void edit(object sender,DataGridCommandEventArgs e)
      {
       this.DataGrid1.EditItemIndex=e.Item.ItemIndex;
       if(this.DropDownList1.SelectedValue=="")
        SetBind();
       else
        SetBind3();
      }

      protected void cancel(object sender,DataGridCommandEventArgs e)
      {
       this.DataGrid1.EditItemIndex=-1;
       if(this.DropDownList1.SelectedValue=="")
        SetBind();
       else
        SetBind3();
      }

      protected void update(object sender,DataGridCommandEventArgs e)
      {
       if(e.Item.ItemType==ListItemType.EditItem)//只有在編輯按下以后才能提交
       {
        SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
        SqlCommand comm=new SqlCommand("update stu set
    stuname=@name,studepid=@depid where stuid=@id",conn);
        SqlParameter parm1=new SqlParameter("@name",SqlDbType.NVarChar,50);
        parm1.Value=((TextBox)e.Item.FindControl("name")).Text;
        SqlParameter parm2=new SqlParameter("@depid",SqlDbType.Int);
        parm2.Value=((DropDownList)e.Item.FindControl("dep")).SelectedValue;
        SqlParameter parm3=new SqlParameter("@id",SqlDbType.Int);
        parm3.Value=this.DataGrid1.DataKeys[e.Item.ItemIndex];
        comm.Parameters.Add(parm1);
        comm.Parameters.Add(parm2);
        comm.Parameters.Add(parm3);
        conn.Open();
        comm.ExecuteNonQuery();
        conn.Close();
        this.DataGrid1.EditItemIndex=-1;
        if(this.DropDownList1.SelectedValue=="")
         SetBind();
        else
         SetBind3();//如果選擇過濾則使用SetBind3()
       }
      }

      private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
      {
       SetBind3();
      }
     }
    }
    css:
    .border {
     background-color: #00496C;
    }
    .header {
     font-family: "宋體", sans-serif;
     font-size: 10pt;
     font-weight: bold;
     color: #FFFFFF;
     background-color: #0080C0;
     text-align: center;
    }
    .item {
     font-family: "宋體", sans-serif;
     font-size: 9pt;
     font-weight: normal;
     color: #0080C0;
     background-color: #FFFFFF;
     text-align: center;
    }

    代碼比較簡單,下面簡單說明一下:
    (1)SetBind()是基本的綁定;SetBind2()是綁定外面的那個DropDownList;SetBind3()是在下拉框選擇了以后過濾后的DataGrid的綁定
    (2)這里使用Css來實現表格邊框是利用CellSpacing,所以這個數值就是邊框的寬度,在表格邊框的css中使用background-color來描述邊框的顏色。

    轉:http://lovecherry.cnblogs.com/archive/2005/12/13/125525.html#295905

    原文轉自:http://www.kjueaiud.com

    評論列表(網友評論僅供網友表達個人看法,并不表明本站同意其觀點或證實其描述)
    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>