asp.net触发复选框控件

时间:2011-07-07 09:53:13

标签: c# asp.net

大家好我正在使用以下代码,并希望根据复选框状态使按钮可见且不可见。我正在使用触发器调用一个事件,我将编写代码使该按钮可见或不可见。如果我使用下面的代码我得到一个错误,如“System.InvalidOperationException:在UpdatePanel'UpdatePanel1'中找不到触发器ID为”chkDelete“的控件。”请帮帮我。

<asp:ScriptManager ID="ScriptManager1" runat="server" /> 
     <asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers ="false"> 
     <ContentTemplate> 
             <asp:GridView ID="gvEventMechanic" runat="server" AutoGenerateColumns="False" PageSize="5" 
                        GridLines="None" AllowSorting="true" BorderWidth="1" 
                        EnableViewState="true" AllowPaging="true">       
            <Columns> 
                <asp:TemplateField>                     
                    <HeaderTemplate> 
                        Disable 
                        </HeaderTemplate> 
                    <ItemStyle HorizontalAlign="Center" /> 
                    <ItemTemplate>                            
                        <asp:CheckBox ID="chkDelete" runat="server" AutoPostBack="true" ></asp:CheckBox> 
                    </ItemTemplate> 
                </asp:TemplateField>                    
            </Columns>                 
        </asp:GridView> 
        </ContentTemplate>   
         <Triggers> 
                <asp:AsyncPostBackTrigger ControlID="chkDelete" EventName="CheckBoxEventChanged" /> 
            </Triggers>                    
        </asp:UpdatePanel>

4 个答案:

答案 0 :(得分:1)

作为替代方案,为什么不使用客户端环境来执行此操作?它更容易,更原生。

   $('#input[type=checkbox][id*=chkDelete]').change(function(){
        $('#button').toggleClass('disabled');
   });

现在,基于这个类,您可以使用CSS来调暗按钮,如果它是一个span或div(自定义按钮)。否则你可以使用:

   $('#input[type=checkbox][id*=chkDelete]').change(function(){
        if ($(this).is(':checked'))
        {
            $('#button').removeAttr('disabled');
        }
        else
        { 
            $('#button').attr('disabled', 'disabled');
        }
   });

答案 1 :(得分:1)

这将使您获得删除相应记录所需的所有信息。

// If you bind a list of objects as your data source you can use this to get the
// index into the list.
protected void OnCheckedChanged( Object sender, EventArgs e )
{
    if ( sender is CheckBox )
    {
        // we do this to get the index into the list of the item we want to work with.
        CheckBox     cb = sender as CheckBox;
        GridViewRow gvr = cb.NamingContainer as GridViewRow;

        int dataItemIndex = gvr.DataItemIndex;  // index into your list, regardless of page
        int rowIndex      = gvr.RowIndex;       // row index in gridview.
    }
}

答案 2 :(得分:0)

CheckBox字段<asp:CheckBox ID="chkDelete" runat="server" AutoPostBack="true" ></asp:CheckBox>的ControlId对于每一行都是不同的,这就是为什么它无法将controlId映射到触发器。
我建议你使用复选框的CheckedChanged事件来触发你的方法。

答案 3 :(得分:0)

是的,CheckedChanged。 虽然它看起来不像你在复选框中提到的那样。但它就是这样的。