DropDownList通过绑定GridView里的数据得到自己的数据

来源:互联网 发布:mac win8截图快捷键 编辑:程序博客网 时间:2024/04/27 21:18

前题DropDownList控件已经绑定了数据源

我们要实现的是当GridView绑定数据的时候通过得到它里边的数据让DropDownList里的数据得以更新。

添加GridView控件,再添加DropDownList控件具体的方法我想大家都会。。。不多说,把前台代码贴出来好了!

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Admin.aspx.cs" Inherits="Member_Admin" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <style type="text/css">
    .tabs { position:relative; top:1px; left:10px; }
    .tab { border:solid 1px black; background-color:#eee; padding:2px 10px; }
    .selectedTab { background-color:white; border-bottom: solid 1px white;}
    .tabContents { border:solid 1px black; padding:10px; background-color:white; }
    </style>
    <script language="javascript">
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Menu ID="Menu1" StaticMenuItemStyle-CssClass="tab" StaticSelectedStyle-CssClass="selectedTab" CssClass="tabs" runat="server" Orientation="Horizontal" OnMenuItemClick="Menu1_MenuItemClick">
            <Items>
                <asp:MenuItem Text="会员" Value="0" Selected="true"></asp:MenuItem>
                <asp:MenuItem Text="公告" Value="1"></asp:MenuItem>
            </Items>    
        </asp:Menu>
        <div class="tabContents">
        <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
            <asp:View ID="View0" runat="server">
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="770px" OnRowDataBound="GridView1_RowDataBound" OnRowCreated="GridView1_RowCreated">
                    <Columns>
                        <asp:BoundField DataField="unitname" HeaderText="名称" />
                        <asp:TemplateField HeaderText="类型">
                            <ItemTemplate>
                                <asp:DropDownList ID="DDlType" runat="server" DataSource='<%#DDlTypeBind()%>' DataTextField="usertypename" DataValueField="usertype">
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="province" HeaderText="省份" />
                        <asp:BoundField DataField="city" HeaderText="城市" />
                        <asp:BoundField DataField="phone" HeaderText="电话" />
                        <asp:TemplateField HeaderText="状态">
                            <ItemTemplate>
                                <asp:DropDownList ID="DDlState" runat="server" DataSource='<%#DDlStateBind()%>' DataTextField="userstatename" DataValueField="userstate">
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="usertype" />
                        <asp:BoundField DataField="userstate" />
                        <asp:CommandField ButtonType="Button" HeaderText="操作" ShowEditButton="True" />
                    </Columns>
                </asp:GridView>
            </asp:View>
            <asp:View ID="View1" runat="server">
            <table>
                <tr>
                    <td>
                        <asp:Label ID="Label3" runat="server" Text="当前公告编写时间:"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td align="center">
                        <asp:Label ID="Label4" runat="server" Text="公告内容:"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox4" runat="server" Height="341px" TextMode="MultiLine" Width="617px"></asp:TextBox>
                    </td>
                </tr>
            </table>
            </asp:View>
        </asp:MultiView>
        </div>
    </div>
    </form>
</body>
</html>

后台代码:

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 Mysqlserver;

public partial class Member_Admin : System.Web.UI.Page
{
    protected SqlServerDataBase ODB = new Mysqlserver.SqlServerDataBase();
    protected void Page_Load(object sender, EventArgs e)
    {
        Bind();
    }

    protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
    {
        int index = Int32.Parse(e.Item.Value);
        MultiView1.ActiveViewIndex = index;
    }

    protected void Bind()
    {
        string sql = "select unitname,usertype,province,city,phone,userstate,usertype,userstate from user_info_t";
        DataSet GridViewDS = ODB.Select(sql, null);
        GridView1.DataSource = GridViewDS;
        GridView1.DataBind();
    }

    protected DataSet DDlTypeBind()
    {
        string sql = "select usertype,usertypename from user_type_t";
        DataSet TypeDS = ODB.Select(sql, null);
        return TypeDS;
    }

    protected DataSet DDlStateBind()
    {
        string sql = "select userstate,userstatename from user_state_t";
        DataSet StateDS = ODB.Select(sql, null);
        return StateDS;
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList DDlType = (DropDownList)e.Row.FindControl("DDlType");
            if (e.Row.Cells[6].Text != "")
            {
                DDlType.Enabled = false;
                DDlType.ClearSelection();
                DDlType.Items.FindByValue(e.Row.Cells[6].Text.ToString()).Selected = true;
            }
            //
            DropDownList DDlState = (DropDownList)e.Row.FindControl("DDlState");
            if (e.Row.Cells[7].Text != "")
            {
                DDlState.Enabled = false;
                DDlState.ClearSelection();
                DDlState.Items.FindByValue(e.Row.Cells[7].Text.ToString()).Selected = true;
            }
        }
    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[6].Visible = false;
            e.Row.Cells[7].Visible = false;
        }
    }
}

这里需要说明的是:

      当数据源字段对应的控件如果是TemplateField类型时,在RowDataBound事件里用e.Row.Cells[].Text 是得不到数据的。变通的方法是可以加隐藏列,但要记住不要在前台设置visible属性,否则一样得不到数据。应该在RowCreated事件中修改属性。为什么?原因就是当绑定数据的时候同时也在生成行,(具体的方法可以加断点加以测试)这样既可以得到数据又可以隐藏不需要显示的列了!