惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
IT之家
IT之家
博客园_首页
博客园 - 【当耐特】
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
G
Google Developers Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
GbyAI
GbyAI
腾讯CDC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
I
InfoQ
H
Help Net Security
T
Tailwind CSS Blog
B
Blog RSS Feed
Martin Fowler
Martin Fowler
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
博客园 - 叶小钗
雷峰网
雷峰网
量子位

博客园 - flyluo

利用sqlmap进行POST注入 [转]C#创建文件的实战应用示例解析 VC下的scoket编程 【转】利用Hough变换实现直线检测的代码 [转]RGB转换为灰度图的vc代码 ASP.NET 2.0,C#----利用GridView控件导出其他文件(导出Excel,导出Word文件) 如何避免安装SQL2005的COM+错误 实现多语言网站(Asp.Net 2.0) 印度的九九乘法--超强 net客户段验证 net客户段验证 为GridView删除添加提示 再次装反IIS和.NET FRAMWORK2.0的顺序 一个简单的发布工具 汉字转拼音 c#加密解密 金额小写转大写 【转贴】C#中有关string和byte[]转换的问题 .NET开发人员十大必备下载工具
gridview添加,删除,编辑DropDownList 根据值对应相应的选择项...
flyluo · 2009-09-08 · via 博客园 - flyluo

前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ComanyBM.aspx.cs" Inherits="ComanyBM" %>

<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>

<!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>
        <link  href="Css/StyleSheet.css" type="text/css" rel="stylesheet"/>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table style="width: 90%">
            <tr>
                <td>
                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound"
                        ShowFooter="True" Width="100%" DataKeyNames="id" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
                        <Columns>
                            <asp:BoundField DataField="id" HeaderText="序号" ReadOnly="True" />
                            <asp:TemplateField HeaderText="部门名称">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("bmmc") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("bmmc") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="部门类别">
                                <EditItemTemplate>
                                    <asp:DropDownList ID="DropDownList2" runat="server">
                                    </asp:DropDownList><asp:HiddenField
                                        ID="HiddenField1" Value='<%# Bind("bmlb") %>'  runat="server" />
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:DropDownList ID="DropDownList1" runat="server">
                                    </asp:DropDownList>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("bmlb") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="部门权值">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("hb1") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="TextBox7" runat="server"></asp:TextBox>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("hb1") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="是否禁用">
                                <EditItemTemplate>
                                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Qz(Eval("hb2")) %>' />
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:CheckBox ID="CheckBox2" runat="server" />
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Qz(Eval("hb2")) %>' Enabled="false" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="描述">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("bz") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("bz") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField ShowHeader="False">
                                <EditItemTemplate>
                                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
                                        Text="更新"></asp:LinkButton>
                                    <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
                                        Text="取消"></asp:LinkButton>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    &nbsp;<asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/images/botton_gif_060.gif" OnClick="ImageButton2_Click" />
                                </FooterTemplate>
                                <ItemTemplate>
                                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
                                        Text="编辑"></asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <EmptyDataTemplate>
                            <table border="1" cellspacing="0" width="100%">
                                <tr>
                                    <td>
                                        序号</td>
                                    <td>
                                        部门名称</td>
                                    <td>
                                        部门类别</td>
                                     <td>
                                        部门权值</td>
                                    <td>
                                        是否禁用</td>
                                    <td>
                                        描述</td>
                                    <td>
                                        操作</td>
                                </tr>
                                <tr>
                                    <td>
                                        &nbsp;</td>
                                    <td>
                                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
                                    <td>
                                        <asp:DropDownList ID="DropDownList3" runat="server">
                                        </asp:DropDownList></td>
                                     <td>
                                        <asp:TextBox ID="TextBox8" runat="server"></asp:TextBox></td>
                                     <td>
                                         <asp:CheckBox ID="CheckBox3" runat="server" /></td>
                                    <td>
                                        <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox></td>
                                    <td>
                                        <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/botton_gif_060.gif"
                                            OnClick="ImageButton1_Click" /></td>
                                </tr>
                            </table>
                        </EmptyDataTemplate>
                    </asp:GridView>
                </td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>
后台代码

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;

