以编程方式查看和处理objectdatasource的选择数据 [英] view and process objectdatasource's select data programmatically
问题描述
我想根据第二个数据源中的报纸ID预选复选框.它的select方法返回的数据是可枚举的.并且复选框的值为字符串.
如何比较属于复选框的值和来自第二个数据源的选择数据?
我尝试以下解决方案,但未检查任何cbs:
受保护的 无效 CheckBoxList1_DataBound(对象发件人,EventArgs e)
{
IEnumerable结果= favoriteDataSource.Select();
foreach ( var 项目 in 结果中的项)
{
foreach (ListItem cb in CheckBoxList1.Items)
{
如果(item.ToString()== cb.Value)
{
cb.Selected = true ;
}
}
}
cb将报纸名称保存为文本,将ids保留为值.所以我想比较身份证.
这是数据源(收藏夹数据源)
坍塌
< asp:ObjectDataSource ID =" runat 服务器"
OldValuesParameterFormatString =" original_ {0}"
SelectMethod =" GetfavoritesByContactId" TypeName myapp.FavoritesBLL" < SelectParameters >
< asp:QueryStringParameter 名称 =" QueryStringField contactid "
span> =" Int32" / >
< /SelectParameters >
< /asp:ObjectDataSource >
这是填充了另一个数据源(报纸数据源)的复选框列表.没问题,所有cb都已填充.
坍塌
< asp:CheckBoxList ID =" runat 服务器"
span> DataSourceID =" NewspaperDatasource" DataTextField npName"
span> DataValueField =" 报纸编号" ondatabound CheckBoxList1_DataBound" < /asp:CheckBoxList >
这是我尝试比较报纸ID的代码.
坍塌
受保护的void CheckBoxList1_DataBound(对象发送者,EventArgs e)
{
IEnumerable结果= favoriteDataSource.Select();
foreach(结果中的可变项)
{
foreach(CheckBoxList1.Items中的ListItem cb)
{
如果(item.ToString()== cb.Value)
{
cb.Selected = true;
}
}
}
}
我认为ienumerable存在问题.
我只是在这里猜测,因此您必须自己尝试一下,但是似乎item.ToString() 1.给出了您最喜欢的报纸之一的名称,并且在所有复选框中都为报纸的名称
将位于属性cb.Text中.
这都是猜测,因为我们还没有看到初始化asp:checkbox实例的代码,也没有在所有报纸上都能读到的代码.我仍然认为cb.Text可能会解决问题.
受保护的 无效 CheckBoxList1_DataBound(对象发件人,EventArgs e) { IEnumerable结果= favoriteDataSource.Select(); foreach ( var 项目 in 结果中的项) { foreach (ListItem cb in CheckBoxList1.Items) { // 将该cb.Text代替cb.Value // 项目的确切类型是什么?那你就会知道 // 使用什么属性将其与cb.Text进行比较 如果(item.ToString()== cb.Text) { cb.Selected = true ; } } }
脚注:
1.如果FavoritsDataSource.Select()返回了您自己编写的类型的对象列表,则很有可能ToString()方法将只返回那些对象的类名以外的任何有意义的信息.
脚注
最好的问候,
曼弗雷德(Manfred)
i have a checkboxlist populated from a datasource which contains all newspapers. a second datasource which i want to view and compare its select data, contains favorites newspapers.
i want checkboxes preselected according to newspaper ids from the second datasources. its select method returns data as ienumarable. and the values of checkboxes are strings.
how can i compare the values belong to checkboxes and the select data from the second datasource ?
i try the following solution but none of cbs is checked :
protected void CheckBoxList1_DataBound(object sender, EventArgs e)
{
IEnumerable results = FavoritesDataSource.Select();
foreach (var item in results)
{
foreach (ListItem cb in CheckBoxList1.Items)
{
if (item.ToString()== cb.Value)
{
cb.Selected = true;
}
}
}
cb is holding newspaper names as text and ids as value. so i want to compare ids.
this is the datasource ( favorites datasource )
Collapse
<asp:ObjectDataSource ID="FavoritesDataSource" runat="server"
OldValuesParameterFormatString="original_{0}"
SelectMethod="GetfavoritesByContactId" TypeName="myapp.FavoritesBLL">
<SelectParameters>
<asp:QueryStringParameter Name="contactid" QueryStringField="contactid"
Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
this is the checkboxlist populated with another data source (newspapers data source ). there is no problem with that, all cbs are populated.
Collapse
<asp:CheckBoxList ID="CheckBoxList1" runat="server"
DataSourceID="NewspaperDatasource" DataTextField="npName"
DataValueField="newspaperid" ondatabound="CheckBoxList1_DataBound">
</asp:CheckBoxList>
and this is the code i try to compare newspaper ids.
Collapse
protected void CheckBoxList1_DataBound(object sender, EventArgs e)
{
IEnumerable results = FavoritesDataSource.Select();
foreach (var item in results)
{
foreach (ListItem cb in CheckBoxList1.Items)
{
if (item.ToString()== cb.Value)
{
cb.Selected = true;
}
}
}
}
i think there's a problem with ienumerable.
I''m only guessing here so you''ll have to try it out yourself, but it would seem that the item.ToString()1. gives you the name of one of the favorite newspapers and in all the checkboxes the name of the newspaper
would be in the property cb.Text.
This is all guesswork as we didn''t see the code yet that initialized the asp:checkbox instances and neither the code that reads in all the newspapers. I still think that cb.Text probably does the trick.
protected void CheckBoxList1_DataBound(object sender, EventArgs e) { IEnumerable results = FavoritesDataSource.Select(); foreach (var item in results) { foreach (ListItem cb in CheckBoxList1.Items) { // Make that cb.Text instead of cb.Value // what exactly is the type of item? Then you'd know // what property to use to compare it to cb.Text if (item.ToString() == cb.Text) { cb.Selected = true; } } }
Footnotes:
1. If FavoritsDataSource.Select() returned a list of objects of a type you wrote yourself, chances are good the ToString() method will not return anything sensible at all but the class name of those objects.
End Footnotes
Best Regards,
Manfred
这篇关于以编程方式查看和处理objectdatasource的选择数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!