user239684
user239684

Reputation: 349

asp.net gridview textbox focus using javascript

i am developing a asp.net application where i have a content page derived from master page and inside the page i have a gridview control where i have some bound fields and a textbox to take value and calculate the remaining. I am using the following javascript code.

     <td colspan="4">
   <div id="divGrid" style="width: 890px; height: 200px; overflow: auto">
      <asp:GridView ID="CustomerGrid" runat="server" BackColor="White" AutoGenerateColumns="False"
          BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" 
          GridLines="Horizontal" Width="920px">
          <RowStyle BackColor="White" ForeColor="#333333" />
         <%-- <HeaderStyle CssClass="HeaderFreeze" />--%>
          <Columns>
              <asp:BoundField DataField="Ref_No" HeaderText="Deal/Transfer Ref # " >
                  <HeaderStyle Font-Names="Verdana" Font-Size="11px" />
                  <ItemStyle Font-Names="Verdana" Font-Size="11px" Width="180px" ForeColor="Blue" Font-Bold="true" />
              </asp:BoundField>
              <asp:BoundField DataField="Settlement_Date" HeaderText="Settlement Date" >
                  <HeaderStyle Font-Names="Verdana" Font-Size="11px" />
                  <ItemStyle Font-Names="Verdana" Font-Size="11px" />
              </asp:BoundField>
              <asp:BoundField DataField="Settlement_Amount" HeaderText="Settlement Amt" >
                  <HeaderStyle Font-Names="Verdana" Font-Size="11px" />
                  <ItemStyle Font-Names="Verdana" Font-Size="11px" />
              </asp:BoundField>
              <asp:BoundField DataField="Interest_Rate" HeaderText="Interest Rate" >
                  <HeaderStyle Font-Names="Verdana" Font-Size="11px" />
                  <ItemStyle Font-Names="Verdana" Font-Size="11px" />
              </asp:BoundField>
              <asp:BoundField DataField="PDealerName" HeaderText="Primary Dealer" >
                  <HeaderStyle Font-Names="Verdana" Font-Size="11px" />
                  <ItemStyle Font-Names="Verdana" Font-Size="11px" />
              </asp:BoundField>
              <asp:BoundField DataField="PD_Price" HeaderText="PD Price" >
                  <HeaderStyle Font-Names="Verdana" Font-Size="11px" />
                  <ItemStyle Font-Names="Verdana" Font-Size="11px" />
              </asp:BoundField>

              <asp:TemplateField HeaderText="FaceValue">
                 <ItemTemplate>
                     <asp:Label ID="LBLFaceValue" runat="server" Text='<%# Eval("Face_Value") %>'></asp:Label>
                 </ItemTemplate>
                 <HeaderStyle Font-Names="Verdana" Font-Size="11px" />
                  <ItemStyle Font-Names="Verdana" Font-Size="11px" />
              </asp:TemplateField>                  

             <%-- <asp:BoundField DataField="Face_Value" HeaderText="Face Value" >
                  <HeaderStyle Font-Names="Verdana" Font-Size="11px" />
                  <ItemStyle Font-Names="Verdana" Font-Size="11px" />
              </asp:BoundField>--%>

              <asp:BoundField DataField="Available" HeaderText="Available" >
                  <HeaderStyle Font-Names="Verdana" Font-Size="11px" />
                  <ItemStyle Font-Names="Verdana" Font-Size="11px" />
              </asp:BoundField>
              <asp:TemplateField HeaderText="Value">
              <ItemTemplate>
                  <asp:TextBox ID="txtValue" runat="server" Width="100px" onblur="CalculateTotals();"></asp:TextBox>
              </ItemTemplate>
                  <HeaderStyle Font-Names="Verdana" Font-Size="11px" />
                  <ItemStyle Font-Names="Verdana" Font-Size="11px" />

              </asp:TemplateField>
              <asp:TemplateField HeaderText="Remaining">
                  <ItemTemplate>
                      <asp:Label ID="lblRemaining" runat="server" Text=""></asp:Label>
                  </ItemTemplate>
                  <HeaderStyle Font-Names="Verdana" Font-Size="11px" />
                  <ItemStyle Font-Names="Verdana" Font-Size="11px" />
              </asp:TemplateField>
               <asp:BoundField DataField="Transaction_Type" HeaderText="T" Visible="false">
                  <HeaderStyle Font-Names="Verdana" Font-Size="11px" />
                  <ItemStyle Font-Names="Verdana" Font-Size="11px" />
              </asp:BoundField>
          </Columns>
          <FooterStyle BackColor="White" ForeColor="#333333" />
          <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
          <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
          <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
      </asp:GridView>
      </div>
      &nbsp;</td>
<script type="text/javascript">

function CalculateTotals() {
    var gv = document.getElementById("<%= CustomerGrid.ClientID %>");
    var tb = gv.getElementsByTagName("input");
    var lb = gv.getElementsByTagName("span");

    var sub = 0;
    var total = 0;
    var indexQ = 1;
    var indexP = 0;

    for (var i = 0; i < tb.length; i++) {
        if (tb[i].type == "text") {
            sub = parseFloat(lb[indexP].innerHTML) - parseFloat(tb[i].value);
            if (sub < 0) 
            {
                alert("Exceeding from the face value...");
                return;
                tb[i].focus();
                //return;
            }

            if (isNaN(sub)) {
                lb[i + indexQ].innerHTML = "";
                sub = 0;
            }
            else {
                lb[i + indexQ].innerHTML = sub;
            }

            indexQ++;
            indexP = indexP + 2;

            total += parseFloat(sub);
        }
    }
}

 </script>

Problem is that I have a condition that input value cannot exceed from Face Value..if it exceeds it shows an alert..I want to show error and the focus should be back to the textbox control of that particular row. Somehow its not setting the focus back the textbox control of the particular row.

just check the area where i need modification.

if (sub < 0) 
        {
            alert("Exceeding from the face value...");
            return;
            tb[i].focus();
            //return;
        }

Any suggestions?

Upvotes: 0

Views: 7963

Answers (1)

Josiah Ruddell
Josiah Ruddell

Reputation: 29831

The return is before the call to focus. Focus will never be called the way the code currently is.

alert("Exceeding from the face value...");
return;
tb[i].focus();

Should be

alert("Exceeding from the face value...");
tb[i].focus();
return;

Upvotes: 1

Related Questions