Assign number to TextBox inside ASP.Net Repeater on CheckBox checked in JavaScript

Last Reply one year ago By dharmendr

Posted one year ago

Hello Sir

i have a form having 4 rows inside repeater and first column is checkbox on checbox click i want enter value in order no. column textbox. Suppose any checkbox i click the value should be 1 again if i click any check box then value should be 2 again i click any checkbox the vale should be 3 like that 4 also. but if i uncheck any checkbox then value will decrease which is greater that checked value.

Like if i uncheck checkbox of value 3 then checkbox of vale 4 will become 3 in repaeter and so on. https://ibb.co/jgzBgL

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPageInnernew.Master" AutoEventWireup="true" CodeBehind="TT_SubstitutionSetting.aspx.cs" Inherits="eCareERP.TimeTable.TT_SubstitutionSetting" %>

<%@ Register Assembly="CustomButton" Namespace="Custom.eCareERP" TagPrefix="cc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:Label ID="lbl_alloted" Text="0" runat="server" Visible="false"></asp:Label>
    <div class="table table-responsive over-auto " id='example'>
        <table id="myTable_k" class="table table-striped table-bordered">
            <thead>
                <tr>
                    <th>S.No.</th>
                    <th>
                        <div class="checkbox checkbox-info m-0">
                            <asp:CheckBox ID="checkeddall" runat="server" />
                            <label>Select </label>
                        </div>
                    </th>
                    <th>Substitution Pattern</th>
                    <th>Order No.</th>
                </tr>
            </thead>
            <tbody>
                <asp:Repeater ID="rpt_SubsSetting" runat="server" OnItemDataBound="rpt_SubsSetting_ItemDataBound">
                    <ItemTemplate>
                        <tr id="ItemRow">
                            <td><%# Container.ItemIndex + 1 %>
                                <asp:Label ID="lblid" runat="server" Text='<%# Eval("Pat_Id") %>' Visible="false" />
                            </td>
                            <td>
                                <div class="checkbox checkbox-info m-0">
                                    <asp:CheckBox ID="chkSelect" onchange="CustomValidation()" Checked='<%# Eval("pat_check").ToString() == "True" %>' runat="server" CssClass="chkbox m-0" Text=" " />
                                </div>
                            </td>
                            <td>
                                <asp:Label ID="lbl_subjects" runat="server" Text='<%# Eval("Pat_Name") %>' />
                            </td>
                            <td>
                                <div class="col-md-6 p-0">
                                    <asp:HiddenField ID="hdnid" runat="server" />
                                    <fspl:eCareTextBox ID="TXT_periods" SetCss="input-sm" TextType="Numeric" Required="true" MaxValue="90" runat="server" Enabled="false" SetFocus="true" SetEnable="true" Text='<%# Eval("Pat_Number") %>' />
                                </div>
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
            </tbody>
        </table>
    </div>
    <div class="row button-box m-t-0 text-center">
        <cc1:SingleButtonNew ID="btnSubstitute" CssClass="btn btn-outline" OnbtnSingleClick="btnSubstitute_btnSingleClick" OnClientClick="return CheckDuplicate()" runat="server" Text="Update Setting" />
    </div>
    <script type="text/javascript">
        $(document).ready(function () {
            CustomValidation();
        });
        function CustomValidation()
        {
            var txt = $("input[id*='TXT_periods']");
            var chk = $("input[id*='ContentPlaceHolder1_rpt_SubsSetting_chkSelect']");
            var txt_show = $("span[id='ContentPlaceHolder1_lbl_alloted']");
            var num = 0;
            flag = false;
            for (var i = 0; i < chk.length; i++)
            {
                if (chk[i].checked)
                {
                    flag = true;
                    if (parseInt(txt[i].value) > 0)
                    {
                        num = parseInt(num) + parseInt(txt[i].value);
                    }                    
                    txt[i].disabled = false;                  
                }
                else
                {//ContentPlaceHolder1_rpt_SubsSetting_TXT_periods_7_NameRequiredFieldValidator_7

                    txt[i].disabled = true;
                    EnableDisbleValid();
                    txt[i].value = '';
                }
            }
            //document.getElementById('ContentPlaceHolder1_lbl_alloted').textContent = num;
        } 
    </script>
