即使 ReadOnly 设置为 false,ASP.NET GridView 中的 CheckBoxField 列也会被禁用

发布于 2024-09-06 06:32:25 字数 391 浏览 8 评论 0原文

我有一个带有两个 CheckBoxField 列的 GridView。它们都将 ReadOnly 属性设置为 false,但为它们生成的 html 代码具有属性 disabled="disabled"。所以该值无法更改。

生成的 HTML 示例:

<span disabled="disabled"><input id="ctl00_ContentBody_GridView_ctl02_ctl01" type="checkbox" name="ctl00$ContentBody$GridView$ctl02$ctl01" checked="checked" disabled="disabled" /></span>

有人能说一下如何弄清楚吗?

I have a GridView with two CheckBoxField columns. They both have ReadOnly property set to false, but html code generated for them has attribute disabled="disabled". So the value cannot be changed.

Generated HTML example:

<span disabled="disabled"><input id="ctl00_ContentBody_GridView_ctl02_ctl01" type="checkbox" name="ctl00$ContentBody$GridView$ctl02$ctl01" checked="checked" disabled="disabled" /></span>

Can anybody say how to figure it out?

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

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

发布评论

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

评论(2

旧情勿念 2024-09-13 06:32:25

这是设计使然;默认情况下,GridView 中的行不可编辑。

有两种方法可以解决此问题:

1. 添加编辑链接

在 GridView 标记中,添加 AutoGenerateEditButton="True"。当 GridView 在浏览器中呈现时,您现在应该会找到一个标记为“编辑”的超链接。如果单击它,GridView 中的字段将变得可编辑,并且“编辑”链接将变成两个链接,一个用于将更改保存到数据库,另一个用于放弃它们。使用此方法,可以为您完成将 GridView 中的更改连接到数据库的所有管道,具体取决于您如何进行数据绑定。此示例使用 SqlDataSource 控件。
替代文本
(来源:philippursglove.com

替代文字

2. 添加一个带有复选框的 TemplateField

标签内,您可以添加您自己设置数据绑定的 TemplateField,例如

<asp:TemplateField HeaderText="Discontinued">  
    <ItemTemplate>  
        <asp:CheckBox runat="server" ID="DiscontinuedCheckBox" 
            Checked='<%# Eval("Discontinued")  %>' AutoPostback="true" 
            OnCheckedChanged="DiscontinuedCheckBox_CheckedChanged" />  
    </ItemTemplate>  
</asp:TemplateField>

alt text
(来源:philippursglove.com

此复选框将被启用,但您需要自己完成工作才能将任何更改反映回数据库。只要您可以获得数据库密钥,这很简单,因为您需要在某个时刻运行 UPDATE 语句,并且您希望在正确的行上运行它!您可以通过以下两种方式执行此操作:

在 Gridview 标记中,添加 DataKeyNames="MyDatabasePrimaryKey"。然后,在 CheckedChanged 事件处理程序中,您需要找出您所在的行并在 DataKeys 数组中查找。

protected void DiscontinuedCheckBox_CheckedChanged(object sender, EventArgs e)
{
    CheckBox DiscontinuedCheckBox;
    SqlConnection conn;
    SqlCommand cmd;
    int productId;
    GridViewRow selectedRow;

    // Cast the sender object to a CheckBox
    DiscontinuedCheckBox = (CheckBox)sender;

    // We can find the row we clicked the checkbox in by walking up the control tree
    selectedRow = (GridViewRow)DiscontinuedCheckBox.Parent.Parent;

    // GridViewRow has a DataItemIndex property which we can use to look up the DataKeys array
    productId = (int)ProductGridView.DataKeys[selectedRow.DataItemIndex].Value;

    using (conn = new SqlConnection(ProductDataSource.ConnectionString))
    {
        cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandType = CommandType.Text;
        if (DiscontinuedCheckBox.Checked)
        {
            cmd.CommandText = "UPDATE Products SET Discontinued = 1 WHERE ProductId = " + ProductId.ToString();
        }
        else
        {
            cmd.CommandText = "UPDATE Products SET Discontinued = 0 WHERE ProductId = " + ProductId.ToString();
        }
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }
}

或者,您可以在 HiddenField 控件中添加键:

<asp:TemplateField HeaderText="Discontinued">  
    <ItemTemplate>  
        <asp:hiddenfield runat="server" id="ProductIdHiddenField" 
            Value='<%# Eval("ProductID") %>' />
        <asp:CheckBox runat="server" ID="DiscontinuedCheckBox" 
            Checked='<%# Eval("Discontinued")  %>' 
            AutoPostback="true"
            OnCheckedChanged="DiscontinuedCheckBox_CheckedChanged" />  
    </ItemTemplate>  
</asp:TemplateField>

代码:

protected void DiscontinuedCheckBox_CheckedChanged(object sender, EventArgs e)
{
    CheckBox DiscontinuedCheckBox;
    HiddenField ProductIdHiddenField;

    DiscontinuedCheckBox = (CheckBox)sender;

    ProductIdHiddenField = (HiddenField)DiscontinuedCheckBox.Parent.FindControl("ProductIdHiddenField");

    using (conn = new SqlConnection(ProductDataSource.ConnectionString))
    {
    ...
    if (DiscontinuedCheckBox.Checked)
    {
        cmd.CommandText = "UPDATE Products SET Discontinued = 1 WHERE ProductId = " + ProductIdHiddenField.Value;
    }
    ...
    }

This is by design; rows in a GridView are not editable by default.

There's two ways you might address this:

1. Add an Edit link

In your GridView tag, add AutoGenerateEditButton="True". When your GridView renders in the browser, you should now find a hyperlink labelled 'Edit'. If you click it, the fields in your GridView will become editable, and the Edit link will become two links, one to save your changes to the database and the other to discard them. Using this method, all the plumbing to wire up changes in the GridView to the database can be done for you, depending on how you're doing the databinding. This example uses a SqlDataSource control.
alt text
(source: philippursglove.com)

alt text

2. Add a TemplateField with a CheckBox inside it

Inside the <columns> tag, you can add TemplateFields that you set the databinding up for yourself e.g.

<asp:TemplateField HeaderText="Discontinued">  
    <ItemTemplate>  
        <asp:CheckBox runat="server" ID="DiscontinuedCheckBox" 
            Checked='<%# Eval("Discontinued")  %>' AutoPostback="true" 
            OnCheckedChanged="DiscontinuedCheckBox_CheckedChanged" />  
    </ItemTemplate>  
</asp:TemplateField>

alt text
(source: philippursglove.com)

This checkbox will be enabled, but you need to do the work yourself to reflect any changes back to the database. This is straightforward as long as you can get a database key, as you'll need to run an UPDATE statement at some point and you want to run it on the right row! Here's two ways you could do this:

In your Gridview tag, add DataKeyNames="MyDatabasePrimaryKey". Then in your CheckedChanged event handler, you need to find out which row you are in and look that up in the DataKeys array.

protected void DiscontinuedCheckBox_CheckedChanged(object sender, EventArgs e)
{
    CheckBox DiscontinuedCheckBox;
    SqlConnection conn;
    SqlCommand cmd;
    int productId;
    GridViewRow selectedRow;

    // Cast the sender object to a CheckBox
    DiscontinuedCheckBox = (CheckBox)sender;

    // We can find the row we clicked the checkbox in by walking up the control tree
    selectedRow = (GridViewRow)DiscontinuedCheckBox.Parent.Parent;

    // GridViewRow has a DataItemIndex property which we can use to look up the DataKeys array
    productId = (int)ProductGridView.DataKeys[selectedRow.DataItemIndex].Value;

    using (conn = new SqlConnection(ProductDataSource.ConnectionString))
    {
        cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandType = CommandType.Text;
        if (DiscontinuedCheckBox.Checked)
        {
            cmd.CommandText = "UPDATE Products SET Discontinued = 1 WHERE ProductId = " + ProductId.ToString();
        }
        else
        {
            cmd.CommandText = "UPDATE Products SET Discontinued = 0 WHERE ProductId = " + ProductId.ToString();
        }
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }
}

Or, you could add the key in a HiddenField control:

<asp:TemplateField HeaderText="Discontinued">  
    <ItemTemplate>  
        <asp:hiddenfield runat="server" id="ProductIdHiddenField" 
            Value='<%# Eval("ProductID") %>' />
        <asp:CheckBox runat="server" ID="DiscontinuedCheckBox" 
            Checked='<%# Eval("Discontinued")  %>' 
            AutoPostback="true"
            OnCheckedChanged="DiscontinuedCheckBox_CheckedChanged" />  
    </ItemTemplate>  
</asp:TemplateField>

Code:

protected void DiscontinuedCheckBox_CheckedChanged(object sender, EventArgs e)
{
    CheckBox DiscontinuedCheckBox;
    HiddenField ProductIdHiddenField;

    DiscontinuedCheckBox = (CheckBox)sender;

    ProductIdHiddenField = (HiddenField)DiscontinuedCheckBox.Parent.FindControl("ProductIdHiddenField");

    using (conn = new SqlConnection(ProductDataSource.ConnectionString))
    {
    ...
    if (DiscontinuedCheckBox.Checked)
    {
        cmd.CommandText = "UPDATE Products SET Discontinued = 1 WHERE ProductId = " + ProductIdHiddenField.Value;
    }
    ...
    }
未央 2024-09-13 06:32:25

PhilPursglove 解决方案对我有用(即使在嵌套的 grivview 中)。谢谢你!

我的完整代码(修改为使用控制树也获取 grivview,因为由于动态创建,我无法直接访问 netest gridview):

protected void Cb_IsApprovedByManagement_CheckChanged(object sender, EventArgs e)
    {
        CheckBox cb = (CheckBox)sender;

        // find the row we clicked the checkbox in by walking up the control tree
        GridViewRow selectedRow = (GridViewRow)cb.Parent.Parent;
        GridView gridView = (GridView)selectedRow.Parent.Parent;

        //  look up the DataKeys array
        int QuestionID_Current = (int)gridView.DataKeys[selectedRow.DataItemIndex].Value;

        // change value
        QuestionManager.ToggleActivity(QuestionManager.GetQuestion(QuestionID_Current));

PhilPursglove solution works for me (even in a nested grivview). Thank you!

My full code (modified to get also the grivview using the control tree, because i can't access the netest gridview directly because of dynamic creation):

protected void Cb_IsApprovedByManagement_CheckChanged(object sender, EventArgs e)
    {
        CheckBox cb = (CheckBox)sender;

        // find the row we clicked the checkbox in by walking up the control tree
        GridViewRow selectedRow = (GridViewRow)cb.Parent.Parent;
        GridView gridView = (GridView)selectedRow.Parent.Parent;

        //  look up the DataKeys array
        int QuestionID_Current = (int)gridView.DataKeys[selectedRow.DataItemIndex].Value;

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