错误:未对象引用设置到对象的实例
一个gridview内关于部门 和角色的设置
aspx:
<asp:TemplateField HeaderText="部门">
<EditItemTemplate>
<asp:DropDownList ID="ddlDepartment" runat="server" Width="80px" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server"><%# Eval("DepartmentName") %></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="角色">
<EditItemTemplate>
<asp:DropDownList ID="ddlRole" runat="server" Width="80px" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server"><%# Eval("RoleName") %></asp:Label>
</ItemTemplate>
</asp:TemplateField>
cs:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (((DropDownList)e.Row.FindControl("ddlRole")) != null)
{
DropDownList ddlrole = (DropDownList)e.Row.FindControl("ddlRole");
// 生成 DropDownList 的值,绑定数据
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlDataAdapter da = new SqlDataAdapter("Proc_RoleList", conn);
da.Fill(ds);
if (conn.State.ToString() == "Open") conn.Close();
ddlrole.DataSource = ds.Tables[0].DefaultView;
ddlrole.DataTextField = "RoleName";
ddlrole.DataValueField = "RoleId";
ddlrole.DataBind();
}
if (((DropDownList)e.Row.FindControl("ddlDepartment")) != null)
{
DropDownList ddldepartment = (DropDownList)e.Row.FindControl("ddlDepartment");
// 生成 DropDownList 的值,绑定数据
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlDataAdapter da = new SqlDataAdapter("Proc_DepartmentList", conn);
da.Fill(ds);
if (conn.State.ToString() == "Open") conn.Close();
ddldepartment.DataSource = ds.Tables[0].DefaultView;
ddldepartment.DataTextField = "DepartmentName";
ddldepartment.DataValueField = "DepartmentId";
ddldepartment.DataBind();
}
web.con