Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using the Following code.

What I'm trying to do is if User enters something in txtPaidAmount the txtDeductAmount becomes 0 and if User enters something in txtDeductAmount txtPaidAmount becomes 0 so far If I run it through these TextBoxes it works but Problem arise when I Try to implement it on ListView lblPaidAmount and lblDeductAmount it doesn't work at all.

Can anyone Help me out with this.

Aspx Code.

ASP.NET
<asp:ListView ID="ListView1" runat="server" >
<LayoutTemplate>
          <table class="table" style="text-align:left">
              <th>ID</th>
              <th>AcName</th>
              <th>Naration</th>
              <th>PaidAmount</th>
              <th>DeductionAmount</th>
              <th>Action</th>

                   <tr id="itemPlaceholder"  runat="server"></tr>
                          </table>
                      </LayoutTemplate>

                       <ItemTemplate>
                          <tr>
                              <td>
                                  <asp:Label runat="server" ID="lblID" Text='<%#Bind("ID") %>'></asp:Label>
                              </td>
                              <td>
                                  <%--<asp:Label runat="server" ID="lblAcount" Text='<%#Eval("AcName") %>'></asp:Label>--%>
                                  <asp:TextBox ID="lblAcount" runat="server" CssClass="form-control" Text='<%#Bind("AcName") %>'></asp:TextBox>
                              </td>
                              <td>
                                  <%--<asp:Label runat="server" ID="lblNaration" Text='<%#Eval("Naration") %>'></asp:Label>--%>
                                  <asp:TextBox ID="lblNaration" runat="server" CssClass="form-control" Text='<%#Bind("Naration") %>'></asp:TextBox>
                              </td>
                              <td>
                                  <%--<asp:Label runat="server" ID="lblPaidAmount" Text='<%#Eval("PaidAmount") %>'></asp:Label>--%>
                                  <asp:TextBox ID="lblPaidAmount" runat="server" CssClass="form-control" Text='<%#Bind("PaidAmount") %>' onkeypress="return isNumberKey(event)"></asp:TextBox>
                              </td>
                              <td>
                                  <%--<asp:Label runat="server" ID="lblDeductAmount" Text='<%#Eval("DeductionAmount") %>'></asp:Label>--%>
                                  <asp:TextBox ID="lblDeductAmount" runat="server" CssClass="form-control" Text='<%#Bind("DeductionAmount") %>' onkeypress="return isNumberKey(event)"></asp:TextBox>
                              </td>

                              <td><asp:LinkButton ID="DeleteButton" cssClass="btn btn-info fa fa-trash-o" runat="server" CommandName="DeleteIt"></asp:LinkButton>


                              <%--EDIT IN THIS LINE--%>
                              <%--<asp:LinkButton ID="UpdateButton" cssClass="btn btn-info fa fa-pencil" runat="server" CommandName="UpdateIt"></asp:LinkButton>--%></td>
                              <%--EDIT IN THIS LINE--%>


                              <td>
                              </td>

                              <%--<asp:Panel ID="Panel2" runat="server" Visible="False">
                                  <tr>
                                            <td>
                                                <asp:Label ID="Label12" runat="server" Text="Credit Acount"></asp:Label>
                                                <asp:TextBox ID="txtAcName" runat="server" CssClass="form-control" Text='<%#Bind("AcName") %>'></asp:TextBox>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <asp:Label ID="Label13" runat="server" Text="Naration"></asp:Label>
                                                <asp:TextBox ID="txtNaration" runat="server" CssClass="form-control" Text='<%#Bind("Naration") %>'></asp:TextBox>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <asp:Label ID="Label14" runat="server" Text="Paid Amount"></asp:Label>
                                                <asp:TextBox ID="txtPaidAmount" runat="server" CssClass="form-control" Text='<%#Bind("PaidAmount") %>'></asp:TextBox>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <asp:Label ID="Label15" runat="server" Text="Deduction Amount"></asp:Label>
                                                <asp:TextBox ID="txtDeductAmount" runat="server" CssClass="form-control" Text='<%#Bind("DeductionAmount") %>'></asp:TextBox>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <asp:LinkButton ID="btnUpdateRecord" cssClass="btn btn-info fa fa-check" runat="server" CommandName="UpdateRecord"></asp:LinkButton>
                                                <asp:LinkButton ID="btnCancelUpdate" cssClass="btn btn-info fa fa-close" runat="server" CommandName="CancelUpdate"></asp:LinkButton>
                                            </td>
                                        </tr>                                        
                                    </asp:Panel>--%>
                          </tr>
                      </ItemTemplate>

                      <%--<InsertItemTemplate>--%>
                          <%--<tr id="Tr1"  runat="server">
                            <td>
                            </td>
                            <td>
                                <asp:TextBox ID="txtAcName" runat="server" CssClass="form-control" Text='<%#Eval("AcName") %>'
                                    Width="100px" AutoPostBack="True">Naration</asp:TextBox>
                            </td>
                            <td>
                                <asp:TextBox ID="txtNaration" runat="server" CssClass="form-control" Text='<%#Eval("Naration") %>'
                                    Width="100px">Naration</asp:TextBox>
                            </td>
                            <td>
                                <asp:TextBox ID="txtPaidAmount" runat="server" CssClass="form-control" onkeydown="change()" Text='<%#Eval("PaidAmount") %>'
                                    Width="100px">PaidAmount</asp:TextBox>
                            </td>
                            <td>
                                <asp:TextBox ID="txtDeductAmount" runat="server" CssClass="form-control" onkeydown="change1()" Text='<%#Eval("DeductionAmount") %>'
                                    Width="100px">DeductionAmount</asp:TextBox>
                            </td>
                            <td>
                                <asp:LinkButton ID="InsertButton" runat="server" CommandName="Add" CssClass="btn btn-info">Insert</asp:LinkButton>
                            </td>
                        </tr>--%>

                      <%--</InsertItemTemplate>--%>

                  </asp:ListView>


