在 ASP.Net 中动态呈现时,CheckBoxList Items 和 TableRows 有什么区别?

发布于 2024-10-30 17:34:33 字数 3017 浏览 0 评论 0原文

我正在努力稍微更改 ASP.Net 网站中页面的功能。在原始页面中,有一个复选框列表写入页面中,没有硬编码的项目。

相反,在初始渲染中,cbl 将数据绑定到查询结果,如下所示:

sSQL = "select query here";

using (SqlConnection dbcon = new DataAccess().OpenDb())
{
     using (SqlDataReader dr = new SqlCommand(sSQL, dbcon).ExecuteReader())
     {
          cbl.DataTextField = "description";
          cbl.DataValueField = "productid";
          cbl.DataSource = dr;
          cbl.DataBind();
     }
     dbcon.Close();
}

在回发时,执行以下代码:

foreach (ListItem li in cbl.Items) { if (li.Selected) rtnQS += li.Value + ","; }

愉快地遍历复选框列表中的动态数据绑定列表项并检索选中的值。我尝试做同样的事情,在页面上放置一个空表,然后动态创建行:

sSQL = "similar select query here";

using (SqlConnection dbcon = new DataAccess().OpenDb())
{
    using (SqlDataReader dr = new SqlCommand(sSQL, dbcon).ExecuteReader())
    {
       while (dr.Read())
       {
           TableRow tr = new TableRow();

           TableCell tc = new TableCell();

           CheckBox chk = new CheckBox();

           chk.Text = dr["description"].ToString();
           chk.InputAttributes.Add("id", dr["id"].ToString());
           chk.Checked = true;

           tc.Controls.Add(chk);

           TableCell tc2 = new TableCell();

           if (Convert.ToBoolean(dr["controlcondition"]))
           {
                CheckBoxList cbl_sub = new CheckBoxList();
                cbl_sub.Attributes.Add("for", dr["id"].ToString());
                sSQL = "subquery";
                using (SqlConnection newcon = new DataAccess().OpenDb())
                {
                   using (SqlDataReader br = new SqlCommand(sSQL, newcon).ExecuteReader())
                   {
                       cbl_block.DataTextField = "subname";
                       cbl_block.DataValueField = "subid";
                       cbl_block.DataSource = br;
                       cbl_block.DataBind();
                   }
                }

                tc2.Controls.Add(cbl_sub);
           }
           else
           {
              TextBox txtSub = new TextBox();
              txtSub.Attributes.Add("for", dr["id"].ToString());

              tc2.Controls.Add(txtSub);
           }

           tr.Cells.Add(tc);
           tr.Cells.Add(tc2);

           tblDispatchOpt.Rows.Add(tr);
        }
    }

    dbcon.Close();
}

唯一的问题是,当迭代器到达表时,它只读取硬编码的标题行。 (不过,该表在原始页面上呈现出漂亮的所有正确属性。)

以下是尝试读取表行的代码:

foreach (TableRow row in tblDispatchOpt.Rows)
{
      TableCell cell = row.Cells[0];

      ++counter;

      foreach (Control c in cell.Controls)
      {
           if (c is CheckBox)
           {
               CheckBox cb = (CheckBox)c;
               if (cb.Checked && cb.Attributes["ttid"] != null) dtTTInfo.Rows.Add(cb.Attributes["ttid"].ToString(), "", "");
           }
      }
}

除了调试标签检查高兴地告诉我只有一行,而且它是硬编码的标题行。因此基本上不会读取其他行,因为页面在读取它们时似乎不相信它们存在。

那么为什么它不能与桌子一起使用呢?或者,如果它不能与表一起使用,通常的原因是回发上的动态控件无法使用,例如渲染、生命周期等,为什么它可以与复选框列表一起使用?

I'm working on slightly changing the functionality of a page in an ASP.Net website. In the original page there was a checkbox list written into the page with no items hard coded.

Instead on the initial render the cbl gets databound to the results of a query thus:

sSQL = "select query here";

using (SqlConnection dbcon = new DataAccess().OpenDb())
{
     using (SqlDataReader dr = new SqlCommand(sSQL, dbcon).ExecuteReader())
     {
          cbl.DataTextField = "description";
          cbl.DataValueField = "productid";
          cbl.DataSource = dr;
          cbl.DataBind();
     }
     dbcon.Close();
}

On a postback the following code:

foreach (ListItem li in cbl.Items) { if (li.Selected) rtnQS += li.Value + ","; }

Happily goes through the dynamic databound list items in the checkbox list and retrieves the checked values. I tried to do the same thing, putting an empty table on the page and then creating the rows dynamically:

sSQL = "similar select query here";

using (SqlConnection dbcon = new DataAccess().OpenDb())
{
    using (SqlDataReader dr = new SqlCommand(sSQL, dbcon).ExecuteReader())
    {
       while (dr.Read())
       {
           TableRow tr = new TableRow();

           TableCell tc = new TableCell();

           CheckBox chk = new CheckBox();

           chk.Text = dr["description"].ToString();
           chk.InputAttributes.Add("id", dr["id"].ToString());
           chk.Checked = true;

           tc.Controls.Add(chk);

           TableCell tc2 = new TableCell();

           if (Convert.ToBoolean(dr["controlcondition"]))
           {
                CheckBoxList cbl_sub = new CheckBoxList();
                cbl_sub.Attributes.Add("for", dr["id"].ToString());
                sSQL = "subquery";
                using (SqlConnection newcon = new DataAccess().OpenDb())
                {
                   using (SqlDataReader br = new SqlCommand(sSQL, newcon).ExecuteReader())
                   {
                       cbl_block.DataTextField = "subname";
                       cbl_block.DataValueField = "subid";
                       cbl_block.DataSource = br;
                       cbl_block.DataBind();
                   }
                }

                tc2.Controls.Add(cbl_sub);
           }
           else
           {
              TextBox txtSub = new TextBox();
              txtSub.Attributes.Add("for", dr["id"].ToString());

              tc2.Controls.Add(txtSub);
           }

           tr.Cells.Add(tc);
           tr.Cells.Add(tc2);

           tblDispatchOpt.Rows.Add(tr);
        }
    }

    dbcon.Close();
}

The only problem is that when the iterator comes to the table it only reads the hard coded header row. (The table renders beautifully with all the correct attributes on the original page though.)

Here's the bit of code that tries to read the table rows:

foreach (TableRow row in tblDispatchOpt.Rows)
{
      TableCell cell = row.Cells[0];

      ++counter;

      foreach (Control c in cell.Controls)
      {
           if (c is CheckBox)
           {
               CheckBox cb = (CheckBox)c;
               if (cb.Checked && cb.Attributes["ttid"] != null) dtTTInfo.Rows.Add(cb.Attributes["ttid"].ToString(), "", "");
           }
      }
}

Except that a debugging label check happily tells me there's only one row and it's the hard coded header row. So essentially doesn't read the other rows as it seems the page doesn't believe they exist when it comes to read them.

So why doesn't it work with the table? Or, if it doesn't work with the table for the usual reason that things don't work with dynamic controls on postbacks e.g. rendering, lifecycle etc., why does it work with the checkboxlist?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

凉世弥音 2024-11-06 17:34:33

如果您想从中获取值,则必须在每次回发时重新创建动态控件。
您应该在 CreateChildControls() 方法中执行此操作。

You have to recreate dynamic controls on every postback if you want to get values from them.
You should do that in CreateChildControls() method.

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