从的ValidationGroup检索选中的复选框值 [英] Retrieving the selected checkbox values from a ValidationGroup

查看:217
本文介绍了从的ValidationGroup检索选中的复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页,其中动态填充与基于数据库的复选框列表视图。对复选框的文本等于数据库中的所有用户的用户名。我想创造一个在选定的复选框的用户名会被删除的控制。要做到这一点,我打算使用foreach循环,将在的ValidationGroup中的每个选中的复选框运行。

I have a page which dynamically populates a listview with checkboxes based on a database. The text for the checkboxes is equal to the usernames of all the users in the database. I am trying to create a control in which the usernames in the selected checkboxes will be deleted. To do this, I plan on using a foreach loop that will run for each selected checkbox within the ValidationGroup.

首先,这里是ASP .NET code,显示我的方式来格式化页面。

First, here is the ASP .NET code, displaying my approach to formatting the page.

        <asp:ListView ID="lvUsers" runat="server">
            <ItemTemplate>
                    <asp:CheckBox ID="chkUser" runat="server" Text='<%# Eval("UserName") %>' ValidationGroup="userCheck" /><br />
            </ItemTemplate>
        </asp:ListView>

下面是我目前(碎)code,这是目前在尝试时,它只能运行的每个选中的复选框foreach循环运行的每个ListView项,foreach循环。

Here is my current (broken) code, which is currently attempting to run a foreach loop for each listview item, when it should only be running a foreach loop for each selected checkbox.

foreach (ListViewItem item in lvUsers.Items) //Trying to replace this with "for each selected checkbox within the userCheck ValidationGroup".
    {

        int UserID = 0;

        String sqlStatement = "SELECT UserID FROM Users WHERE UserName = " + item; //This should be selecting where the UserName = the text value of each selected checkbox.
        SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        SqlCommand comm = new SqlCommand(sqlStatement, conn);
        conn.Open();

        SqlDataReader Reader = comm.ExecuteReader();

        while (Reader.Read())
        {
            UserID = (int)Reader["UserID"];
        }

        Reader.Close();
        conn.Close();

        //Down here I delete all the connected values from various tables based on the value obtained from UserID above.

        }

任何帮助将是非常美联社preciated。

Any help on this would be much appreciated.

推荐答案

找到ASP.NET 控制更好的方法,你可以递归ListView中检索复选框,并测试他们的的ValidationGroup

Using the nice ControlFinder class given by Jimmy in Better way to find control in ASP.NET, you can retrieve the CheckBoxes recursively in the ListView and test their ValidationGroup:

ControlFinder<CheckBox> finder = new ControlFinder<CheckBox>();
finder.FindChildControlsRecursive(lvUsers);

foreach (CheckBox chkBox in finder.FoundControls)
{
    if (chkBox.Checked && chkBox.ValidationGroup == "userCheck")
    {
        // Do something
    }
}

这篇关于从的ValidationGroup检索选中的复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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