With this Markup I am adding Row to the Database which is then shown in the Listview.

ASP.NET
<table id="EntryTable">
        <tr id="Tr1"  runat="server">
               <td>
               </td>
               <td>
               </td>
               <td>
               </td>
               <td>
               </td>
               <td>
                <asp:TextBox ID="txtAcName" runat="server" CssClass="form-control" Text=''
                                    Width="100px" PlaceHolder="Acount Name" onkeypress="return isQuotation(event)"></asp:TextBox>
               </td>
                <td>
                 <asp:TextBox ID="txtNaration" runat="server" CssClass="form-control" Text=''
                                    Width="100px" PlaceHolder="Naration" onkeypress="return isQuotation(event)"></asp:TextBox>
                </td>
                <td>
                 <asp:TextBox ID="txtPaidAmount" runat="server" CssClass="form-control" onkeydown="change()" Text=''
                                    Width="100px" PlaceHolder="Paid Amount" onkeypress="return isNumberKey(event)"></asp:TextBox>
                            </td>
                            <td>
                <asp:TextBox ID="txtDeductAmount" runat="server" CssClass="form-control" onkeydown="change1()" Text=''
                                    Width="100px" PlaceHolder="DeductionAmount" onkeypress="return isNumberKey(event)"></asp:TextBox>
                </td>
                <td>
                <asp:LinkButton ID="InsertButton" runat="server" CssClass="btn btn-info">Insert</asp:LinkButton>
                            </td>
                        </tr>

          </table>


The JS I am using to set the next TextBox to 0.

JavaScript
function change() {
            var t1Val = document.getElementById("<%=txtPaidAmount.ClientID %>");
            document.getElementById("<%=txtDeductAmount.ClientID %>").value = 0;
        };

        function change1() {
            var t2Val = (document.getElementById("txtDeductAmount")).value;
            (document.getElementById("txtPaidAmount")).value = 0;
        };

 function isNumberKey(evt) {
            var charCode = (evt.which) ? evt.which : event.keyCode
            if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57))
                return false;

            return true;
        }

        function isQuotation(evt) {
            var charCode = (evt.which) ? evt.which : event.keyCode
            if (charCode == 39)
                return false;

            return true;
        }
Posted
Updated 7-Sep-15 21:40pm
v3
Comments
[no name] 8-Sep-15 2:36am    
Where is your isNumberKey() in javascript. Please provide that.
masterfang 8-Sep-15 3:40am    
I updated the JS section kindly help me out.

1 solution

If your controls are in a ListView and the listview is bound to five items, which of those 5 txtPaidAmount controls do you think you're referring to in this code?

var t1Val = document.getElementById("<%=txtPaidAmount.ClientID %>");

Try something like this;

XML
<asp:ListView runat="server" ID="MyListView" OnItemDataBound="MyListView_ItemDataBound">
    <ItemTemplate>
        Paid: <asp:TextBox ID="txtPaidAmount" runat="server" />
        Deduct: <asp:TextBox ID="txtDeductAmount" runat="server" />
    </ItemTemplate>
</asp:ListView>


JavaScript
<script type="text/javascript">
    function change(paid, deduct) {
        var t1Val = document.getElementById(paid);
        document.getElementById(deduct).value = 0;
    };
</script>


code-behind;

C#
protected void MyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        TextBox txtPaidAmount = (TextBox)e.Item.FindControl("txtPaidAmount");
        TextBox txtDeductAmount = (TextBox)e.Item.FindControl("txtDeductAmount");

        txtPaidAmount.Attributes.Add("onkeydown", string.Format("change('{0}','{1}')", txtPaidAmount.ClientID, txtDeductAmount.ClientID));
    }
}
 
Share this answer
 
Comments
masterfang 8-Sep-15 5:17am    
Hi Thank for your reply, It gives error.

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
F-ES Sitecore 8-Sep-15 5:22am    
On what line?
masterfang 8-Sep-15 5:23am    
txtPaidAmount.Attributes.Add("onkeydown", string.Format("change('{0}','{1}')", txtPaidAmount.ClientID, txtDeductAmount.ClientID));
F-ES Sitecore 8-Sep-15 5:43am    
Works ok for me, are you using my sample markup on a page to test the technique works and to understand why it works?

However I see you have the controls outside the listview too, so you'll need this in your page load event also

protected void Page_Load(object sender, EventArgs e)
{
txtPaidAmount.Attributes.Add("onkeydown", string.Format("change('{0}','{1}')", txtPaidAmount.ClientID, txtDeductAmount.ClientID));

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900