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

时间:2021-10-07 14:55:26

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.

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

Generated HTML example:

生成的HTML示例:

<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?

任何人都可以说如何解决这个问题?

2 个解决方案

#1


40  

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

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

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 http://philippursglove.com/*/checkboxgridview1.png alt text http://philippursglove.com/*/checkboxgridview2.png

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

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.

标记内,您可以添加自己设置数据绑定的TemplateFields,例如:

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

alt text http://philippursglove.com/*/checkboxgridview3.png

alt text http://philippursglove.com/*/checkboxgridview3.png

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:

此复选框将被启用,但您需要自己完成工作以将任何更改反映回数据库。只要您可以获取数据库密钥,这很简单,因为您需要在某个时刻运行UPDATE语句,并且您希望在右侧行运行它!这有两种方法可以做到这一点:

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.

在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();
    }
}

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

或者,您可以在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>

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;
    }
    ...
    }

#2


1  

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

PhilPursglove解决方案适合我(即使是嵌套的grivview)。谢谢!

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):

我的完整代码(修改为使用控制树获取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));

#1


40  

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

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

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 http://philippursglove.com/*/checkboxgridview1.png alt text http://philippursglove.com/*/checkboxgridview2.png

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

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.

标记内,您可以添加自己设置数据绑定的TemplateFields,例如:

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

alt text http://philippursglove.com/*/checkboxgridview3.png

alt text http://philippursglove.com/*/checkboxgridview3.png

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:

此复选框将被启用,但您需要自己完成工作以将任何更改反映回数据库。只要您可以获取数据库密钥,这很简单,因为您需要在某个时刻运行UPDATE语句,并且您希望在右侧行运行它!这有两种方法可以做到这一点:

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.

在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();
    }
}

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

或者,您可以在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>

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;
    }
    ...
    }

#2


1  

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

PhilPursglove解决方案适合我(即使是嵌套的grivview)。谢谢!

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):

我的完整代码(修改为使用控制树获取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));