关于GRID邦定DROPDOWN 有两张表,一张学生表:student 另一张为班级表:class我想把“student”邦定到DataGrid控件里,在班级字段里用DropDownList邦定,而且该控件选择项与该学生班级相对应,这样应该怎么解决好???????? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1.继承dropdownlist重新做一个控件,可以绑定班级,根据班级列出班级学生,将此控件放入datagrid的模板列中,与datagrid中的班级字段绑定2.datagrid的模板列中添加dropdownlist控件,在datagrid的itemdatabind事件中再填绑定dropdownlist的值 这是我上课有一个例子<form id="Form1" method="post" runat="server"> <table width="500" align="center"> <tr> <td width="80" style="HEIGHT: 18px">姓名</td> <td style="HEIGHT: 18px"> <asp:DropDownList id="cboName" runat="server" Width="144px" AutoPostBack="True"></asp:DropDownList></td> </tr> <tr> <td colspan="2"> <asp:DataGrid id="dtgGrade" runat="server" Width="328px" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="3" GridLines="Horizontal"> <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> <PagerStyle HorizontalAlign="Right" ForeColor="#4A3C8C" BackColor="#E7E7FF" Mode="NumericPages"></PagerStyle> </asp:DataGrid> </td> </tr> </table></form> class 应该为一个单独的数源DataGrid为一个数据源 .如果你想在DataGrid 里面包括class 的数据,你可以用DataSet分成两个表来做选择项是不是有主外键关连的呢? private void Page_Load(object sender, System.EventArgs e){ if(!this.IsPostBack) { SqlConnection Conn=new SqlConnection("server=T5;database=CSharpDB;Integrated Security=SSPI"); SqlDataAdapter da=new SqlDataAdapter("Select 序号,姓名 From Student",Conn); DataTable dt=new DataTable(); da.Fill(dt); cboName.DataSource=dt.DefaultView; cboName.DataValueField=dt.Columns[0].ColumnName; cboName.DataTextField=dt.Columns[1].ColumnName; cboName.DataBind(); this.Bind(cboName.SelectedValue); }}private void Bind(string sID){ SqlConnection Conn=new SqlConnection("server=T5;database=CSharpDB;Integrated Security=SSPI"); SqlDataAdapter da=new SqlDataAdapter("Select 课程名称,成绩 From Grade Where 学号='"+sID+"'",Conn); DataTable dt=new DataTable(); da.Fill(dt); if(dt.Rows.Count>0) { dtgGrade.DataSource=dt.DefaultView; dtgGrade.DataBind(); } else { dtgGrade.DataSource=null; dtgGrade.DataBind(); }}private void InitializeComponent(){ this.cboName.SelectedIndexChanged += new System.EventHandler(this.cboName_SelectedIndexChanged); this.Load += new System.EventHandler(this.Page_Load);}private void cboName_SelectedIndexChanged(object sender, System.EventArgs e){ this.Bind(cboName.SelectedValue);} 直接用javascript是能实现的那么现在要做的就是在绑定事件中得到或设置dropdownlist唯一的客户端id再在js中进行动态绑定js代码可以在绑定事件中加入 <asp:DropDownList id="cboName" runat="server" Width="144px" DataSorce= '<%#绑定式1%>' selected(index或是text value) = '<%#绑定式2%>'></asp:DropDownList>绑定式你可以在后置代码中用属性的方式给出来 看错了上面的不是你要的你只要用模板列实现班级字段,设置DropDownList为模板列的呈现项在绑定后再对模板列进行绑定就行 http://community.csdn.net/Expert/topic/3656/3656190.xml?temp=.5386469看看这个贴子应该对你有帮助在下拉控件那列的编辑模板<EditItemTemplate><asp:DropDownList id="DropDownList1" runat="server" Width="100%" DataValueField="OID" DataTextField="cName" SelectedIndex='<%# GetDeptIndex(DataBinder.Eval(Container.DataItem,"ID").ToString()) %>' DataSource="<%# GetDeptInfo() %>"></asp:DropDownList></EditItemTemplate>//代码中//数据源public DataTable GetDeptInfo(){return table;}//默认绑定索引public int GetDeptIndex(string oid){ ...... return 0;} //将班组ID传入,查询学生信息DataSource="<%# GetClassInfo(DataBinder.Eval(Container.DataItem,"classID").ToString()) %>">//代码中public DataTable GetClassInfo(){return table;} 顶~~wubike(xiaolei)呵呵我也来给个例子public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.Label Label1; public enum Level { A = 1,B = 2,C = 3,D = 4,E = 5 } public int SelectedIndex { get { return ((int)ViewState["index"] - 1); } set { ViewState["index"] = value; } } public int[] DdlSource { get { return new int[] {1,2,3,4,5}; } } public DataTable DataSource { get { return (DataTable)ViewState["dataSource"]; } set { ViewState["dataSource"] = value; } } protected System.Web.UI.WebControls.DataGrid DataGrid1; private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 if(!IsPostBack) { //Label1.Text = MsgBooks.config.sqlConnString; DataTable dt = new DataTable(); dt.Columns.Add("Url",typeof(string)); dt.Columns.Add("Level",typeof(Level)); DataRow dr = dt.NewRow(); dr["Url"] = "www.jjshang.com"; dr["Level"] = Level.E; dt.Rows.Add(dr); dr = dt.NewRow(); dr["Url"] = "www.chinaren.com"; dr["Level"] = Level.D; dt.Rows.Add(dr); dr = dt.NewRow(); dr["Url"] = "www.sina.com"; dr["Level"] = Level.A; dt.Rows.Add(dr); dr= dt.NewRow(); dr["Url"] = "mail.yeah.net"; dr["Level"] = Level.E; dt.Rows.Add(dr); this.DataSource = dt; DataGrid1.DataSource = this.DataSource; DataGrid1.DataBind(); } } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand); this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { SelectedIndex = int.Parse(((Label)e.Item.Cells[1].Controls[1]).Text); DataGrid1.EditItemIndex = e.Item.ItemIndex; DataGrid1.DataSource = this.DataSource; DataGrid1.DataBind(); } private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { DataGrid1.EditItemIndex = -1; DataGrid1.DataSource = this.DataSource; DataGrid1.DataBind(); } }页面中<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 368px" runat="server" AutoGenerateColumns="False" Width="368px" DataKeyField="Level"> <Columns> <asp:BoundColumn DataField="Url" HeaderText="地址"></asp:BoundColumn> <asp:TemplateColumn HeaderText="级别"> <ItemTemplate> <asp:Label id=lblTxt runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Level")%>'> </asp:Label> </ItemTemplate> <EditItemTemplate> <asp:DropDownList id=ddlChoose DataSource ='<%#DdlSource%>' SelectedIndex= '<%#SelectedIndex%>' runat="server"> </asp:DropDownList> </EditItemTemplate> </asp:TemplateColumn> <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑"></asp:EditCommandColumn> </Columns> </asp:DataGrid> 求生成静态页面的代码或思路!!! 关于aspx页面中新建项的一个问题 报表 repeater控件问题? 网站中dll引用的问题 点网页上的后退后删除过的字段会重新出现,然后操作会出现错误,请问如何解决? 关于Grid view里的RowCommand事件 新手请教 小弟求一个从SQL数据库里读取数据,做成滚动公告栏的方法 怎么在asp.net中修改IE状态栏的文字? 有没有这样的短信接口 服务器端ListBox,在客户端用脚本给它填加了项之后,服务器端ListBox的值怎么取? [急!!!!]如何导入名字空间啊?
2.datagrid的模板列中添加dropdownlist控件,在datagrid的itemdatabind事件中再填绑定dropdownlist的值
<table width="500" align="center">
<tr>
<td width="80" style="HEIGHT: 18px">姓名</td>
<td style="HEIGHT: 18px">
<asp:DropDownList id="cboName" runat="server" Width="144px" AutoPostBack="True"></asp:DropDownList></td>
</tr>
<tr>
<td colspan="2">
<asp:DataGrid id="dtgGrade" runat="server" Width="328px" BorderColor="#E7E7FF" BorderStyle="None"
BorderWidth="1px" BackColor="White" CellPadding="3" GridLines="Horizontal">
<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>
<PagerStyle HorizontalAlign="Right" ForeColor="#4A3C8C" BackColor="#E7E7FF" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
</td>
</tr>
</table>
</form>
DataGrid为一个数据源 .如果你想在DataGrid 里面包括class 的数据,你可以用DataSet分成两个表来做
选择项是不是有主外键关连的呢?
{
if(!this.IsPostBack)
{
SqlConnection Conn=new SqlConnection("server=T5;database=CSharpDB;Integrated Security=SSPI");
SqlDataAdapter da=new SqlDataAdapter("Select 序号,姓名 From Student",Conn);
DataTable dt=new DataTable();
da.Fill(dt);
cboName.DataSource=dt.DefaultView;
cboName.DataValueField=dt.Columns[0].ColumnName;
cboName.DataTextField=dt.Columns[1].ColumnName;
cboName.DataBind();
this.Bind(cboName.SelectedValue);
}
}private void Bind(string sID)
{
SqlConnection Conn=new SqlConnection("server=T5;database=CSharpDB;Integrated Security=SSPI");
SqlDataAdapter da=new SqlDataAdapter("Select 课程名称,成绩 From Grade Where 学号='"+sID+"'",Conn);
DataTable dt=new DataTable();
da.Fill(dt);
if(dt.Rows.Count>0)
{
dtgGrade.DataSource=dt.DefaultView;
dtgGrade.DataBind();
}
else
{
dtgGrade.DataSource=null;
dtgGrade.DataBind();
}
}
private void InitializeComponent()
{
this.cboName.SelectedIndexChanged += new System.EventHandler(this.cboName_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}private void cboName_SelectedIndexChanged(object sender, System.EventArgs e)
{
this.Bind(cboName.SelectedValue);
}
那么现在要做的就是在绑定事件中得到或设置dropdownlist唯一的客户端id
再在js中进行动态绑定
js代码可以在绑定事件中加入
绑定式你可以在后置代码中用属性的方式给出来
看看这个贴子应该对你有帮助
在下拉控件那列的编辑模板
<EditItemTemplate>
<asp:DropDownList id="DropDownList1" runat="server" Width="100%" DataValueField="OID" DataTextField="cName" SelectedIndex='<%# GetDeptIndex(DataBinder.Eval(Container.DataItem,"ID").ToString()) %>' DataSource="<%# GetDeptInfo() %>">
</asp:DropDownList>
</EditItemTemplate>//代码中
//数据源
public DataTable GetDeptInfo()
{
return table;
}//默认绑定索引
public int GetDeptIndex(string oid)
{
......
return 0;
}
DataSource="<%# GetClassInfo(DataBinder.Eval(Container.DataItem,"classID").ToString()) %>">//代码中
public DataTable GetClassInfo()
{
return table;
}
wubike(xiaolei)
呵呵
我也来给个例子
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
public enum Level
{
A = 1,B = 2,C = 3,D = 4,E = 5
}
public int SelectedIndex
{
get
{
return ((int)ViewState["index"] - 1);
}
set
{
ViewState["index"] = value;
}
}
public int[] DdlSource
{
get
{
return new int[] {1,2,3,4,5};
}
} public DataTable DataSource
{
get
{
return (DataTable)ViewState["dataSource"];
}
set
{
ViewState["dataSource"] = value;
}
}
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
//Label1.Text = MsgBooks.config.sqlConnString;
DataTable dt = new DataTable();
dt.Columns.Add("Url",typeof(string));
dt.Columns.Add("Level",typeof(Level)); DataRow dr = dt.NewRow();
dr["Url"] = "www.jjshang.com";
dr["Level"] = Level.E;
dt.Rows.Add(dr); dr = dt.NewRow();
dr["Url"] = "www.chinaren.com";
dr["Level"] = Level.D;
dt.Rows.Add(dr); dr = dt.NewRow();
dr["Url"] = "www.sina.com";
dr["Level"] = Level.A;
dt.Rows.Add(dr); dr= dt.NewRow();
dr["Url"] = "mail.yeah.net";
dr["Level"] = Level.E;
dt.Rows.Add(dr);
this.DataSource = dt; DataGrid1.DataSource = this.DataSource;
DataGrid1.DataBind(); } } #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
SelectedIndex = int.Parse(((Label)e.Item.Cells[1].Controls[1]).Text);
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataSource = this.DataSource;
DataGrid1.DataBind();
} private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
DataGrid1.DataSource = this.DataSource;
DataGrid1.DataBind();
}
}
页面中
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 368px" runat="server"
AutoGenerateColumns="False" Width="368px" DataKeyField="Level">
<Columns>
<asp:BoundColumn DataField="Url" HeaderText="地址"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="级别">
<ItemTemplate>
<asp:Label id=lblTxt runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Level")%>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id=ddlChoose DataSource ='<%#DdlSource%>' SelectedIndex= '<%#SelectedIndex%>' runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑"></asp:EditCommandColumn>
</Columns>
</asp:DataGrid>