• 全部
  • .NET Framework
  • ASP
  • Web Services
  • .NET互联网桌面应用
  • VB
  • 图表区
  • 分析与设计
  • 组件/控件开发
  • AppLauncher
  • 问答

请大家帮我看看datagrid中的dropdownlist为什么总是绑不了数据?

hit75 2004-11-04 01:07:41
aspx代码:
<%@ Page language="c#" Codebehind="branch_set.aspx.cs" AutoEventWireup="false" Inherits="judge.branch_set" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>branch_set</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="dg1" style="Z-INDEX: 100; LEFT: 16px; POSITION: absolute; TOP: 72px" runat="server"
Width="480px" AutoGenerateColumns="False">
<Columns>

<asp:BoundColumn DataField="branch_id" HeaderText="部门号" ReadOnly="True">
<ItemStyle Width="120px"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="部门名称">
<ItemTemplate>
<%# DataBinder.Eval(Container,"DataItem.branch_nm")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtState" Runat =server Width="30" Text='<%# DataBinder.Eval(Container,"DataItem.branch_nm")%>' >
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="前置机ip">
<ItemTemplate>
<%# DataBinder.Eval(Container,"DataItem.ip_addr")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Textbox1" Runat =server Width="30" Text='<%# DataBinder.Eval(Container,"DataItem.ip_addr")%>' >
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="前置机port">
<ItemTemplate>
<%# DataBinder.Eval(Container,"DataItem.port")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Textbox2" Runat =server Width="30" Text='<%# DataBinder.Eval(Container,"DataItem.port")%>' >
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="标志">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"flag")%>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddl1" runat="server" SelectedIndex ='<%# DataBinder.Eval(Container, "DataItem.ddl1") %>'></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton ID="cmdEdit" Runat="server" CommandName="Edit" text="修改"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="cmdCancel" Runat="server" CommandName="Cancel" text="取消"></asp:LinkButton>
<asp:LinkButton id="cmdUpdate" Runat="server" CommandName="Update" text="更新"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton id="cmdDel" Runat="server" text="删除" CommandName="Delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid><asp:label id="Label1" style="Z-INDEX: 109; LEFT: 200px; POSITION: absolute; TOP: 16px" runat="server"
Width="128px" Font-Bold="True" ForeColor="Blue">部门管理</asp:label>
<HR style="Z-INDEX: 107; LEFT: 8px; POSITION: absolute; TOP: 48px; HEIGHT: 1px" width="550"
SIZE="1">
</form>
</body>
</HTML>
...全文
106 点赞 收藏 13
写回复
13 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
hit75 2004-11-04
这种方法我试过,不行呀
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:


行 127:
行 128:
行 129: myDropDownList.DataSource = ds;//.Tables["tFlagDef"].DefaultView;
行 130: myDropDownList.DataTextField = "flag_desc";
行 131: myDropDownList.DataValueField = "flag";

回复
saucer 2004-11-04
try

DropDownList myDropDownList =(DropDownList)dg1.Items[dg1.EditItemIndex].FindControl("ddl1");

===>

DropDownList myDropDownList =(DropDownList)e.Item.FindControl("ddl1");
回复
hit75 2004-11-04
确实是呀,你看aspx代码就可以看到
回复
guxiangdefeng 2004-11-04
你的DropDownList是叫"ddl1"吗,
不是的话是找不到的.
回复
hit75 2004-11-04
错误信息为:
索引超出范围。必须为非负值并小于集合大小。参数名: index
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。参数名: index

源错误:


行 115: {
行 116:
行 117: DropDownList myDropDownList =(DropDownList)dg1.Items[dg1.EditItemIndex].FindControl("ddl1");
行 118:
行 119: SqlConnection db_conn;


源文件: f:\documents and settings\testttt\webapplication1\branch_set.aspx.cs 行: 117

堆栈跟踪:
回复
hit75 2004-11-04
思归,我按照你的方法改了,还是不行,代码如下
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebApplication1
{
/// <summary>
/// branch_set 的摘要说明。
/// </summary>
public class branch_set : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dg1;
protected System.Web.UI.WebControls.Label Label1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
SqlConnection db_conn;
SqlDataAdapter sda;
DataSet ds;
string sqlstr;

if (IsPostBack )
{

}
else
{
db_conn = new SqlConnection(Application["JudgeDb_conn"].ToString());
sqlstr = " select branch_id,branch_nm,ip_addr,port,flag from tBranch where flag='1' ";
sda=new SqlDataAdapter(sqlstr,db_conn);
ds= new DataSet();
sda.Fill(ds,"tbranch");

dg1.DataSource=ds;

dg1.DataBind();



//sortfield="";


}
}

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

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dg1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg1_EditCommand);
this.dg1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dg1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

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

SqlConnection db_conn;

db_conn = new SqlConnection(Application["JudgeDb_conn"].ToString());

if (this.dg1.EditItemIndex == e.Item.ItemIndex)
{


dg1.EditItemIndex = e.Item.ItemIndex;


DropDownList dropdown =(DropDownList)dg1.Items[dg1.EditItemIndex].FindControl("ddl1");


string CmdSql = "select flag,flag_desc from tFlagDef";
SqlDataAdapter sda=new SqlDataAdapter(CmdSql,db_conn);
DataSet ds = new DataSet();
sda.Fill(ds, "tFlagDef");
//DropDownList绑定数据库

dropdown.DataSource = ds;//.Tables["tFlagDef"].DefaultView;
dropdown.DataTextField = "flag_desc";
dropdown.DataValueField = "flag";
dropdown.DataBind();

}
//BindGrid();//此方法为你DataGrid的数据绑定方法


}

private void dg1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (this.dg1.EditItemIndex == e.Item.ItemIndex)
{

DropDownList myDropDownList =(DropDownList)dg1.Items[dg1.EditItemIndex].FindControl("ddl1");

SqlConnection db_conn;

db_conn = new SqlConnection(Application["JudgeDb_conn"].ToString());

string CmdSql = "select flag,flag_desc from tFlagDef";
SqlDataAdapter sda=new SqlDataAdapter(CmdSql,db_conn);
DataSet ds = new DataSet();
sda.Fill(ds, "tFlagDef");


myDropDownList.DataSource = ds;//.Tables["tFlagDef"].DefaultView;
myDropDownList.DataTextField = "flag_desc";
myDropDownList.DataValueField = "flag";
myDropDownList.DataBind();
}

}
}
}
回复
saucer 2004-11-04
did you do inside ItemDataBound??

check for if

(this.dg1.EditItemIndex == e.Item.ItemIndex)


or

if (ListItemType.EditItem== e.Item.ItemType)
回复
hit75 2004-11-04
肯定就是没找到dll1,问题就是不知道为什么找不到
回复
saucer 2004-11-04
you shouldn't do the binding in Edit_Command


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

BindGrid();


}



private void dg1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{

if (this.dg1.EditItemIndex == e.Item.ItemIndex)
{

DropDownList myDropDownList =(DropDownList)dg1.Items[dg1.EditItemIndex].FindControl("ddl1");

SqlConnection db_conn;

db_conn = new SqlConnection(Application["JudgeDb_conn"].ToString());

string CmdSql = "select flag,flag_nm from tFlagDef";
SqlDataAdapter sda=new SqlDataAdapter(CmdSql,db_conn);
DataSet ds = new DataSet();
sda.Fill(ds, "tFlagDef");


myDropDownList.DataSource = ds;//.Tables["tFlagDef"].DefaultView;
myDropDownList.DataTextField = "flag_nm";
myDropDownList.DataValueField = "flag";
myDropDownList.DataBind();
}
}
回复
wangrenda 2004-11-04
没找到控件
回复
wangrenda 2004-11-04
if(myDropDownList!=null)
{
//bind
}
else
{
Response.write("没有找到DLL,为什么?");
}
回复
hit75 2004-11-04
错误信息为
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:


