您的位置:首页 > 运维架构

gridview添加,删除,编辑DropDownList 根据值对应相应的选择项

2009-09-08 13:48 441 查看
前台代码
<%@ 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>
 <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>
 </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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