public partial class ComanyBM : System.Web.UI.Page
{
        protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.Query();
           
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

    }
    private void Query()
    {
        JSOA.BLL.GSBMB obj = new JSOA.BLL.GSBMB();
        GridView1.DataSource = obj.GetList("");
        GridView1.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((DropDownList)e.Row.FindControl("DropDownList2") != null)
            {

                if ((e.Row.RowState & DataControlRowState.Edit) != 0)
                {
                    DropDownList dl = (DropDownList)e.Row.FindControl("DropDownList2");
                    if (dl != null)
                    {

                        JSOA.Co.DropListBind.DropBind(dl, "3");
                        //  选中 DropDownList
                        HiddenField dtm = (HiddenField)e.Row.Cells[2].FindControl("HiddenField1");
                        string dd = dtm.Value;
                        dl.SelectedIndex = dl.Items.IndexOf(dl.Items.FindByValue(dtm.Value.ToString().Trim()));
                    }
                }
                           

            }
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E3E3E3',this.style.color='#0a6cce'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff',this.style.color='#000000'");
            e.Row.Cells[0].Text = Convert.ToString(e.Row.RowIndex + 1);//产生序号
            try
            {
                e.Row.Cells[2].Text = JSOA.Co.DataConver.DicConver(((Label)e.Row.FindControl("Label2")).Text.ToString().Trim());
            }
            catch { }
           
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            if ((DropDownList)e.Row.FindControl("DropDownList1") != null)
            {
                DropDownList dl = (DropDownList)e.Row.FindControl("DropDownList1");
                JSOA.Co.DropListBind.DropBind(dl, "3");
                //  选中 DropDownList
                //dl.SelectedValue = ((HiddenField)e.Row.FindControl("Label2")).Value;
                //

            }
        }
        if (e.Row.RowType == DataControlRowType.EmptyDataRow)
        {
            if ((DropDownList)GridView1.Controls[0].Controls[0].FindControl("DropDownList3") != null)
            {
                DropDownList dl = (DropDownList)GridView1.Controls[0].Controls[0].FindControl("DropDownList3");
                JSOA.Co.DropListBind.DropBind(dl, "3");
                //  选中 DropDownList
                //dl.SelectedValue = ((HiddenField)e.Row.FindControl("Label2")).Value;
                //

            }
        }
     

    }
 
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        JSOA.BLL.GSBMB obj = new JSOA.BLL.GSBMB();
        JSOA.Model.GSBMB Model = new JSOA.Model.GSBMB();
        Model.BMMC = ((TextBox)GridView1.Controls[0].Controls[0].FindControl("TextBox2")).Text.Trim();
        Model.BMLB = ((DropDownList)GridView1.Controls[0].Controls[0].FindControl("DropDownList3")).SelectedValue;
        Model.BZ = ((TextBox)GridView1.Controls[0].Controls[0].FindControl("TextBox6")).Text.Trim();
        Model.HB1 = ((TextBox)GridView1.Controls[0].Controls[0].FindControl("TextBox8")).Text.Trim();
        if (((CheckBox)GridView1.Controls[0].Controls[0].FindControl("CheckBox3")).Checked)
        {
            Model.HB2 = "1";
        }
        else {
            Model.HB2 = "0";
        }
        Model.LRR = "当前用户id";
        Model.LRSJ = DateTime.Now;
        try {
            obj.Add(Model);
            JSOA.Common.MessageBox.Show(this, "部门信息添加成功!");
            this.Query();
        }
        catch { }
       
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        JSOA.BLL.GSBMB obj = new JSOA.BLL.GSBMB();
        JSOA.Model.GSBMB Model = new JSOA.Model.GSBMB();
        Model.BMMC = ((TextBox)GridView1.FooterRow.FindControl("TextBox4")).Text.Trim();
        Model.BMLB = ((DropDownList)GridView1.FooterRow.FindControl("DropDownList1")).SelectedValue;
        Model.BZ = ((TextBox)GridView1.FooterRow.FindControl("TextBox5")).Text.Trim();
        Model.HB1 = ((TextBox)GridView1.FooterRow.FindControl("TextBox7")).Text.Trim();
        if (((CheckBox)GridView1.FooterRow.FindControl("CheckBox2")).Checked)
        {
            Model.HB2 = "1";
        }
        else
        {
            Model.HB2 = "0";
        }
        Model.LRR = "当前用户id";
        Model.LRSJ = DateTime.Now;
        try
        {
            obj.Add(Model);
            JSOA.Common.MessageBox.Show(this, "部门信息添加成功!");
            this.Query();
        }
        catch { }
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        JSOA.BLL.GSBMB obj = new JSOA.BLL.GSBMB();
        JSOA.Model.GSBMB Model = new JSOA.Model.GSBMB();
        Model.ID = System.Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
        Model.BMMC = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text.Trim();
        Model.BMLB = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList2")).SelectedValue;
        Model.BZ = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3")).Text.Trim();
        Model.HB1 = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text.Trim();
        if (((CheckBox)GridView1.Rows[e.RowIndex].FindControl("CheckBox1")).Checked)
        {
            Model.HB2 = "1";
        }
        else {
            Model.HB2 = "0";
        }
        Model.LRR = "当前用户id";
        Model.LRSJ = DateTime.Now;
        try
        {
            obj.Update(Model);
            JSOA.Common.MessageBox.Show(this, "部门信息修改成功!");
            GridView1.EditIndex = -1;
            this.Query();
        }
        catch { }
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        this.Query();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
         this.Query();
    }
    protected bool Qz(object qx)
    {
        bool Result=false;
        if (qx.ToString().Trim() == "1")
        {
            Result = true;
        }
        return Result;
    }
}