开发者

asp.net trigger for a checkbox control

Hi All i am using the below code and wanted to make a button visible and invisible based on checkbox status. I am using the trigger to call a event where i will write code to make that button visible or invisible. If i use the below code i get an error like "System.InvalidOperationException: A control with ID 'chkDelete' could not be found for the trigger in UpdatePanel 'UpdatePanel1'." Please help me.

<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">       
            <Colu开发者_运维技巧mns> 
                <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>


As an alternative, why don't you use client-side environment to do this? It's more easy and more native.

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

Now, based on this class, you can use CSS to dim your button, if it's a span, or a div (custom button). Otherwise you can use:

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


This will let you get all the information you need to delete the appropriate record.

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


ControlId for the CheckBox field <asp:CheckBox ID="chkDelete" runat="server" AutoPostBack="true" ></asp:CheckBox> will different for each row that is why it can't map the controlId to the trigger.
I suggest you to use checkbox's CheckedChanged event to trigger your method.


Yes it is CheckedChanged. Although it doesnot look what you have mentioned in checkbox. but it works this way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