DataList 根据 DataKey 值设置 SelectedIndex

发布于 2024-08-03 07:18:16 字数 1395 浏览 3 评论 0原文

我在(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我只土不豪 2024-08-10 07:18:16

在 DataList 领域,事情似乎有点倒退,具体取决于项目的渲染时间以及它所在的模板(请查看下面的第一个链接以获取解释)。 ItemDataBound 方法是有效的,但有时有点古怪,如该文章中所述。在您描述的情况下,我相信第二种方法会起作用,您可以在调用 DataBind() 之前设置 SelectedIndex 属性。步骤如下:

  1. 假设 PostBack 为 false
  2. 设置 SelectedIndex
  3. 设置数据源
  4. DataBind

下面是一个示例:

  void Page_Load(Object sender, EventArgs e) 
  {
     // Load sample data only once, when the page is first loaded.
     if (!IsPostBack) 
     {
       dlCampChars.DataSource = CreateDataSource();
       dlCampChars.DataBind();
     }
  }

private DataTable CreateDataSource()
{
  // however you get your data and whatever the resulting object is
  // for example: DataTable, DataView, etc.
  DataTable dt = [relevant code here];

  // retrieve the user's stored DataKey
  string datakey = [retrieved datakey value from DB];

  // 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]["DataKey"].ToString() == datakey)
    {
      // match found, set index and break out of loop
      itemIndex = i;
      break;
    }
  }

  // set SelectedIndex
  dlCampChars.SelectedIndex = itemIndex;

  // now return the DataSource (ie. DataTable etc.)
  return dt;
}

您可能会发现这些文章很有帮助:

编辑: 添加了循环代码的 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:

  1. Provided PostBack is false
  2. Set the SelectedIndex
  3. Set the DataSource
  4. DataBind

Here's an example:

  void Page_Load(Object sender, EventArgs e) 
  {
     // Load sample data only once, when the page is first loaded.
     if (!IsPostBack) 
     {
       dlCampChars.DataSource = CreateDataSource();
       dlCampChars.DataBind();
     }
  }

private DataTable CreateDataSource()
{
  // however you get your data and whatever the resulting object is
  // for example: DataTable, DataView, etc.
  DataTable dt = [relevant code here];

  // retrieve the user's stored DataKey
  string datakey = [retrieved datakey value from DB];

  // 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]["DataKey"].ToString() == datakey)
    {
      // match found, set index and break out of loop
      itemIndex = i;
      break;
    }
  }

  // set SelectedIndex
  dlCampChars.SelectedIndex = itemIndex;

  // now return the DataSource (ie. DataTable etc.)
  return dt;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文