2012-09-25 73 views
0

我想匹配在我的文本框中從gridview的現有值寫入的值。我曾嘗試使用下面的函數使用javascript在gridview中與數據匹配的文本框值

protected void isRecordAlreadyExist(TextBox txt_Value, int res) 
{ 
    ds_main = new DataSet(); 
    paramArray = new string[3, 2]; 
    paramArray[0, 0] = "@KeyWinCountNumber"; 
    paramArray[0, 1] = txtkeyWinCount.Text.Trim(); 
    paramArray[1, 0] = "@ContractNumber"; 
    paramArray[1, 1] = txtContractNum.Text.Trim(); 
    paramArray[2, 0] = "`"; 
    obj = new DalLib(); 
    ds_main = obj.getDataSet("sp_tbl_Contract_MatchValues", paramArray); 
    gvContract.DataSource = ds_main.Tables[res]; 
    if (ds_main.Tables[res].Rows.Count > 0) 
    { 
     mtvResult.ActiveViewIndex = 3; 
     btnSubmit.Enabled = false; 
    } 
    else 
    { 
     btnSubmit.Enabled = true; 
    } 
} 

protected void txtkeyWinCount_TextChanged(object sender, EventArgs e) 
{ 
    if (!string.IsNullOrEmpty(txtkeyWinCount.Text)) 
    { 
     isRecordAlreadyExist(txtkeyWinCount, 0); 
    } 
    else 
    { 
     mtvResult.ActiveViewIndex = -1; 
    } 
} 

這樣做在服務器端,但我也想文本框的值從gridview的數據在客戶端使用JavaScript匹配。 我該怎麼做?任何幫助,將不勝感激。

回答

0

如果發現它返回true,則可以在函數中搜索網格中的TextBox值之後,編寫類似下面的內容。

您可以撥打文本框的onchange JavaScript事件此功能

function FindTextBoxValueInGrid 
    { 
    // Declare the flag 
    var flag = false; 
    // Get the text we want to compare in the table 
    var checkText = document.getElementById('txtkeyWinCount').value; 

    // Get the grid 
    var gvContract= document.getElementById('gvContract'); 

    // Loop through each row 
    // Starts at row 1 because I assume row 0 is a header row 
    for (i=1;i<=gvContract.rows.length;i++) 
    { 
     var currentRow = gvContract.rows[i]; 

     if (currentRow.cells[0].value == checkText) 
     { 
      flag = true; 
     } 
    } 
    return flag; 
} 
相關問題