datagrid的DropDownList模板列?
我在datagird加了DropDownList模板列,请问怎么写DropDownList选择时的事件?
要添加数据库。
DropDownList模板列和c#代码该怎么写?

解决方案 »

  1.   

    <%@ Page language="c#" Codebehind="DataGrid.aspx.cs" AutoEventWireup="false" Inherits="ReadWord.DataGrid" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>DataGrid</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <asp:DataGrid Runat="server" id="DataGrid1" ShowFooter="True">
    <Columns>
    <asp:TemplateColumn>
    <FooterTemplate>
    <asp:DropDownList Runat="server" ID="myDropDownList" AutoPostBack="True">
    <asp:ListItem Value="1">test1</asp:ListItem>
    <asp:ListItem Value="2">test2</asp:ListItem>
    </asp:DropDownList>
    </FooterTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>
    </form>
    </body>
    </HTML>
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;namespace ReadWord
    {
    /// <summary>
    /// DataGrid 的摘要说明。
    /// </summary>
    public class DataGrid : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataGrid DataGrid1;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    if(!IsPostBack)
    {
    string strCn="data source=bmonkey;initial catalog=Northwind;uid=sa;password=556677";
    string strSql="select top 1 * from customers";
    SqlConnection cn=new SqlConnection(strCn);
    SqlDataAdapter da=new SqlDataAdapter(strSql,cn);
    DataSet ds=new DataSet();
    da.Fill(ds,"Customers");
    this.DataGrid1.DataSource=ds.Tables[0].DefaultView;
    this.DataGrid1.DataBind(); }

    }    
    private void DataGrid1_ItemCreated(object sender,DataGridItemEventArgs e)
    {
    if(e.Item.ItemType == ListItemType.Footer)
    {
    System.Web.UI.WebControls.DropDownList myList;
    myList=(DropDownList)e.Item.Cells[0].Controls[0].FindControl("myDropDownList");
    myList.SelectedIndexChanged +=new EventHandler(myList_SelectedIndexChanged);
    } }
    private void myList_SelectedIndexChanged(object sender,EventArgs e)
    {
    Response.Write(((DropDownList)sender).SelectedValue.ToString());
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.Load += new System.EventHandler(this.Page_Load);
    this.DataGrid1.ItemCreated +=new DataGridItemEventHandler(DataGrid1_ItemCreated);

    }
    #endregion
    }
    }
      

  2.   

    <asp:DropDownList ID="dlt1" Runat="server" SelectedValue='<%# DataBinder.Eval( Container.DataItem,"quanxian").ToString() %>'>
    <asp:ListItem Value="0">管理员</asp:ListItem>
    <asp:ListItem Value="1">普通管理员</asp:ListItem>
    </asp:DropDownList>这里这是个简单的例子。这里我这样做是可以让你对应的DROPDOWNLIST选中的值正好是你数据对应的值,这里的这个quanxian字段里的值有0或者是1那根据你的情况,自己设置一下字段,就好了。获得值象普通控件一样就可以了
      

  3.   

    就上面的来说哈。上面的ID是dlt1
    那么取值的话就string strDDl = ((DropDownList)e.Item.FindControl("dlt1")).SelectedValue;
      

  4.   

    DataGrid的databind事件中写就好了,声明一个DropDownList的变量,就像普通的
    控件一样使用就可以勒