在 ASP.NET 的 DATAGRID 中查找 CHECKBOX

发布于 2024-09-28 00:00:24 字数 2472 浏览 6 评论 0原文

我有一个 GRIDVIEW 和几个复选框。

当我选择一个复选框时,我需要运行一些代码。

为了检测它,我对 GRIDVIEW 中包含的 CHECKBOX 使用事件处理程序。

我无法使用错误的代码访问 CHECKBOX。

你知道我做错了什么吗?感谢您的帮助。再见

ASPX

    <asp:Label ID="uxMessageDisplayer" runat="server" Visible="False" EnableViewState="False"></asp:Label>
<asp:GridView ID="uxUserListDisplayer" runat="server" AutoGenerateColumns="False"
    OnRowDataBound="uxUserListDisplayer_RowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="Active">
            <ItemTemplate>
                <asp:CheckBox ID="uxActiveCheckBoxSelector" runat="server" AutoPostBack="true" OnCheckedChanged="uxRoleCheckBoxSelector_CheckChanged" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Users">
            <ItemTemplate>
                <asp:Label runat="server" ID="uxUserNameLabelDisplayer" Text='<%# Container.DataItem %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:LinkButton ID="uxLinkEditButton" runat="server" CausesValidation="False" CommandName="Edit"
                    Text="Edit"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:LinkButton ID="uxLinkDeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
                    Text="Delete"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

代码背后

    protected void uxRoleCheckBoxSelector_CheckChanged(object sender, EventArgs e)
    {
         // Reference the CheckBox that raised this event   
        //CheckBox uxActiveCheckBoxSelector = sender as CheckBox;

        CheckBox activeCheckBox = (CheckBox)FindControl("uxActiveCheckBoxSelector");

        if (activeCheckBox.Checked == true)
        {
            uxMessageDisplayer.Text = "T - Aproved User";
            uxMessageDisplayer.Enabled = false;
        }
        else
        {
            uxMessageDisplayer.Text = "F - NOT Aproved User";
            uxMessageDisplayer.Enabled = false;
        }
       }

I have a GRIDVIEW and with several CHECKBOXS.

When i selected a CHECKBOX I need run some code.

To detect it, I use an EVENT HANDLER for the CHECKBOX included in a GRIDVIEW.

I cannot access the CHECKBOX with my wrong code.

Do you have any idea what I am doing wrong? Thanks for your help. Bye

ASPX

    <asp:Label ID="uxMessageDisplayer" runat="server" Visible="False" EnableViewState="False"></asp:Label>
<asp:GridView ID="uxUserListDisplayer" runat="server" AutoGenerateColumns="False"
    OnRowDataBound="uxUserListDisplayer_RowDataBound">
    <Columns>
        <asp:TemplateField HeaderText="Active">
            <ItemTemplate>
                <asp:CheckBox ID="uxActiveCheckBoxSelector" runat="server" AutoPostBack="true" OnCheckedChanged="uxRoleCheckBoxSelector_CheckChanged" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Users">
            <ItemTemplate>
                <asp:Label runat="server" ID="uxUserNameLabelDisplayer" Text='<%# Container.DataItem %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:LinkButton ID="uxLinkEditButton" runat="server" CausesValidation="False" CommandName="Edit"
                    Text="Edit"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:LinkButton ID="uxLinkDeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
                    Text="Delete"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

CODE BEHIND

    protected void uxRoleCheckBoxSelector_CheckChanged(object sender, EventArgs e)
    {
         // Reference the CheckBox that raised this event   
        //CheckBox uxActiveCheckBoxSelector = sender as CheckBox;

        CheckBox activeCheckBox = (CheckBox)FindControl("uxActiveCheckBoxSelector");

        if (activeCheckBox.Checked == true)
        {
            uxMessageDisplayer.Text = "T - Aproved User";
            uxMessageDisplayer.Enabled = false;
        }
        else
        {
            uxMessageDisplayer.Text = "F - NOT Aproved User";
            uxMessageDisplayer.Enabled = false;
        }
       }

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

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

发布评论

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

