开发者

Javascript: Error in identify two different Gridview checkbox

In my web application (asp.net & vb code), I had two different gridview. Each gridview had checkbox to select records for batch update. And then there are two button for batch update, one for Gridview 1 and Gridv开发者_如何学Goiew 2.

I need to validate user had checked checkbox when click corresponding batch update button.

my code is work that it can prompt error msg if no checkbox checked when click corresponding batch update button.

But i found there is a error. For example, when I checked gridview2 checkbox and then clicked gridview1 batch update button, it cannot prompt any error msg, seems that my javascript cannot identify the checkbox related to another gridview.

the following is my code: Javascript Code:

function IsSelectedAtleastOneOT() {
    var loTable1 = document.all("<%=Gridview1.ClientID%>"); // GridView Name        
    count1 = 0;
    with (document.forms[0]) {
        for (var i = 0; i < elements.length; i++) {
            var e1 = elements[i];
e.id.substring(e.id.lastIndexOf('_') + 1, e.id.length) == 'ControlName') // This is our control Name
              if (e1.type == "checkbox" && e1.checked == true) // This is our control Name
            {
                count1 += 1;
            }
        }
    }
    if (count1 == 0) {
        alert("You have not selected any record.");
        return false;
    }
    return true;
}

function IsSelectedAtleastOneActualDur() {
    var loTable2 = document.all("<%=Gridview2.ClientID%>"); // GridView Name
    count2 = 0;
    with (document.forms[0]) {
        for (var i = 0; i < elements.length; i++) {
            var e2 = elements[i];
            if (e2.type == "checkbox" && e2.checked == true ) 
            {
                count2 += 1;
            }
        }
    }
    if (count2 == 0) {
        alert("You have not select any record.");
        return false;
    }
    return true;
}

and the ASP.net code:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize ="10" AutoGenerateColumns="False"
    CellSpacing="1" DataSourceID="SqlDataSource1" CellPadding="2" 
    AllowSorting="True" DataKeyNames="ot_key" >
    <Columns>
         <asp:TemplateField>

 <ItemTemplate>
 <asp:CheckBox ID="chkSelect" runat="server"   />
 </ItemTemplate>
  </asp:TemplateField>

        <asp:HyperLinkField DataNavigateUrlFields="action,ot_key" DataNavigateUrlFormatString="{0}?id={1}"
            DataTextField="post_code" HeaderText="Staff" SortExpression="post_code" meta:resourcekey="GridView1_post_code"/>
    .......

    </Columns>
    <EmptyDataTemplate>
        <span style="font-size: 14pt; color: #990000">
        <asp:Localize runat="server" ID="text_no_record" Text="No records found." meta:resourcekey="text_no_record" />
        </span>
    </EmptyDataTemplate>
</asp:GridView>

<asp:Button ID="btnBatchUpdate" runat="server" Text="Batch Recommend/Approve Overtime Work"  
          OnClick="btnBatchUpdate_Click" 
          OnClientClick="return IsSelectedAtleastOneOT();" Visible="false" 
    Width="277px" meta:resourcekey="btnBatchUpdate" />

<asp:GridView ID="GridView2" runat="server" AllowPaging="True" AutoGenerateColumns="False"
    CellSpacing="1" DataSourceID="SqlDataSource3" CellPadding="2" AllowSorting="True" DataKeyNames="actual_dur_key"  PageSize ="10">
    <Columns>
         <asp:TemplateField>

 <ItemTemplate>
 <asp:CheckBox ID="chkSelectActual_dur" runat="server" />
 </ItemTemplate>
  </asp:TemplateField>

        <asp:HyperLinkField DataNavigateUrlFields="action,actual_dur_key" DataNavigateUrlFormatString="{0}?id={1}"
            DataTextField="post_code" HeaderText="Staff" SortExpression="post_code" meta:resourcekey="GridView2_post_code"/>
.....

    </Columns>
    <EmptyDataTemplate>
        <span style="font-size: 14pt; color: #990000">
        <asp:Localize runat="server" ID="text_no_record" Text="No records found." meta:resourcekey="text_no_record" />
        </span>
    </EmptyDataTemplate>
</asp:GridView>       

<asp:Button ID="btnBatchUpdateActualDur" runat="server" Text="Batch Recommend/Approve Actual Duration"  
          OnClick="btnBatchUpdateActualDur_Click" 
          OnClientClick="return IsSelectedAtleastOneActualDur();" 
    Visible="false" Width="276px" meta:resourcekey="btnBatchUpdateActualDur"/>


As per my understanding, you want to validate both grid views on click of both batch buttons. So just combine the two java scripts as one function or Just create another javascript function which will check both the functions and return a valid result like below

function IsSelectedBoth()
{
     if(IsSelectedAtleastOneOT() && IsSelectedAtleastOneActualDur())
        return true;
     else
        return false;
}

for both the buttons add the same onClientclick function as IsSelectedBoth()

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