DropDownList作为模板列出现的一个比较隐蔽的BUG,谁解决了就送分

时间:2021-07-18 19:38:00
我想用DropDownList作为一个DataGrid中的一个模板列,而且DropDownList中的Item都是从数据库中绑定的,我的代码如下:
<%@ Page language="c#" Codebehind="SignUpDetail.aspx.cs" AutoEventWireup="false" Inherits="CEMS1._0.Admin.SignUpDetail" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>SignUpDetail</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<TABLE id="Table1" style="BORDER-RIGHT: #6699ff thin solid; BORDER-TOP: #6699ff thin solid; Z-INDEX: 101; LEFT: 88px; BORDER-LEFT: #6699ff thin solid; BORDER-BOTTOM: #6699ff thin solid; POSITION: absolute; TOP: 48px"
cellSpacing="0" cellPadding="0" width="560" border="0">
<TR>
<TD>
<asp:DropDownList id="ddlName" runat="server" Width="100px" AutoPostBack="True"></asp:DropDownList></TD>
</TR>
<TR>
<TD><FONT face="宋体"></FONT><FONT face="宋体">
<asp:DataGrid id="dgdStudent" runat="server" Width="560px" BorderColor="#E7E7FF" BorderStyle="None"
BorderWidth="1px" BackColor="White" CellPadding="3" GridLines="Horizontal" AutoGenerateColumns="False">
<SelectedItemStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#738A9C"></SelectedItemStyle>
<AlternatingItemStyle BackColor="#F7F7F7"></AlternatingItemStyle>
<ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#4A3C8C"></HeaderStyle>
<FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="UserID" ReadOnly="True" HeaderText="学    号"></asp:BoundColumn>
<asp:BoundColumn DataField="Time" ReadOnly="True" HeaderText="报名时间"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="上课地点">
<ItemTemplate>
<asp:Label id=lblRoom runat="server" ForeColor="Red" Text='<%# DataBinder.Eval(Container.DataItem, "Room") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddlRoom" runat="server" Width="50px"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="确定" HeaderText="重新分配教室" CancelText="取消" EditText="更改上课地点"></asp:EditCommandColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="#4A3C8C" BackColor="#E7E7FF" Mode="NumericPages"></PagerStyle>
</asp:DataGrid></FONT></TD>
</TR>
</TABLE>
</form>
</body>
</HTML>

4 个解决方案

#1


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;

namespace CEMS1._0.Admin
{
/// <summary>
/// SignUpDetail 的摘要说明。
/// </summary>
public class SignUpDetail : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgdStudent;
protected System.Web.UI.WebControls.DropDownList ddlName;

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
BindCourse();
BindStudent();
}
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.ddlName.SelectedIndexChanged += new System.EventHandler(this.ddlName_SelectedIndexChanged);
this.dgdStudent.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgdStudent_CancelCommand);
this.dgdStudent.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgdStudent_EditCommand);
this.dgdStudent.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgdStudent_UpdateCommand);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private bool BindCourse()
{Session["CourseID"] = 1101;
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["conn.ConnectionString"]);

SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "BindCourse";

conn.Open();

try
{
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
finally 
{
conn.Close();
}

SqlDataAdapter adpt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adpt.Fill(ds);
ddlName.DataSource = ds;
ddlName.DataTextField = "Name";
ddlName.DataValueField = "CourseID";
ddlName.DataBind();

//绑定下拉框显示前一个页面点击的课程
for(int i =0;  i<ddlName.Items.Count; i++)
{
if(ddlName.Items[i].Value == Session["CourseID"].ToString())
ddlName.Items[i].Selected = true;
else
ddlName.Items[i].Selected = false;
}

if(ds.Tables[0].Rows.Count == 0)
return false;
else
return true;
}

public void BindStudent()
{
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["conn.ConnectionString"]);

//绑定学生信息
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "BindStudent";

cmd.Parameters.Add("@CourseID",SqlDbType.Int,4);
cmd.Parameters["@CourseID"].Value = Convert.ToInt32(ddlName.SelectedItem.Value);

conn.Open();

try
{
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
finally 
{
conn.Close();
}

SqlDataAdapter adpt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adpt.Fill(ds);


dgdStudent.DataSource = ds;
dgdStudent.DataBind();
}

private void dgdStudent_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgdStudent.EditItemIndex = -1;
BindStudent();
}

private void dgdStudent_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgdStudent.EditItemIndex = e.Item.ItemIndex;

DropDownList ddlRoom = new DropDownList();
ddlRoom = (DropDownList)e.Item.FindControl("ddlRoom");

SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["conn.ConnectionString"]);

SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "BindRoom";

cmd.Parameters.Add("@CourseID",SqlDbType.Int,4);
cmd.Parameters["@CourseID"].Value = Convert.ToInt32(ddlName.SelectedItem.Value);

conn.Open();

try
{
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
finally 
{
conn.Close();
}

SqlDataAdapter adpt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adpt.Fill(ds);

ddlRoom.DataSource = ds;
ddlRoom.DataTextField = "Room";
ddlRoom.DataBind();
}

private void dgdStudent_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{

}

private void ddlName_SelectedIndexChanged(object sender, System.EventArgs e)
{
BindStudent();
}
}
}


