思归大哥的这种方法,datagrid的问题,如果有其它的绑定项,则显示不出来了?为什么啊?
原始的DataGrid是这样的:
<asp:DataGrid id="dg" runat="server" OnItemDataBound="dg_ItemDatabound" AutoGenerateColumns="False" Width="150px">
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<asp:DropDownlist id="ddl" runat="server" OnSelectedIndexChanged="TestSelection" AutoPostBack="true"></asp:DropDownlist>
</HeaderTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
以上能正常绑定数据,且能在页面上正常显示出来,可是我这样的话,加上如下(加的位置是没问题的,加在 </HeaderTemplate> </asp:TemplateColumn> 之间)
<ItemTemplate>
<asp:CheckBox id="Ckb_Sel" runat="server" Checked="False"></asp:CheckBox>
<%# DataBinder.Eval(Container.DataItem,"zyid") %>
<%# DataBinder.Eval(Container.DataItem,"zymc") %>
</ItemTemplate>
所有的<ItemTemplate>内的例均显示不出来,就像根本就没有以上语句似的,怎么回事啊?为什么
<ItemTemplate>
</ItemTemplate>
之间的绑定项会失效啊
问题点数:20、回复次数:11Top
1 楼parsely(林琳)回复于 2003-10-03 20:43:20 得分 0
思归大哥回复我的原帖如下:
http://expert.csdn.net/Expert/topic/2322/2322132.xml?temp=.4995539
Top
2 楼parsely(林琳)回复于 2003-10-03 20:52:47 得分 0
而且我把这个
<asp:BoundColumn Visible="False" DataField="zyid" ReadOnly="True" HeaderText="id"></asp:BoundColumn>加在
<Columns> <asp:TemplateColumn>之间后,则这个页面所有东西都显示不出来了,
而
<asp:BoundColumn Visible="False" DataField="zyid" ReadOnly="True" HeaderText="id"></asp:BoundColumn>在另外一个例子上是好使的,Top
3 楼parsely(林琳)回复于 2003-10-03 21:01:13 得分 0
以下是思归大哥给的一个能正常运行的程序,我后来加上了一点东西
<ItemTemplate>
<asp:CheckBox id="Ckb_Sel" runat="server" Checked="False"></asp:CheckBox>
</ItemTemplate>
可是为什么这段 <ItemTemplate> </ItemTemplate>之间的内容不显示呢?
<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">
string sBoundField = "";
void Page_Load(object o, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
void BindData()
{
SqlDataAdapter da = new SqlDataAdapter("select * from authors",
"server=localhost;database=pubs;uid=sa;pwd=;");
DataTable dt = new DataTable ();
da.Fill(dt);
dg.DataSource = dt.DefaultView;
dg.DataBind();
}
void dg_ItemDatabound(object source, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
DropDownList ddl = (DropDownList)e.Item.FindControl("ddl");
if (sBoundField == "")
sBoundField = ddl.SelectedItem.Value;
else
ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(sBoundField));
}
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Cells[0].Text = DataBinder.Eval(e.Item.DataItem,sBoundField).ToString();
}
}
void TestSelection(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
sBoundField = ddl.SelectedItem.Value;
BindData();
}
</script>
<html>
<head>
</head>
<body>
<form id="form1" runat="server">
<asp:DataGrid id="dg" runat="server" OnItemDataBound="dg_ItemDatabound" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<asp:CheckBox id="Ckb_All" runat="server"></asp:CheckBox>
<asp:DropDownlist id="ddl" runat="server" OnSelectedIndexChanged="TestSelection" AutoPostBack="true">
<asp:ListItem Text="First Name" Value="au_fname" />
<asp:ListItem Text="Last Name" Value="au_lname" />
</asp:DropDownList>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox id="Ckb_Sel" runat="server" Checked="False"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<asp:Button id="btn" runat="server" Text="Submit"></asp:Button>
</form>
</body>
</html>
Top
4 楼saucer(思归)回复于 2003-10-03 21:11:39 得分 10
in my code, I was doing
e.Item.Cells[0].Text = DataBinder.Eval(e.Item.DataItem,sBoundField).ToString();
which replaces whatever in that column, what you are doing in your code?
>>>则这个页面所有东西都显示不出来了
open the HTML source in IE and check what's going on, one possibility is that you have unbalanced tagsTop
5 楼parsely(林琳)回复于 2003-10-03 22:28:52 得分 0
我就是想要做一个这样的功能: http://www.rczx.com/siguidage.gif
因为有的项是固定项,如 http://www.rczx.com/siguidage.gif 这个例子里的 “职位名称”这个字段,所以我要在 datagrid 中加入一些自定义比较灵活的字段
<ItemTemplate>
<asp:CheckBox id="Ckb_Sel" runat="server" Checked="False"></asp:CheckBox>
<%# DataBinder.Eval(Container.DataItem,"职位名称") %>
</ItemTemplate>
我现在就是不知道按你的方法如何显示别的字段,没有DropDownlist触发的固定字段
Top
6 楼parsely(林琳)回复于 2003-10-03 22:31:01 得分 0
http://www.rczx.com/siguidage.gif
是http://wwww.51job.com 里查询功能所显示的结果,是php做的
Top
7 楼saucer(思归)回复于 2003-10-03 22:39:18 得分 5
remove
e.Item.Cells[0].Text = DataBinder.Eval(e.Item.DataItem,sBoundField).ToString();
but use
<ItemTemplate>
<asp:CheckBox id="Ckb_Sel" runat="server" Checked="False"></asp:CheckBox>
<%# DataBinder.Eval(Container.DataItem, sBoundField) %>
you can consider to change sBoundField to a public or protected property
Top
8 楼parsely(林琳)回复于 2003-10-03 23:02:32 得分 0
<%# DataBinder.Eval(Container.DataItem,"职位名称") %>
以上 "职位名称"是一个字段名称,
<%# DataBinder.Eval(Container.DataItem,"职位名称") %> 中“职位名称”如何换成以下方法中的 sBoundField的值呢?
------------------------------------------------------------------------------
public void dg_ItemDatabound(object source, DataGridItemEventArgs e)
{
string sBoundField = "";
string sql = " select * from test_dropdown" ;
if (e.Item.ItemType == ListItemType.Header)
{
DropDownList ddl = (DropDownList)e.Item.FindControl("ddl");
DataSet C1 = reg_data.dbbind( sql );
ddl.DataSource=C1;
ddl.DataTextField="dname";
ddl.DataValueField="dname";
ddl.DataBind();
if (sBoundField == "")
sBoundField = ddl.SelectedItem.Value;
else
ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(sBoundField));
}
Top
9 楼saucer(思归)回复于 2003-10-03 23:10:20 得分 5
1. make sBoundField a public or protected member or create a property for it,
2. remove this line
string sBoundField = "";
from dg_ItemDatabound
3. change
<%# DataBinder.Eval(Container.DataItem,"职位名称") %>
===>
<%# DataBinder.Eval(Container.DataItem,sBoundField) %>Top
10 楼parsely(林琳)回复于 2003-10-03 23:16:41 得分 0
谢了,我试试!Top
11 楼parsely(林琳)回复于 2003-10-04 18:13:03 得分 0
好了,全好了,谢谢!Top
相关问题
- 思归大哥能回答吗:嵌套的datagrid主从表中从表如何动态添加模板列?
- 急!!!DataGrid问题!(恳请孟子,思归大哥等相助)(100分,分不够可以再加)
- 今日无事,过来给思归大哥和孟子大哥上柱香!
- 思归大哥:有关命名空间的使用
- 思归大哥请进,上次未完问题
- 动态控件问题-思归大哥帮忙
- 思归大哥请进,谢谢你!(http://community.csdn.net/Expert/topic/3725/3725676.xml?temp=.1150324)
- 谁能?孟子、思归等大哥帮忙哟(winform方式)
- module 和 assembly 的区别 请思归大哥明示
- 思归大哥,高手救我!!!很急,关于URL重写!!