行 102: //DropDownList绑定数据库
行 103:
行 104: dropdown.DataSource = ds;//.Tables["tFlagDef"].DefaultView;
行 105: dropdown.DataTextField = "flag_nm";
行 106: dropdown.DataValueField = "flag";


源文件: c:\inetpub\wwwroot\judge\branch_set.aspx.cs 行: 104
回复
hit75 2004-11-04
cs代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace judge
{
/// <summary>
/// branch_set 的摘要说明。
/// </summary>
public class branch_set : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dg1;
protected System.Web.UI.WebControls.Label Label1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
SqlConnection db_conn;
SqlDataAdapter sda;
DataSet ds;
string sqlstr;

if (IsPostBack )
{

}
else
{
db_conn = new SqlConnection("user id=score;password=score;database=judge;server=hit;Connect Timeout =6000 ");
sqlstr = " select branch_id,branch_nm,ip_addr,port,flag from tBranch where flag='1' ";
sda=new SqlDataAdapter(sqlstr,db_conn);
ds= new DataSet();
sda.Fill(ds,"tbranch");

dg1.DataSource=ds;

dg1.DataBind();



//sortfield="";


}
}

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

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dg1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg1_EditCommand);
this.dg1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dg1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void dg1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.dg1.EditItemIndex = e.Item.ItemIndex;
for(int i=0;i<e.Item.Cells.Count-1;i++)

{
e.Item.Cells[i].Attributes.Add("Width", "50px");

}
SqlConnection db_conn;

db_conn = new SqlConnection(Application["JudgeDb_conn"].ToString());

if (e.Item.ItemIndex >= 0)
{
dg1.EditItemIndex = e.Item.ItemIndex;


DropDownList dropdown =(DropDownList)dg1.Items[dg1.EditItemIndex].FindControl("ddl1");


string CmdSql = "select flag,flag_nm from tFlagDef";
SqlDataAdapter sda=new SqlDataAdapter(CmdSql,db_conn);
DataSet ds = new DataSet();
sda.Fill(ds, "tFlagDef");
//DropDownList绑定数据库

dropdown.DataSource = ds;//.Tables["tFlagDef"].DefaultView;
dropdown.DataTextField = "flag_nm";
dropdown.DataValueField = "flag";
dropdown.DataBind();

}
//BindGrid();//此方法为你DataGrid的数据绑定方法


}

private void dg1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
return;
SqlConnection db_conn;

db_conn = new SqlConnection(Application["JudgeDb_conn"].ToString());
if (e.Item.ItemType ==ListItemType.Item || e.Item.ItemType ==ListItemType.AlternatingItem )

//if (e.Item.ItemIndex >= 0)
{
dg1.EditItemIndex=e.Item.ItemIndex;
DropDownList myDropDownList =(DropDownList)dg1.Items[dg1.EditItemIndex].FindControl("ddl1");


Label1.Text=myDropDownList.ID;
return;
string CmdSql = "select flag,flag_nm from tFlagDef";
SqlDataAdapter sda=new SqlDataAdapter(CmdSql,db_conn);
DataSet ds = new DataSet();
sda.Fill(ds, "tFlagDef");
//DropDownList绑定数据库

myDropDownList.DataSource = ds;//.Tables["tFlagDef"].DefaultView;
myDropDownList.DataTextField = "flag_nm";
myDropDownList.DataValueField = "flag";
myDropDownList.DataBind();

}
}
}
}
回复
相关推荐
发帖
.NET技术社区
创建于2007-09-28

5.8w+

社区成员

.NET技术交流专区
申请成为版主
帖子事件
创建了帖子
2004-11-04 01:07
社区公告
暂无公告