2017-02-24 79 views
0

TextBox控件设置焦点我有文本框更改事件列表视图i循环通ListView和繁殖文本框值1到文本框2和秀textbox3它的工作原理确定内部ListView控件和文本框,我想在文本框,当用户按tab 1所以它去了文本框2,但它没有发生。里面的ListView

这里是我的代码的html和页面背后

<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" 
     DataKeyNames="CategoryId"> 

    <ItemTemplate> 

    <%-- //ajax update panel to have asyn call--%> 
     <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
    <ContentTemplate> 
     <table style="width: 100%;"> 
      <tr> 
       <td> 
        Item: <asp:Label ID="Label4" runat="server" Text='<%# Eval("CategoryName") %>'></asp:Label> &nbsp; &nbsp; 
       </td> 
       <td> 
        &nbsp; 
        QTY: <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" CssClass="qty" ontextchanged="TextBox1_TextChanged" Text="0" ></asp:TextBox> 
       </td> 
       <td> 
        &nbsp; 
        Item Price: <asp:TextBox ID="TextBox2" runat="server" AutoPostBack="true" ontextchanged="TextBox2_TextChanged" Text="0" ></asp:TextBox> 
       </td> 

       <td> 
        &nbsp; 
       Total:(QTY X Item Price) <asp:TextBox ID="TextBox3" runat="server" Enabled='false'></asp:TextBox> 
       </td> 
      </tr> 
      </table> 
      </ContentTemplate> 
      <%--// trigger to call textbox change event on asyn call--%> 
      <Triggers > 
     <asp:AsyncPostBackTrigger ControlID ="TextBox1" EventName ="TextChanged" /> 
     <asp:AsyncPostBackTrigger ControlID ="TextBox2" EventName ="TextChanged" /> 
     </Triggers> 
    </asp:UpdatePanel> 
    </ItemTemplate> 

    </asp:ListView> 

页背后

public void cal() 
    { 
     //loop thru listview 
     foreach (ListViewItem item in ListView1.Items) 
     { 

      //make vairables and get control value inside listview in variable to be call for calculation 
      TextBox QTY = (TextBox)item.FindControl("TextBox1"); 
      TextBox ItemPrice = (TextBox)item.FindControl("TextBox2"); 

      TextBox TotalPrice = (TextBox)item.FindControl("TextBox3"); 

      if (QTY.Text != "0" && ItemPrice.Text != "0" && QTY.Text != string.Empty && ItemPrice.Text != string.Empty) 
      { 
       // make int or decimal variable to multiply QTY and Item Price textbox value == i conver both QTY and Item Price textbox to int using int.parse method 
       int totalprice = int.Parse(QTY.Text) * int.Parse(ItemPrice.Text); 

       TotalPrice.Text = totalprice.ToString(); 


      } 




     } 



    } 
+0

如果有解决方案,通过javasript或jquery将textbox1与textbox12相乘,并显示在listbox中的textbox3也是受欢迎的主要问题是设置下一个文本框的焦点选项卡上按下谢谢 – user3597236

回答

0

与您的代码,TextBox1的TextBox2中的每一次用户的变化值,然后按选项卡中,页面将被提交并且页面的内容将被更新。所以TextBox2中不能获得焦点的时候TextBox1的和压片用户的变化值。

你可以转换计算jQuery函数和约束它来改变你的文本框的事件是这样

$(document).ready(function() { 
    $(".quantity,.price").change(function(e) { 
     var container = $(e.currentTarget).closest("table"); 
     var quantity = $(".quantity", container).val(); 
     var price = $(".price", container).val(); 
     if ($.isNumeric(quantity) && $.isNumeric(price) 
      && parseInt(quantity) != 0 && parseInt(price) != 0) { 
      var total = parseInt(quantity) * parseInt(price); 
      $(".total", container).val(total); 
     } 
    }); 
}); 

不要忘记删除AsyncPostBackTrigger时,文本框的值改为禁用回发。

<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" 
     DataKeyNames="CategoryId"> 
    <ItemTemplate> 
     <ContentTemplate> 
      <table style="width: 100%;"> 
       <tr> 
        <td> 
         Item: <asp:Label ID="Label4" runat="server" Text='<%# Eval("CategoryName") %>'></asp:Label> &nbsp; &nbsp; 
        </td> 
        <td> 
         &nbsp; 
         QTY: <asp:TextBox ID="TextBox1" runat="server" CssClass="quantity" Text="0" ></asp:TextBox> 
        </td> 
        <td> 
         &nbsp; 
         Item Price: <asp:TextBox ID="TextBox2" runat="server" CssClass="price" Text="0" ></asp:TextBox> 
        </td> 

        <td> 
         &nbsp; 
        Total:(QTY X Item Price) <asp:TextBox ID="TextBox3" runat="server" CssClass="total" Enabled='false'></asp:TextBox> 
        </td> 
       </tr> 
      </table> 
     </ContentTemplate> 
    </ItemTemplate> 
</asp:ListView> 

我在这个链接https://jsfiddle.net/oomsvgk0/1/创建了样本,你可以检查它。

+0

我的文本框是在asp网内的listview将你的代码与列表视图中的文本框工作请咨询 – user3597236

+0

我已经更新了脚本,使其与列表视图中的控件一起工作。主要更新是我已经改变它使用类选择器,而不是控件的id。请检查。 –

+0

在其设置焦点在选项卡按下但不执行计算 – user3597236