2011-07-21 57 views
0

問:gridview中的模板字段問題

我有以下問題,並且我不知道如何修復它。

我有一個grid view其中一列是template field(文本框)。網格視圖由8行組成。我所做的是每次用戶在文本框中輸入數據時,我將總數放在最後一個文本框中(set enabled = false)。我通過某種方法在文本框中總結數據條目並在事件中調用它text changed。但每當我在文本框中輸入一個數字,然後點擊Tab in the keyboard或使用鼠標光標移動到下一個框時,我就失去了焦點,我必須再次將鼠標光標置於預期的文本框中。

我嘗試了以下方法來解決我的問題,但徒勞無功。

foreach (GridViewRow r in gv_Evaluation.Rows) 
      { 
       ((RadTextBox)r.Cells[3].FindControl("txt_evaluateWeights")).Attributes.Add("blur", "calc()"); 
      } 

在我的頁面加載,這根本不起作用。


protected void txt_evaluateWeights_TextChanged(object sender, EventArgs e) 
{ 
    calc(); 
    ((TextBox)sender).Focus(); 
} 

這樣,焦點返回到前一個文本框(我的意思是,我已經做了一個),文本框不是我想的焦點,輸入數據。

編輯:

我的計算方法:

private void calc() 
     { 
      float sum = 0; 
      for (int i = 0; i < 7; i++) 
      { 
       RadTextBox txt1 = (RadTextBox)gv_Evaluation.Rows[i].Cells[3].FindControl("txt_evaluateWeights"); 
       int weight; 
       bool result = Int32.TryParse(txt1.Text, out weight); 
       if (result) 
       { 
        sum += weight; 
       } 
      } 

      double percentage; 
      percentage = Math.Round((sum/100) * 100, 2); 
      RadTextBox txt3 = (RadTextBox)gv_Evaluation.Rows[7].Cells[3].FindControl("txt_evaluateWeights"); 
      txt3.Text = percentage.ToString();//string.Format("{0:0.0%}", percentage.ToString()); 

     } 
+1

你能發佈你的'calc'功能呢? – naveen

+0

好的............ –

+1

您想回傳並計算服務器端的金額還是想在客戶端進行?另外,您是否考慮過啓用Paging並且某些行根本不存在於客戶端的問題? 「將總數放在最後一個文本框中」是什麼意思,最後一行是TextBox還是f.e.在頁腳? –

回答

1

使用服務器端回傳做,就這樣做的可怕方式。

改爲使用JavaScript。這裏是jQuery的一個小例子

GridView控件

<asp:GridView ID="DemoGrid" runat="server" 
      AutoGenerateColumns="false" 
      ShowFooter="true"> 
    <Columns> 
     <asp:TemplateField HeaderText="index"> 
      <ItemTemplate><%# Container.DataItemIndex + 1 %></ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Item"> 
      <ItemTemplate> 
       <asp:Label ID="DemoLabel" runat="server" Text='<%# Container.DataItem %>' /> 
      </ItemTemplate> 
      <FooterTemplate>Total</FooterTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField HeaderText="Amount"> 
      <ItemTemplate> 
       <asp:TextBox ID="DemoText" runat="server" CssClass="quantity"> 
       </asp:TextBox> 
      </ItemTemplate> 
      <FooterTemplate> 
       <asp:Label ID="TotalLabel" runat="server" CssClass="result"/> 
      </FooterTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

守則

protected void Page_Load(object sender, EventArgs e){ 
    if (!IsPostBack) 
    { 
     string[] array = new string[] { "demo1", "demo2", "demo3", "demo4", "demo5" }; 
     DemoGrid.DataSource = array; 
     DemoGrid.DataBind(); 
    } 
} 

背後的的JavaScript(jQuery的)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script> 
<script type="text/javascript"> 
    $(document).ready(function() { 
     $(".quantity").bind("blur", function() { 
      var $quantity = $(this); 
      var quantity = +$quantity.val(); //cast to number 
      if (!isNaN(quantity)) { 
       var $sum = $quantity.closest("table").find("tr:last .result"); 
       var sum = +$sum.html(); 
       $sum.html(sum + quantity); 
      } 
     }); 
    }); 
</script> 

希望這有助於

相關問題