</asp:Content>

 

You are viewing reply posted by: dharmendr one year ago.
Posted one year ago Modified on one year ago

Hi telldurges,

Refer the below sample code.

HTML

<div>
    <asp:Label ID="lbl_alloted" Text="0" runat="server" Visible="false"></asp:Label>
    <div class="table table-responsive over-auto " id='example'>
        <table id="myTable_k" class="table table-striped table-bordered">
            <thead>
                <tr>
                    <th>
                        S.No.
                    </th>
                    <th>
                        <div class="checkbox checkbox-info m-0">
                            <asp:CheckBox ID="checkeddall" runat="server" />
                            <label>
                                Select
                            </label>
                        </div>
                    </th>
                    <th>
                        Substitution Pattern
                    </th>
                    <th>
                        Order No.
                    </th>
                </tr>
            </thead>
            <tbody>
                <asp:Repeater ID="rpt_SubsSetting" runat="server">
                    <ItemTemplate>
                        <tr id="ItemRow">
                            <td>
                                <%# Container.ItemIndex + 1 %>
                                <asp:Label ID="lblid" runat="server" Text='<%# Eval("Pat_Id") %>' Visible="false" />
                            </td>
                            <td>
                                <div class="checkbox checkbox-info m-0">
                                    <asp:CheckBox ID="chkSelect" onclick="CustomValidation(this)" Checked='<%# Eval("pat_check").ToString() == "True" %>'
                                        runat="server" CssClass="chkbox m-0" Text=" " />
                                </div>
                            </td>
                            <td>
                                <asp:Label ID="lbl_subjects" runat="server" Text='<%# Eval("Pat_Name") %>' />
                            </td>
                            <td>
                                <div class="col-md-6 p-0">
                                    <asp:HiddenField ID="hdnid" runat="server" />
                                    <asp:TextBox ID="TXT_periods" setcss="input-sm" texttype="Numeric" required="true"
                                        maxvalue="90" runat="server" Enabled="false" setfocus="true" setenable="true"
                                        Text='<%# Eval("Pat_Number") %>' />
                                </div>
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
            </tbody>
        </table>
    </div>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
    var arr = new Array();
    function CustomValidation(ele) {
        var txt = $("input[id*='TXT_periods']");
        var chk = $("input[id*='chkSelect']");
        var checkedIndex = $(ele).closest('tr').index();
        if ($(ele).closest('tr').find($("input[id*='chkSelect']")).is(':checked')) {
            arr.push(checkedIndex);
        }
        else {
            arr = arr.filter(function (item) {
                return item !== checkedIndex
            });
        }

        for (var i = 0; i < arr.length; i++) {
            txt[arr[i]].value = i + 1;
            txt[arr[i]].disabled = false;
        }

        for (var i = 0; i < chk.length; i++) {
            if (!chk[i].checked) {
                txt[i].disabled = true;
                txt[i].value = '';
            }
        }
    }
</script>

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        System.Data.DataTable dt = new System.Data.DataTable();
        dt.Columns.AddRange(new System.Data.DataColumn[4] { 
                        new System.Data.DataColumn("Pat_Id", typeof(int)),
                        new System.Data.DataColumn("pat_check", typeof(bool)),
                        new System.Data.DataColumn("Pat_Name",typeof(string)),
                        new System.Data.DataColumn("Pat_Number",typeof(int)) });
        dt.Rows.Add(1, false, "John Hammond", 1);
        dt.Rows.Add(2, false, "Mudassar Khan", 3);
        dt.Rows.Add(3, false, "Suzanne Mathews", 2);
        dt.Rows.Add(4, false, "Robert Schidner", 4);
        rpt_SubsSetting.DataSource = dt;
        rpt_SubsSetting.DataBind();
    }
}

Screenshot