评论(3

初心未许 2024-10-05 00:00:24

如果我没有误解您的问题,您正在尝试根据其选中状态将标签文本与 checkbox 设置在同一行。

以下是我在电脑上尝试的代码片段,希望对您有所帮助。

.aspx:

<asp:GridView ID="GridView1" runat="server">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckChanged" AutoPostBack="true" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

.cs:

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
        return;

    //create dummy data
    List<string> rows = new List<string>();
    Enumerable.Range(1, 5).ToList().ForEach(x => rows.Add(x.ToString()));

    //bind dummy data to gridview
    GridView1.DataSource = rows;
    GridView1.DataBind();
}

    protected void CheckBox1_CheckChanged(object sender, EventArgs e)
    {
        //cast sender to checkbox
        CheckBox CheckBox1 = (CheckBox)sender;

        //retrieve the row where checkbox is contained
        GridViewRow row = (GridViewRow)CheckBox1.NamingContainer;

        //find the label in the same row
        Label Label1 = (Label)row.FindControl("Label1");

        //logics
        if (CheckBox1 != null)  //make sure checkbox1 is found
        {
            if (CheckBox1.Checked)
            {
                if (Label1 != null) //make sure label1 is found
                {
                    Label1.Text = "Checked";
                }

            }
            else
            {
                if (Label1 != null)
                {
                    Label1.Text = "Unchecked";
                }
            }
        }
    }

If I am not mistaken by your question, you are trying to set the text of the label on the same row with the checkbox based on its checked status.

Below is the code snippet I tried on my pc, hope it helps.

.aspx:

<asp:GridView ID="GridView1" runat="server">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckChanged" AutoPostBack="true" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

.cs:

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack)
        return;

    //create dummy data
    List<string> rows = new List<string>();
    Enumerable.Range(1, 5).ToList().ForEach(x => rows.Add(x.ToString()));

    //bind dummy data to gridview
    GridView1.DataSource = rows;
    GridView1.DataBind();
}

    protected void CheckBox1_CheckChanged(object sender, EventArgs e)
    {
        //cast sender to checkbox
        CheckBox CheckBox1 = (CheckBox)sender;

        //retrieve the row where checkbox is contained
        GridViewRow row = (GridViewRow)CheckBox1.NamingContainer;

        //find the label in the same row
        Label Label1 = (Label)row.FindControl("Label1");

        //logics
        if (CheckBox1 != null)  //make sure checkbox1 is found
        {
            if (CheckBox1.Checked)
            {
                if (Label1 != null) //make sure label1 is found
                {
                    Label1.Text = "Checked";
                }

            }
            else
            {
                if (Label1 != null)
                {
                    Label1.Text = "Unchecked";
                }
            }
        }
    }
总以为 2024-10-05 00:00:24

我假设事件处理程序实际上已注册到复选框。

CheckBox activeCheckBox = (CheckBox)sender;

什么是“uxActiveCheckBoxSelector”以及为什么您忽略sender

I'm assuming the event handler is actually registered to the checkbox.

CheckBox activeCheckBox = (CheckBox)sender;

what is "uxActiveCheckBoxSelector" and why are you ignoring sender?

樱花落人离去 2024-10-05 00:00:24

代码已按照建议更正!
对初学者有用的资源

        protected void uxRoleCheckBoxSelector_CheckChanged(object sender, EventArgs e)
    {
        // Cast sender to CheckBox
        CheckBox activeCheckBox = (CheckBox)sender;

        // Retrieve the row where CheckBox is contained (NamingContainer used to retrive parent control
        GridViewRow row = (GridViewRow)activeCheckBox.NamingContainer;

        if (activeCheckBox.Checked == true)
        {
            uxMessageDisplayer.Text = "T - Aproved User";
        }
        else
        {
            uxMessageDisplayer.Text = "F - NOT Aproved User";
        }
    }

Code corrected as suggest!
Usefull resource for beginners

        protected void uxRoleCheckBoxSelector_CheckChanged(object sender, EventArgs e)
    {
        // Cast sender to CheckBox
        CheckBox activeCheckBox = (CheckBox)sender;

        // Retrieve the row where CheckBox is contained (NamingContainer used to retrive parent control
        GridViewRow row = (GridViewRow)activeCheckBox.NamingContainer;

        if (activeCheckBox.Checked == true)
        {
            uxMessageDisplayer.Text = "T - Aproved User";
        }
        else
        {
            uxMessageDisplayer.Text = "F - NOT Aproved User";
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文