即使 ReadOnly 设置为 false,ASP.NET GridView 中的 CheckBoxField 列也会被禁用 [英] CheckBoxField columns in ASP.NET GridView are disabled even if ReadOnly set to false

查看:20
本文介绍了即使 ReadOnly 设置为 false,ASP.NET GridView 中的 CheckBoxField 列也会被禁用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个 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="禁用"/></span>

谁能告诉我怎么弄出来?

解决方案

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

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

1.添加编辑链接

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

(来源:

2.添加一个包含 CheckBox 的 TemplateField

<columns> 标签内,您可以添加为自己设置数据绑定的 TemplateFields,例如


(来源:philippursglove.com)

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

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

protected void DiscontinuedCheckBox_CheckedChanged(object sender, EventArgs e){CheckBox DiscontinuedCheckBox;SqlConnection 连接;SqlCommand cmd;国际产品ID;GridViewRow selectedRow;//将发送者对象转换为 CheckBoxDiscontinuedCheckBox = (CheckBox)sender;//我们可以通过向上走控制树来找到我们单击复选框的行selectedRow = (GridViewRow)DiscontinuedCheckBox.Parent.Parent;//GridViewRow 有一个 DataItemIndex 属性,我们可以用它来查找 DataKeys 数组productId = (int)ProductGridView.DataKeys[selectedRow.DataItemIndex].Value;使用 (conn = new SqlConnection(ProductDataSource.ConnectionString)){cmd = 新的 SqlCommand();cmd.Connection = conn;cmd.CommandType = CommandType.Text;if (DiscontinuedCheckBox.Checked){cmd.CommandText = "UPDATE Products SET Discontinued = 1 WHERE ProductId = "+ ProductId.ToString();}别的{cmd.CommandText = "UPDATE Products SET Discontinued = 0 WHERE ProductId = "+ ProductId.ToString();}conn.Open();cmd.ExecuteNonQuery();conn.Close();}}

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

<项目模板><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");使用 (conn = new SqlConnection(ProductDataSource.ConnectionString)){...if (DiscontinuedCheckBox.Checked){cmd.CommandText = "UPDATE Products SET Discontinued = 1 WHERE ProductId = "+ ProductIdHiddenField.Value;}...}

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?

解决方案

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.

(source: philippursglove.com)

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>


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

这篇关于即使 ReadOnly 设置为 false,ASP.NET GridView 中的 CheckBoxField 列也会被禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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