<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="DepartmentView.aspx.cs" Inherits="DepartmentView" Debug="true" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div class="c_MainLeft">
<ul style="text-align:left;">
<li style="list-style-type: none;height:120px;">
<img src="images/045.png" /></li><li style="list-style-type: none;height:40px;"></li>
<li style="list-style-type: none;height:40px;">共有 <asp:Label ID="lbl_Count" runat="server"
Style="color: red"></asp:Label> 个部门</li><li style="list-style-type: none;height:40px;"></li>
</ul>
</div>
<div class="c_MainRight">
<asp:Panel ID="pDepartmentMenu3" runat="server" CssClass="c_Menu3">
<asp:LinkButton ID="lbDepartmentAdd" runat="server" OnClick="lbDepartmentAdd_Click" CssClass="c_Menu3Button">新增</asp:LinkButton>
<asp:LinkButton ID="lbDepartmentEdit" runat="server" OnClick="lbDepartmentEdit_Click" CssClass="c_Menu3Button">修改</asp:LinkButton></asp:Panel>
<asp:GridView ID="GV_Department" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" Style="width: 100%" OnRowCancelingEdit="GV_Department_RowCancelingEdit" OnRowDeleting="GV_Department_RowDeleting" OnSelectedIndexChanging="GV_Department_SelectedIndexChanging">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="id" HeaderText="部门代码">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Width="10%" />
</asp:BoundField>
<asp:BoundField DataField="name" HeaderText="部门名称">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Width="15%" />
</asp:BoundField>
<asp:BoundField DataField="create_name" HeaderText="创建用户">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Width="12%" />
</asp:BoundField>
<asp:BoundField DataField="update_name" HeaderText="更新用户">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Width="12%" />
</asp:BoundField>
<asp:BoundField DataField="create_date" HeaderText="创建日期">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Width="12%" />
</asp:BoundField>
<asp:BoundField DataField="update_date" HeaderText="更新日期">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Width="12%" />
</asp:BoundField>
<asp:TemplateField HeaderText="有效标志">
<ItemTemplate>
<asp:DropDownList ID="ddl1" runat="server" Visible="true" width = "100%">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField> <asp:CommandField HeaderText="选择" ShowSelectButton="True">
<HeaderStyle Width="8%" />
<ItemStyle HorizontalAlign="Center" />
</asp:CommandField>
<asp:TemplateField HeaderText="删除" ShowHeader="False">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Width="8%" />
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
OnClientClick='return confirm("确认要删除吗?")' Text="删除"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
</asp:Content>将有效标志位 绑定为 DropDownList ddl (红色部分) 可编译后 显示 编译错误
说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。 编译器错误消息: CS0165: 使用了未赋值的局部变量“ddl”源错误: 行 27: SignDropDownList sign = new SignDropDownList();
行 28: DataSet ds_Sign = sign.GetDataSet("");
行 29: ddl.DataSource = ds_Sign;
行 30: ddl.DataValueField = "id";
行 31: ddl.DataTextField = "name";
源文件: e:\soft\HR\DepartmentView.aspx.cs 行: 29 请问 这个问题是哪里出错了 该如何修改 谢谢
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div class="c_MainLeft">
<ul style="text-align:left;">
<li style="list-style-type: none;height:120px;">
<img src="images/045.png" /></li><li style="list-style-type: none;height:40px;"></li>
<li style="list-style-type: none;height:40px;">共有 <asp:Label ID="lbl_Count" runat="server"
Style="color: red"></asp:Label> 个部门</li><li style="list-style-type: none;height:40px;"></li>
</ul>
</div>
<div class="c_MainRight">
<asp:Panel ID="pDepartmentMenu3" runat="server" CssClass="c_Menu3">
<asp:LinkButton ID="lbDepartmentAdd" runat="server" OnClick="lbDepartmentAdd_Click" CssClass="c_Menu3Button">新增</asp:LinkButton>
<asp:LinkButton ID="lbDepartmentEdit" runat="server" OnClick="lbDepartmentEdit_Click" CssClass="c_Menu3Button">修改</asp:LinkButton></asp:Panel>
<asp:GridView ID="GV_Department" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" Style="width: 100%" OnRowCancelingEdit="GV_Department_RowCancelingEdit" OnRowDeleting="GV_Department_RowDeleting" OnSelectedIndexChanging="GV_Department_SelectedIndexChanging">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="id" HeaderText="部门代码">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Width="10%" />
</asp:BoundField>
<asp:BoundField DataField="name" HeaderText="部门名称">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Width="15%" />
</asp:BoundField>
<asp:BoundField DataField="create_name" HeaderText="创建用户">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Width="12%" />
</asp:BoundField>
<asp:BoundField DataField="update_name" HeaderText="更新用户">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Width="12%" />
</asp:BoundField>
<asp:BoundField DataField="create_date" HeaderText="创建日期">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Width="12%" />
</asp:BoundField>
<asp:BoundField DataField="update_date" HeaderText="更新日期">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Width="12%" />
</asp:BoundField>
<asp:TemplateField HeaderText="有效标志">
<ItemTemplate>
<asp:DropDownList ID="ddl1" runat="server" Visible="true" width = "100%">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField> <asp:CommandField HeaderText="选择" ShowSelectButton="True">
<HeaderStyle Width="8%" />
<ItemStyle HorizontalAlign="Center" />
</asp:CommandField>
<asp:TemplateField HeaderText="删除" ShowHeader="False">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Width="8%" />
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
OnClientClick='return confirm("确认要删除吗?")' Text="删除"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
</asp:Content>将有效标志位 绑定为 DropDownList ddl (红色部分) 可编译后 显示 编译错误
说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。 编译器错误消息: CS0165: 使用了未赋值的局部变量“ddl”源错误: 行 27: SignDropDownList sign = new SignDropDownList();
行 28: DataSet ds_Sign = sign.GetDataSet("");
行 29: ddl.DataSource = ds_Sign;
行 30: ddl.DataValueField = "id";
行 31: ddl.DataTextField = "name";
源文件: e:\soft\HR\DepartmentView.aspx.cs 行: 29 请问 这个问题是哪里出错了 该如何修改 谢谢
解决方案 »
- asp.net中 如何打开刚处理过的Excel文件 急!!!!
- 页面上拉2个radiobutton控件,点一个另外一个text控件就不能输入东西,另外点下就能输入东西了,各位高手帮下忙
- 水晶报表制作一个分类统计图表
- 一个关于取类属性的问题..
- 如何获得本地某网站的所有Cookie ?
- JS实现无间断滚动图片时图片重复,
- javascript 報這個invalidOperationException錯,請問怎麼解決?
- 一个令菜鸟不明白的问题,global.asa文件怎么成了数据库了?
- 怎么根据用户的选择来得到数据库中相应相应表相应字段的值
- 有什么方法可以一次插入数据库多条记录?
- js关闭旧窗口提示
- radionbuttonList问题,急求各位大侠帮忙看下
using System.Data;
using System.Data.SqlClient;
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 DepartmentView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DropDownList ddl;
if (!Page.IsPostBack)
{
Department department = new Department();
DataSet ds_Department = department.GetDataSet("");
//GV_Department.DataSource = ds_Department.Tables["T_Department"];
GV_Department.DataSource = ds_Department.Tables["hr.dbo.v_hr_bm"];
GV_Department.DataBind();
//lbl_Count.Text = ds_Department.Tables["T_Department"].Rows.Count.ToString();
lbl_Count.Text = ds_Department.Tables["hr.dbo.v_hr_bm"].Rows.Count.ToString();
SignDropDownList sign = new SignDropDownList();
DataSet ds_Sign = sign.GetDataSet("");
ddl.DataSource = ds_Sign;
ddl.DataValueField = "id";
ddl.DataTextField = "name";
ddl.DataBind();
}
if (Request.Cookies["Rank_Code"] != null)
{
publicFunction pFunction = new publicFunction();
String strRankCode = Request.Cookies["Rank_Code"].Value;
String strFunctionCode = "Department";
String strReturn = pFunction.GetPurview(strRankCode, strFunctionCode);
if (strReturn.Contains("fail"))
Response.Redirect("PurviewError.aspx");
}
}
protected void GV_Department_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
int Int_SelectIndex = e.NewSelectedIndex; Response.Cookies["RowIndex"].Value = Int_SelectIndex.ToString(); Department department = new Department();
DataSet ds_Department = department.GetDataSet("");
GV_Department.DataSource = ds_Department.Tables["T_Department"];
GV_Department.DataBind();
}
protected void GV_Department_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
int Int_RowIndex = -1;
GV_Department.EditIndex = Int_RowIndex; Department department = new Department();
DataSet ds_Department = department.GetDataSet("");
GV_Department.DataSource = ds_Department.Tables["T_Department"];
GV_Department.DataBind();
}
protected void GV_Department_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int Int_RowIndex = e.RowIndex; Department department = new Department(); //删除部门
department.RowIndex = Int_RowIndex;
String strReturn = department.DepartmentDelete();
if (strReturn.Contains("ok"))
{
//获取新的部门信息列表
DataSet ds_Department = new DataSet();
ds_Department = department.GetDataSet("");
GV_Department.DataSource = ds_Department.Tables["T_Department"];
GV_Department.DataBind();
}
else if (strReturn.Contains("fail"))
{
Response.Write(strReturn.Replace("fail", ""));
}
}
protected void lbDepartmentAdd_Click(object sender, EventArgs e)
{
Response.Redirect("DepartmentEdit.aspx?Command=INSERT");
}
protected void lbDepartmentEdit_Click(object sender, EventArgs e)
{
//GridView GV_Department = (GridView)this.Parent.Controls[3].Controls[1];
Response.Redirect("DepartmentEdit.aspx?Command=UPDATE&RowIndex=" + GV_Department.SelectedIndex.ToString());
}
}
检查你的 .designer.cs
DropDownList ddl= GV_Department.Rows[i].FindControl("ddl1") as DropDownList;
因为服务器不知道你那个 ddl 是什么
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
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;/// <summary>
/// Department 的摘要说明
/// </summary>
public class SignDropDownList
{
public SignDropDownList()
{
//
// TODO: 在此处添加构造函数逻辑
//
} private String Sign_id;
private String Sign_name;
private int intRowIndex; #region 属性代码
public String Field_Sign_id
{
get
{
return Sign_id;
}
set
{
Sign_id = value;
}
} public String Field_Sign_name
{
get
{
return Sign_name;
}
set
{
Sign_name = value;
}
} public int RowIndex
{
get
{
return intRowIndex;
}
set
{
intRowIndex = value;
}
}
#endregion //返回符合条件的数据集
public DataSet GetDataSet(String strQuery)
{
ConnectionStringSettingsCollection connectionStrings = ConfigurationManager.ConnectionStrings; //得到连接字符串
String strConn = connectionStrings["strConn"].ConnectionString; String strSQL = ""; strSQL = "Select id,name From hr.dbo.hr_bz";
//连接数据库
SqlConnection connMain = new SqlConnection(strConn);
connMain.Open(); DataSet ds_Sign = new DataSet();
SqlDataAdapter dr_Sign = new SqlDataAdapter(strSQL, connMain); dr_Sign.Fill(ds_Sign, "hr.dbo.hr_bz"); //生成Sing数据表的主键
ds_Sign.Tables["hr.dbo.hr_bz"].PrimaryKey = new DataColumn[] { ds_Sign.Tables["hr.dbo.hr_bz"].Columns["id"] }; return ds_Sign;
}
//返回符合条件的DataReader
public SqlDataReader GetDataReader(String strQuery)
{
ConnectionStringSettingsCollection connectionStrings = ConfigurationManager.ConnectionStrings; //得到连接字符串
String strConn = connectionStrings["strConn"].ConnectionString; String strSQL = "";
strSQL = "Select id,name From hr.dbo.hr_bz";
//连接数据库
SqlConnection connMain = new SqlConnection(strConn);
connMain.Open(); SqlCommand cmd_Department = new SqlCommand(strSQL, connMain);
SqlDataReader SDR_Department = cmd_Department.ExecuteReader(); return SDR_Department;
}
//插入新行
public String DepartmentInsert()
{
ConnectionStringSettingsCollection connectionStrings = ConfigurationManager.ConnectionStrings;
/*
//得到连接字符串
String strConn = connectionStrings["strConn"].ConnectionString; String strSQL = "INSERT INTO T_Department (";
strSQL += "Department_Code,";
strSQL += "Department_Name";
strSQL += ") VALUES (";
strSQL += "'" + Department_Code + "',";
strSQL += "'" + Department_Name + "')";
//连接数据库
SqlConnection connMain = new SqlConnection(strConn);
connMain.Open(); SqlCommand cmdDepartment = new SqlCommand(strSQL, connMain);
int intReturn = cmdDepartment.ExecuteNonQuery(); return "ok";
*/return "OK";
}
//更新记录
public String DepartmentUpdate()
{
/*
try
{
ConnectionStringSettingsCollection connectionStrings = ConfigurationManager.ConnectionStrings; //得到连接字符串
String strConn = connectionStrings["strConn"].ConnectionString; //连接数据库
SqlConnection connMain = new SqlConnection(strConn);
connMain.Open(); String strSQL = "Select * From T_Department"; DataSet ds_Department = new DataSet();
SqlDataAdapter dr_Department = new SqlDataAdapter();
SqlCommandBuilder CB_Department = new SqlCommandBuilder(dr_Department);
dr_Department.SelectCommand = new SqlCommand(strSQL, connMain);
dr_Department.UpdateCommand = CB_Department.GetUpdateCommand(true); dr_Department.Fill(ds_Department, "T_Department"); //填充T_Department信息表 //生成T_Department数据表的主键
ds_Department.Tables["T_Department"].PrimaryKey = new DataColumn[] { ds_Department.Tables["T_Department"].Columns["Department_ID"] }; DataRow DR_Department = ds_Department.Tables["T_Department"].Rows[intRowIndex]; DR_Department["Department_Code"] = Department_Code;
DR_Department["Department_Name"] = Department_Name; dr_Department.Update(ds_Department.Tables["T_Department"]); //把更新的信息发送到数据库 ds_Department.AcceptChanges(); return "ok" + Department_Name;
}
catch (Exception ex)
{
return "fail" + intRowIndex.ToString() + "/" + ex.ToString();
}
*/ return "OK";
}
//删除行
public String DepartmentDelete()
{
/*
try
{
ConnectionStringSettingsCollection connectionStrings = ConfigurationManager.ConnectionStrings; //得到连接字符串
String strConn = connectionStrings["strConn"].ConnectionString; //连接数据库
SqlConnection connMain = new SqlConnection(strConn);
connMain.Open(); String strSQL = "Select * From T_Department"; DataSet ds_Department = new DataSet();
SqlDataAdapter dr_Department = new SqlDataAdapter();
SqlCommandBuilder CB_Department = new SqlCommandBuilder(dr_Department);
dr_Department.SelectCommand = new SqlCommand(strSQL, connMain);
dr_Department.DeleteCommand = CB_Department.GetDeleteCommand(true); dr_Department.Fill(ds_Department, "T_Department"); //填充T_Department信息表 //生成T_Department数据表的主键
ds_Department.Tables["T_Department"].PrimaryKey = new DataColumn[] { ds_Department.Tables["T_Department"].Columns["Department_ID"] }; ds_Department.Tables["T_Department"].Rows[intRowIndex].Delete(); dr_Department.Update(ds_Department.Tables["T_Department"]); ds_Department.AcceptChanges(); return "ok";
}
catch (Exception ex)
{
return "fail" + intRowIndex.ToString() + "/" + ex.ToString();
}
*/return "OK";
}
}
问下 //遍历GridView 利用FindControl()先找到DropDownList
DropDownList ddl= GV_Department.Rows[i].FindControl("ddl1") as DropDownList;这个应该 放在 上面 3个文件的那个文件的 那个位置 是最恰当的 最好能帮我分析下这3个文件的作用 和 拆分原则 谢谢 ! 第一次 学 很白
dp.datasource=....