如何以编程方式创建和使用来自ASP.NET的复选框列表? [英] How to programmatically create and use a list of checkboxes from ASP.NET?

查看:110
本文介绍了如何以编程方式创建和使用来自ASP.NET的复选框列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个含有表格的页面,我需要允许用户选择要处理的行。我想出了如何添加一列复选框到表中,但我似乎不知道如何测试是否检查表单提交时。如果它们是静态元素,我可以检查do this.theCheckBox ,但它们是程序生成的。

I have a page with a table of stuff and I need to allow the user to select rows to process. I've figured out how to add a column of check boxes to the table but I can't seem to figure out how to test if they are checked when the form is submitted. If they were static elements, I'd be able to just check do this.theCheckBox but they are programaticly generated.

另外,我不太高兴我如何附加我的数据(通过填充它在ID ID属性)。

Also I'm not very happy with how I'm attaching my data to them (by stuffing it in there ID property).

我不知道它是相关的,但我正在看一点catch-22,因为我需要知道在上次创建的 之前复选框是否被检查,我可以重新运行创建它们的代码。

I'm not sure if it's relevant but I'm looking at a bit of a catch-22 as I need to known which of the checkboxes that were created last time around were checked before I can re-run the code that created them.

编辑:
我找到了一个几乎解决方案。通过设置 AutoPostBack 属性和 CheckedChanged 事件:

checkbox.AutoPostBack = false;
checkbox.CheckedChanged += new EventHandler(checkbox_CheckedChanged);

我可以在任何已更改的复选框的帖子上获取代码。然而,这有两个问题:

I can get code to be called on a post back for any check box that has changed. However this has two problems:


  • 回调在之后(或期间,我不确定)处理 Page_Load 我需要使用此信息

  • 当页面加载并仍处于选中状态时选中的复选框不会调用回调。

  • The call back is processed after (or during, I'm not sure) Page_Load where I need to use this information
  • The call back is not called for check boxes that were checked when the page loaded and still are.

编辑2:

我最后做的是用一个已知的前缀标记所有的ID,并在 Form_Load 的顶部填充:

What I ended up doing was tagging all my ID's with a know prefix and stuffing this at the top of Form_Load:

foreach (string v in this.Request.Form.AllKeys)
{
    if (v.StartsWith(Prefix))
    {
        var data = v.Substring(Prefix.Length);
    }
}

一切似乎运行得很晚。

推荐答案

我最后做的是用一个已知的前缀标记所有的ID,并在 Form_Load

What I ended up doing was tagging all my ID's with a know prefix and stuffing this at the top of Form_Load:

foreach (string v in this.Request.Form.AllKeys)
{
    if (v.StartsWith(Prefix))
    {
        var data = v.Substring(Prefix.Length);
    }
}

一切似乎运行得很晚。

这篇关于如何以编程方式创建和使用来自ASP.NET的复选框列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