出现的错误是: "未将对象引用设置到对象的实例。 "
看哪位高手能找出其中的Bug,小弟我感激不尽,以50分相许 

#2


现在主要的问题是:点击“编辑”按钮就出现上面的错误,所以我“更新”的代码都没有写了,应该是绑定不成功。祝各位在问题的海洋中畅游愉快!一旦成功,我马上送分!

#3


自己解答算了
应该将FindControl方法用在ItemDataBound函数中,如: 

private void dgdStudent_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.EditItem)
{
DropDownList ddlRoom;
ddlRoom = (DropDownList)e.Item.FindControl("ddlRoom");

SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["conn.ConnectionString"]);

SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "BindRoom";

cmd.Parameters.Add("@CourseID",SqlDbType.Int,4);
cmd.Parameters["@CourseID"].Value = Convert.ToInt32(ddlName.SelectedItem.Value);

conn.Open();

try
{
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
finally 
{
conn.Close();
}

SqlDataAdapter adpt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adpt.Fill(ds);

ddlRoom.DataSource = ds;
ddlRoom.DataTextField = "Room";
ddlRoom.DataBind();
}
}
好像在其他位置难得成功!

#4


顶。。。。。。。。。。。。。。。。。。。

#1


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;

namespace CEMS1._0.Admin
{
/// <summary>
/// SignUpDetail 的摘要说明。
/// </summary>
public class SignUpDetail : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgdStudent;
protected System.Web.UI.WebControls.DropDownList ddlName;

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
BindCourse();
BindStudent();
}
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.ddlName.SelectedIndexChanged += new System.EventHandler(this.ddlName_SelectedIndexChanged);
this.dgdStudent.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgdStudent_CancelCommand);
this.dgdStudent.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgdStudent_EditCommand);
this.dgdStudent.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgdStudent_UpdateCommand);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private bool BindCourse()
{Session["CourseID"] = 1101;
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["conn.ConnectionString"]);

SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "BindCourse";

conn.Open();

try
{
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
finally 
{
conn.Close();
}

SqlDataAdapter adpt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adpt.Fill(ds);
ddlName.DataSource = ds;
ddlName.DataTextField = "Name";
ddlName.DataValueField = "CourseID";
ddlName.DataBind();

//绑定下拉框显示前一个页面点击的课程
for(int i =0;  i<ddlName.Items.Count; i++)
{
if(ddlName.Items[i].Value == Session["CourseID"].ToString())
ddlName.Items[i].Selected = true;
else
ddlName.Items[i].Selected = false;
}

if(ds.Tables[0].Rows.Count == 0)
return false;
else
return true;
}

public void BindStudent()
{
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["conn.ConnectionString"]);

//绑定学生信息
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "BindStudent";

cmd.Parameters.Add("@CourseID",SqlDbType.Int,4);
cmd.Parameters["@CourseID"].Value = Convert.ToInt32(ddlName.SelectedItem.Value);

conn.Open();

try
{
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
finally 
{
conn.Close();
}

SqlDataAdapter adpt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adpt.Fill(ds);


dgdStudent.DataSource = ds;
dgdStudent.DataBind();
}

private void dgdStudent_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgdStudent.EditItemIndex = -1;
BindStudent();
}

private void dgdStudent_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgdStudent.EditItemIndex = e.Item.ItemIndex;

DropDownList ddlRoom = new DropDownList();
ddlRoom = (DropDownList)e.Item.FindControl("ddlRoom");

SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["conn.ConnectionString"]);

SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "BindRoom";

cmd.Parameters.Add("@CourseID",SqlDbType.Int,4);
cmd.Parameters["@CourseID"].Value = Convert.ToInt32(ddlName.SelectedItem.Value);

conn.Open();

try
{
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
finally 
{
conn.Close();
}

SqlDataAdapter adpt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adpt.Fill(ds);

ddlRoom.DataSource = ds;
ddlRoom.DataTextField = "Room";
ddlRoom.DataBind();
}

private void dgdStudent_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{

}

private void ddlName_SelectedIndexChanged(object sender, System.EventArgs e)
{
BindStudent();
}
}
}


出现的错误是: "未将对象引用设置到对象的实例。 "
看哪位高手能找出其中的Bug,小弟我感激不尽,以50分相许 

#2


现在主要的问题是:点击“编辑”按钮就出现上面的错误,所以我“更新”的代码都没有写了,应该是绑定不成功。祝各位在问题的海洋中畅游愉快!一旦成功,我马上送分!

#3


自己解答算了
应该将FindControl方法用在ItemDataBound函数中,如: 

private void dgdStudent_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.EditItem)
{
DropDownList ddlRoom;
ddlRoom = (DropDownList)e.Item.FindControl("ddlRoom");

SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["conn.ConnectionString"]);

SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "BindRoom";

cmd.Parameters.Add("@CourseID",SqlDbType.Int,4);
cmd.Parameters["@CourseID"].Value = Convert.ToInt32(ddlName.SelectedItem.Value);

conn.Open();

try
{
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
finally 
{
conn.Close();
}

SqlDataAdapter adpt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adpt.Fill(ds);

ddlRoom.DataSource = ds;
ddlRoom.DataTextField = "Room";
ddlRoom.DataBind();
}
}
好像在其他位置难得成功!

#4


顶。。。。。。。。。。。。。。。。。。。