日期:2014-05-19  浏览次数:20433 次

|M| 第二贴:更以前别的人代理,我在aspx改,有个地方让高人来指点一下 谢谢
http://community.csdn.net/Expert/topic/5485/5485172.xml?temp=.8529169

如有一个DataGrid绑定了以下表和字段
Product
id   product   from
1     apple       china
2     banana     china
3     orange     USA

然后现在又多了一个表
Price  
id     ProductID   Price   SalePrice   begindate     enddate
1       1                   10         15                 2007-04-20   2007-05-20

现在要求把Price这个表的信息也绑定到product里面
本来最简单的办法是product   price关联查询绑定
但现在要求是不更改原来的aspx.cs文件
所以我就在前台
<%@   Import   Namespace= 'com.Price '   %>     //导入我的price DLL
然后给DataGrid添加四列,分别对应以下
Price.GetDetail(Eval( "ProductID ")).Price
Price.GetDetail(Eval( "ProductID ")).SalePrice
Price.GetDetail(Eval( "ProductID ")).BeginDate
Price.GetDetail(Eval( "ProductID ")).EndDate
这样可以做到,但是这里要再对数据库进行四次查询
但查询出来的记录其实是一样的
所以我想问我查询出来的记录如何赋值给DataGrid里面行的N个控件的值

相当于
<datagrid>
<行>
<%#Eval( "ProductID ")%>
<asp:Lable   id=Price   Text= ' '   runat= 'server '/>
<asp:Lable   id=SalePrice   Text= ' '   runat= 'server '/>
<asp:Lable   id=BeginDate   Text= ' '   runat= 'server '/>
<asp:Lable   id=EndDate   Text= ' '   runat= 'server '/>
</行>
</datagrid>
怎么根据ProductID   给下面的三个控件的Text赋值
我是这样写的
<%#Eval( "ProductID ")%>
<asp:Lable   id=Price   Text= ' <#%   Price.GetDetail(Eval( "ProductID ")).Price   %> '   runat= 'server '/>
<asp:Lable   id=SalePrice   Text= ' <#%   Price.GetDetail(Eval( "ProductID ")).SalePrice   %> '   runat= 'server '/>
<asp:Lable   id=BeginDate   Text= ' <#%   Price.GetDetail(Eval( "ProductID ")).BeginDate   %> '   runat= 'server '/>
<asp:Lable   id=EndDate   Text= ' <#%   Price.GetDetail(Eval( "ProductID ")).EndDate   %> '   runat= 'server '/>
这样可以,但同样的一条记录如果这样写的话就要查询四次
感觉有点多余的感觉


------解决方案--------------------
Price.GetDetail函数输出为一个 html 字符串, 这个字符串就包含这四个 Label, 然后就可以一次显示出来了
------解决方案--------------------
你查询一次,做个public变量存起来,然后分别赋值
------解决方案--------------------
public string Price.GetDetail()
{
string str= " "; //你要显示的四个 Labe 的 html 形式

return str;
}
------解决方案--------------------

------解决方案--------------------
放个REPEATER进去,把product对象绑到repeater来,这样就只用读一次了
------解决方案--------------------
<script language= "C# " runat=server>
public object objPriceInfo = null;
</script>
放到你的grid前面
Price.GetDetail(Eval( "ProductID ")) 这个函数你应该是返回的Price对象.

<%# objPriceInfo = Price.GetDetail(Eval( "ProductID ")) %>