Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp:GridView中的CheckBoxField-VS 2008_C#_Asp.net - Fatal编程技术网

C# asp:GridView中的CheckBoxField-VS 2008

C# asp:GridView中的CheckBoxField-VS 2008,c#,asp.net,C#,Asp.net,我有一个绑定到对象数据源的gridview控件。除了要显示的列之外,我还想显示以下内容 <Columns> <asp:CheckBoxField DataField="Locked" Visible="true" AccessibleHeaderText="On Hold" ReadOnly="false"/> </Columns> 这里有几个问题: 1.如果我执行了上述操作,则根据数据,我的页面加载和某些行将其记录标记为选中,而某

我有一个绑定到对象数据源的gridview控件。除了要显示的列之外,我还想显示以下内容

 <Columns>
         <asp:CheckBoxField DataField="Locked" Visible="true" AccessibleHeaderText="On Hold" ReadOnly="false"/>
 </Columns>

这里有几个问题: 1.如果我执行了上述操作,则根据数据,我的页面加载和某些行将其记录标记为选中,而某些行没有。但是,用户无法单击任何记录撤消其复选标记。这似乎处于禁用状态

  • 这个checkboxfield似乎没有onclick事件。我想在用户选中或取消选中每个记录时立即更新我的记录。是的,这里的设计很糟糕,但我的手被绑住了

  • 如果我在
    中使用
    ,我如何将其绑定到对象数据源中的锁定列,还是必须通过覆盖gridview控件的任何方法来实现


  • 对于click事件,我也无法理解onclick的变化,所以我自己进行了修改

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Linq;
    using System.Data.Linq;
    using System.Data.Linq.Mapping;
    using System.Web;
    using System.Web.UI.WebControls;
    
    
    namespace Web.Library.Controls.Commands
    {
        public class CommandCheckBox : System.Web.UI.WebControls.CheckBox
        {
            private static readonly object EventCommand = new object();
    
            public event CommandEventHandler Command
            {
                add
                {
                    Events.AddHandler(EventCommand, value);
                }
                remove
                {
                    Events.RemoveHandler(EventCommand, value);
                }
            }
    
            public string CommandName
            {
                get
                {
                    if (this.ViewState["CommandName"] == null)
                        return string.Empty;
    
                    return (string)this.ViewState["CommandName"];
                }
                set { this.ViewState["CommandName"] = value; }
            }
    
            public string CommandArgument
            {
                get
                {
                    if (this.ViewState["CommandArgument"] == null)
                        return string.Empty;
    
                    return (string)this.ViewState["CommandArgument"];
                }
                set { this.ViewState["CommandArgument"] = value; }
            }
    
            protected virtual void OnCommand(CommandEventArgs args)
            {
                CommandEventHandler commandEventHand = Events[EventCommand] as CommandEventHandler;
    
                if (commandEventHand != null)
                {
                    commandEventHand(this, args);
                }
    
                base.RaiseBubbleEvent(this, args);
            }
    
            protected override void OnCheckedChanged(EventArgs e)
            {
                base.OnCheckedChanged(e);
    
                CommandEventArgs args = new CommandEventArgs(this.CommandName, this.CommandArgument);
    
                this.OnCommand(args);
            }
    
    
            public CommandCheckBox()
            {
            }
    
        }
    }
    

    至于数据绑定和更新,我只是在GridRow命令中自己处理。

    您的控件被锁定,因为GridView只允许您在行处于编辑模式时编辑行。我认为您真正需要的是在TemplateField中定义的复选框。然后,您可以在代码隐藏中设置初始“checked”值,或者执行以下操作:

    <asp:CheckBox ID="MyCheckBox" runat="server" Checked='<%#Eval("Locked")' ... />
    
    
    
    至于触发器,您可以将OnCheckedChange属性指向您选择的函数,但我不确定如何告诉它检查了哪一行


    更好的替代方法是使用Button或ImageButton控件执行相同的操作。通过这种方式,您可以使用行ID或您需要的任何内容填充CommandArgument,并且在处理GridView的RowCommand时,您可以访问它。

    要回答#2,我将使用一个

    是否存在,以及是否可以使用OnCheckChanged事件?
    protected void grdSomething_RowDataBound ( Object sender, GridViewRowEventArgs e )
    {
        if ( e.Row.RowType == DataControlRowType.DataRow )
        {
            BusinessObject data = ( BusinessObject ) e.Row.DataItem;
            CheckBox chkLocked = ( CheckBox ) e.Row.FindControl( "chkLocked" );
            chkLocked.Checked = data.Locked;
        }
    }
    
    [WebMethod]
    public static void ToggleLockedStatus ( string key )
    {
        if ( BusinessObjectDictionary.ContainsKey( key ) )
        {
            BusinessObjectDictionary[ key ].Locked = !BusinessObjectDictionary[ key ].Locked;
        }
        else
        {
            throw new Exception( "The business object specified was not found." );
        }
    }