2013-01-14 77 views
1

我有一個網格視圖,我想用JavaScript來計算在文本框中輸入的值。如何從視圖中獲取用於網格視圖的客戶端ID以便在javascript中使用

我在onrowcreated函數的文本框中添加了onkeyup,它工作正常。

然後我把gridview放在一個multiview,它停止工作。

這是我的JavaScript函數:

function margin1(rowIndex, price, gridId) { 
     var grid = document.getElementById(gridId); 
     var volumeQuota = grid.rows[rowIndex].cells[2].innerText; 
     alert(volumeQuota); 
     var coef = grid.rows[rowIndex].cells[5].childNodes.item(1).value; 
     alert(coef); 
     var prevSites = grid.rows[rowIndex].cells[4].innerText;; 
     grid.rows[rowIndex].cells[6].childNodes.item(1).value = parseFloat(coef) * (parseFloat(volumeQuota) - parseFloat(prevSites)); 
     grid.rows[rowIndex].cells[7].childNodes.item(1).value = price; 

    } 

在這背後的代碼是如何即時添加。

  if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      TextBox t1 = (TextBox)e.Row.FindControl("p98Margin1"); 
      t1.Attributes.Add("onkeyup", 
       string.Format("javascript:margin1('{0}', {1}, {2})", e.Row.RowIndex + 2,  a98.Text , GridView1.ClientID)); 

當我提醒的JavaScript函數Gridview1.clientId我越來越[objectHTMLTableElement]

回答

1

使用本

t1.Attributes.Add("onkeyup", 
      string.Format("javascript:margin1('{0}', '{1}', '{2})'", e.Row.RowIndex + 2,  a98.Text , GridView1.ClientID)); 

我認爲這應該工作。
我認爲價值應該在單引號'馬克。

+0

感謝它的工作,但像這樣margin1('{0}','{1}','{2}')「, – user410911

+0

,非常歡迎您:) –

0

就像你說過的,你把你的網格放在一個多視圖中,一切都停止了,這意味着你的gridview已經被埋沒了,你需要進一步鑽取以進一步揭示它。

做這個

GridView myGridView=(GridView)(MultiView1.FindControl("GridView1")); 

現在你已經找到了它優雅地引用其ID

if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     TextBox t1 = (TextBox)e.Row.FindControl("p98Margin1"); 
     t1.Attributes.Add("onkeyup", 
      string.Format("javascript:margin1('{0}', {1}, {2})", e.Row.RowIndex + 2,  a98.Text ,myGridView.ClientID)); 
    } 

希望這有助於。

+0

感謝您的幫助 – user410911

相關問題