asp.net gridview textbox focus using javascript
<p>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.</p>

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

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

             &lt;%-- &lt;asp:BoundField DataField="Face_Value" HeaderText="Face Value" &gt;
                  &lt;HeaderStyle Font-Names="Verdana" Font-Size="11px" /&gt;
                  &lt;ItemStyle Font-Names="Verdana" Font-Size="11px" /&gt;
              &lt;/asp:BoundField&gt;--%&gt;

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

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

function CalculateTotals() {
    var gv = document.getElementById("&lt;%= CustomerGrid.ClientID %&gt;");
    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 &lt; tb.length; i++) {
        if (tb[i].type == "text") {
            sub = parseFloat(lb[indexP].innerHTML) - parseFloat(tb[i].value);
            if (sub &lt; 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);
        }
    }
}

 &lt;/script&gt;
</code></pre>

<p>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. </p>

<p>just check the area where i need modification.</p>

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

<p>Any suggestions?</p>
