请问在数据绑定事件里如何获取当前行? e只有Equals、GetHashCode、GetType、ToString四个方法,没有属性。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我想LZ要用的是RowDataBound事件而不是DataBinding protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { Label lb1 = e.Row.FindControl("Label1") as Label; ..... }或者用下面方法,在捆绑每行数据是动态执行方法。<asp:TemplateField HeaderText="与户主关系" SortExpression="FamilyRelation_ID"><ItemTemplate><asp:Label ID="lb1" runat="server" Text='<%#functionName(DataBinder.Eval(Container,"DataItem.DataName").ToString()) %>'></asp:Label></ItemTemplate></asp:TemplateField>在前台写方法,方法名为functionName(string str) RowDataBound事件里的行也只是GridView的行,而不是数据源的行吧? 那你可以在业务逻辑层对数据层返回的数据进行处理,然后再返回页面表示层,如果不是三层的,可以先对数据的每一行进行处理,然后再帮定GridView. 我已经说了,“如果单独循环处理数据源,可以在Page_load中用foreach遍历数据源”这句话,我现在的困惑是数据源要和GridView同步循环,也就是说,每循环一行数据,就判断这行数据的值,来判断是否在对应的GridView的行的模板列中增加或删除子控件。楼上的还是没看明白的表达的意思。 前台页面部分:<asp:Repeater id="rptCategories" runat="server" OnItemDataBound="rptCategories_ItemDataBound"> <HeaderTemplate> <table width="100%" border="0" cellspacing="0" cellpadding="0"> </HeaderTemplate> <ItemTemplate> <!--分类名称--> <tr><th><%# DataBinder.Eval(Container.DataItem, "TypeName") %></th></tr> <!--分类下的产品--> <asp:Repeater id="rptProduct" runat="server"> <ItemTemplate> <tr><td><a href=ProductInfo.aspx?Id=<%# DataBinder.Eval(Container.DataItem, "ID") %>><%# DataBinder.Eval(Container.DataItem, "ProductName") %></a></td></tr> </ItemTemplate> </asp:Repeater> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> 后台代码部分(部分代码): //在绑定分类品名时,绑定分类下的产品 private void rptCategories_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e) { BLL.Products products =new BLL.Products(); if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { Repeater rptProduct = (Repeater) e.Item.FindControl("rptProduct"); //找到分类Repeater关联的数据项 DataRowView rowv = (DataRowView)e.Item.DataItem; //提取分类ID int CategorieId = Convert.ToInt32(rowv["ID"]); //根据分类ID查询该分类下的产品,并绑定产品Repeater rptProduct.DataSource = products.GetProductsByCategorieId(CategorieId); rptProduct.DataBind(); } } 注意:其中 第2层的 OnItemDataBound="rptCategories_ItemDataBound" 是重要部分,因为里层的repeater web里是认识不到的,只有在这指明了事件处理方法,才能触发后台程序里的方法。类似的也可以用这种方法多层嵌套,并能处理里面的事件。第二点在 aspx 指明事件的在后台程序,需要protected 来限定访问限制,如用private 就会出现限制访问的错误提示。前几天我在外面一个业务里曾经遇到这个问题,并且吃了很大的亏,这方面的资料其实在网上是很多的,随便上baidu都可以找到,关键是理解它的思路就行了,这其实也不难。但在处理这个问题时,我的Repeater控件在别的地方出问题了,怎么调试,结果也不对,后来仔细一看 ,原来我的Repeater控件的首尾连在一起了,即变成:<asp:Repeater id="rptCategories" runat="server" OnItemDataBound="rptCategories_ItemDataBound"> </asp:Repeater> 像这种情况,在调试时浏览器是不报错的,但嵌套的结果就不出来,在代码上千行的HTML源程序中,查起来还是很头疼呀。所以我希望大家以后在处理问题时,最好从多个角度入手,全盘考虑,有时问题往往就出在你最熟悉的内容上。 该事件通知服务器控件执行已为其编写的任何数据绑定逻辑。msdn 如果你想在DataGrid里动态添加什么控件,就需要在ItemCreated事件中,而不是在ItemDataBound事件中。如果想在DataGrid中动态为模板列里面的控件绑定值,则需要在 ItemDataBound事件中进行绑定 private void MainDataList_ItemCreated(object sender, System.Web.UI.WebControls.DataListItemEventArgs e) { if ( (e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem) ) {//如果数据源为DataTable或者DataSet则需要使用如下语句转换数据 if(((DataRowView)e.Item.DataItem).Row.ItemArray[0].ToString()=="合计") { // Label1.Text=((DataRowView)e.Item.DataItem).Row.ItemArray[0].ToString()+((DataRowView)e.Item.DataItem).Row.ItemArray[0].ToString(); } } }如果控件数据源为DataReader则转换数据需要使用如下语句转换数据System.Data.Common.DbDataRecord rr = (System.Data.Common.DbDataRecord)e.Item.DataItem; string m = rr.GetString(0);int m = rr.GetInt32(1); 请问你这个DataRowView是什么意思?是控件的属性还是数据源的属性?我查了MSDN,里面说的很含糊,我也没看明白。在GridView中,DataItem返回的是object类型,不知道应该转换成什么类型。(根据你的代码)而且这个DataItem里面也没有索引器。DataRowView rowv = (DataRowView)e.Item.DataItem; //提取分类ID int CategorieId = Convert.ToInt32(rowv["ID"]);不知道这段代码如何转换为GridView的形式? gridview分页问题 asp.net连接oracle遇到的怪问题 一个久未解决的问题 请问如何去掉允许分页的DATAGRID下面的<> 有没有类似ASP的response.end语句??? 请问VS2008上加载了AJAX后编程出问题了。。。 我在父模态窗口里再打开了子模态窗口,但怎么我关闭子窗口会顺便把父窗口也关闭了的? Label文本换行问题 WCF启用ASP.Net 兼容模式发布后使用域名访问报错 Page.MapPath和Server.MapPath ? .NET中怎么定义动态数组 vs2005的AJAX组件新手问题求助
{
Label lb1 = e.Row.FindControl("Label1") as Label;
.....
}
或者用下面方法,在捆绑每行数据是动态执行方法。
<asp:TemplateField HeaderText="与户主关系" SortExpression="FamilyRelation_ID">
<ItemTemplate>
<asp:Label ID="lb1" runat="server" Text='<%#functionName(DataBinder.Eval(Container,"DataItem.DataName").ToString()) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
在前台写方法,方法名为functionName(string str)
<asp:Repeater id="rptCategories" runat="server" OnItemDataBound="rptCategories_ItemDataBound">
<HeaderTemplate>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
</HeaderTemplate>
<ItemTemplate>
<!--分类名称-->
<tr><th><%# DataBinder.Eval(Container.DataItem, "TypeName") %></th></tr>
<!--分类下的产品-->
<asp:Repeater id="rptProduct" runat="server">
<ItemTemplate>
<tr><td><a href=ProductInfo.aspx?Id=<%# DataBinder.Eval(Container.DataItem, "ID") %>><%# DataBinder.Eval(Container.DataItem, "ProductName") %></a></td></tr>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater> 后台代码部分(部分代码):
//在绑定分类品名时,绑定分类下的产品
private void rptCategories_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
BLL.Products products =new BLL.Products();
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater rptProduct = (Repeater) e.Item.FindControl("rptProduct");
//找到分类Repeater关联的数据项
DataRowView rowv = (DataRowView)e.Item.DataItem;
//提取分类ID
int CategorieId = Convert.ToInt32(rowv["ID"]);
//根据分类ID查询该分类下的产品,并绑定产品Repeater
rptProduct.DataSource = products.GetProductsByCategorieId(CategorieId);
rptProduct.DataBind();
}
}
注意:其中 第2层的 OnItemDataBound="rptCategories_ItemDataBound" 是重要部分,因为里层的repeater web里是认识不到的,只有在这指明了事件处理方法,才能触发后台程序里的方法。类似的也可以用这种方法多层嵌套,并能处理里面的事件。第二点在 aspx 指明事件的在后台程序,需要protected 来限定访问限制,如用private 就会出现限制访问的错误提示。前几天我在外面一个业务里曾经遇到这个问题,并且吃了很大的亏,这方面的资料其实在网上是很多的,随便上baidu都可以找到,关键是理解它的思路就行了,这其实也不难。但在处理这个问题时,我的Repeater控件在别的地方出问题了,怎么调试,结果也不对,后来仔细一看 ,原来我的Repeater控件的首尾连在一起了,即变成:
<asp:Repeater id="rptCategories" runat="server" OnItemDataBound="rptCategories_ItemDataBound">
</asp:Repeater>
像这种情况,在调试时浏览器是不报错的,但嵌套的结果就不出来,在代码上千行的HTML源程序中,查起来还是很头疼呀。所以我希望大家以后在处理问题时,最好从多个角度入手,全盘考虑,有时问题往往就出在你最熟悉的内容上。
private void MainDataList_ItemCreated(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
if ( (e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem) )
{
//如果数据源为DataTable或者DataSet则需要使用如下语句转换数据
if(((DataRowView)e.Item.DataItem).Row.ItemArray[0].ToString()=="合计")
{
// Label1.Text=((DataRowView)e.Item.DataItem).Row.ItemArray[0].ToString()+((DataRowView)e.Item.DataItem).Row.ItemArray[0].ToString();
}
}
}如果控件数据源为DataReader则转换数据需要使用如下语句转换数据
System.Data.Common.DbDataRecord rr = (System.Data.Common.DbDataRecord)e.Item.DataItem;
string m = rr.GetString(0);
int m = rr.GetInt32(1);
在GridView中,DataItem返回的是object类型,不知道应该转换成什么类型。(根据你的代码)
而且这个DataItem里面也没有索引器。DataRowView rowv = (DataRowView)e.Item.DataItem;
//提取分类ID
int CategorieId = Convert.ToInt32(rowv["ID"]);不知道这段代码如何转换为GridView的形式?