DataList 根据 DataKey 值设置 SelectedIndex
我在(Asp.Net3.5)页面上显示了一个数据列表,用户可以从中进行选择。然后,所选行的 datakey 值将存储在数据库中。
如果同一用户在将来的某个时刻重新访问该页面,则将从数据库中检索所选的数据键值。我想使用这个数据键值来突出显示 DataList 中的相应行。
如何从此 DataKey 值设置 DataList 的适当 SelectedIndex?
我已经尝试过以下方法;
protected void dlCampChars_DataBinding(object sender, EventArgs e)
{
for (int i = 0; i < dlCampChars.Items.Count; i++)
{
// Ignore values that cannot be cast as integer.
try
{
if (dlCampChars.DataKeys[i].ToString() == lSelection.ToString())
{
Label28.Text = i + "";
dlCampChars.SelectedIndex = i + 1;
}
}
catch { }
}
}
如果我在 ItemDataBinding 中设置它,则 SelectedIndex 更新是在 DL 绑定后进行的,并且没有任何效果。有什么想法吗?
谢谢
更新代码
// if stored DataKey exists loop through DataTable
// looking for the index of the item matching the DataKey
int itemIndex = 0;
for (int i = 0; i < dt.Rows.Count; i++)
{
// check the appropriate "DataKey" column name of the current row
if (dt.Rows[i]["cha_Key"].ToString() == lSelection)
{
// match found, set index and break out of loop
itemIndex = i;
break;
}
}
I have a DataList displayed on a (Asp.Net3.5) page which the user can select from. The datakey value of the selected row is then stored in the database.
If the page should be revisited by the same user at some point in the future the selected datakey value is retreived from the DB. I would like to use this datakey value to highlight the corresponding row in the DataList.
How can i set the appropriate SelectedIndex of the DataList from this DataKey value?
I have tried the following;
protected void dlCampChars_DataBinding(object sender, EventArgs e)
{
for (int i = 0; i < dlCampChars.Items.Count; i++)
{
// Ignore values that cannot be cast as integer.
try
{
if (dlCampChars.DataKeys[i].ToString() == lSelection.ToString())
{
Label28.Text = i + "";
dlCampChars.SelectedIndex = i + 1;
}
}
catch { }
}
}
If i set it in ItemDataBinding the SelectedIndex update is made after the DL has been bound and has no effect. Any ideas??
Thanks
UPDATED CODE
// if stored DataKey exists loop through DataTable
// looking for the index of the item matching the DataKey
int itemIndex = 0;
for (int i = 0; i < dt.Rows.Count; i++)
{
// check the appropriate "DataKey" column name of the current row
if (dt.Rows[i]["cha_Key"].ToString() == lSelection)
{
// match found, set index and break out of loop
itemIndex = i;
break;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 DataList 领域,事情似乎有点倒退,具体取决于项目的渲染时间以及它所在的模板(请查看下面的第一个链接以获取解释)。 ItemDataBound 方法是有效的,但有时有点古怪,如该文章中所述。在您描述的情况下,我相信第二种方法会起作用,您可以在调用 DataBind() 之前设置 SelectedIndex 属性。步骤如下:
下面是一个示例:
您可能会发现这些文章很有帮助:
编辑: 添加了循环代码的 DataTable。无论您的实际数据源对象是什么,这个想法都是相同的。
It seems things are a little backwards in DataList land, depending on when the item is rendered and which template it is in (check out the 1st link below for an explanation). The ItemDataBound approach is valid, but sometimes is a little quirky as described in that article. In the case you describe I believe the 2nd approach would work, where you can set the SelectedIndex property prior to the call to DataBind(). The steps are:
Here's an example:
You might find these articles helpful:
EDIT: added DataTable for loop code. The idea would be the same for whatever your actual datasource object is.