开发者

[javascript]Validate Gridview Footer Checkbox

[Asp.net / Vb.Net] How can I v开发者_JAVA百科alidate my gridview footer checkbox using Javascript.

I have a gridview with footer having 8 checkboxes -(chkActive1,chkActive2,.....chkActive3) - The condition I want to jave is - The user need to check atleast 1 checkbox or else the trasaction would not allow

thanks..


so simple, every check box have unique id check in view source there is some patent in name in controls when you check viewsource of that page, just use that, its simple way.


If I misunderstood you then please correct me, I think this could be done via using a custom validator in asp.net Since you haven't mentioned I am assuming you are using CheckBoxList Control.

On .aspx page use custom validator

<asp:CustomValidator ID="customValidatorForCheckboxlist" runat="server" 
ErrorMessage="Required Field" ValidationGroup="valSurvey" 
 OnServerValidate="CheckifCheckBoxIsEmpty" SetFocusOnError="true" Display="Dynamic"></asp:CustomValidator>

On codebhind using this logic to iterate through your gridview row and check for footer row. Inside footer row iterate through all checkboxes and then see if they are selected or not. I am using a boolean field to check if anything was selected. Jump out if anything is selected.

Protected Sub CheckifCheckBoxIsEmpty(ByVal sender As Object, ByVal e As ServerValidateEventArgs)

        Dim valbool As Boolean = False
        For Each gvrow As GridViewRow In gridview_1.Rows
'Check for footer row.        
If gvrow.RowType = DataControlRowType.Footer 
            For Each ct As Control In gvrow.Cells(1).Controls
                If ct.GetType.ToString().Equals("System.Web.UI.WebControls.CheckBoxList") Then
                    Dim _checkboxlist As CheckBoxList = DirectCast(ct, CheckBoxList)
                    For Each ListItem1 As ListItem In _checkboxlist.Items
                        If ListItem1.Selected = True Then
                            valbool = True
                            Exit For
                        Else
                            valbool = False
                        End If
                    Next
                End If
            Next
          End If 
        Next
        If valbool = False Then
            e.IsValid = False
        Else
            e.IsValid = True
        End If
    End Sub
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