2012-05-11 14 views
3

如何在按鈕點擊申請文本框爲空驗證內部在JavaScript GridView的?我有 包含2個文本框,並在每個row.I保存按鈕要驗證相應保存按鈕點擊文本框一個gridview。如何應用文本框空白驗證按鈕點擊asp.net中的gridview內使用javascript?

我已經應用了邏輯,但問題是,它只適用於硬編碼的textBox標識。如何修改此代碼以便它可以用於所有gridview行?

function gvValidate() { 

var grid = document.getElementById('<%= GridViewCTInformation.ClientID %>'); 
if(grid!=null) 
    { 
    var Inputs = grid.getElementsByTagName("input"); 
    for(i = 0; i < Inputs.length; i++) 
    { 
     if(Inputs[i].type == 'text') 
     { 
      if (Inputs[i].id == 'ctl00_contentPlaceHolderSubScreen_GridViewCTInformation_ctl02_TextBoxCTTermCode') 
      { 
       if (Inputs[i].value == "") { 
        alert("Enter values,blank is not allowed"); 
        return false; 
       } 

      } 
      else if (Inputs[i].id == 'ctl00_contentPlaceHolderSubScreen_GridViewCTInformation_ctl02_TextBoxCTTermDesc') { 
       if (Inputs[i].value == "") { 
        alert("Enter values,blank is not allowed"); 
        return false; 
       } 
      } 

     } 
    } 
    return true; 
} 

}

Protected Sub GridViewTaxInformation_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewTaxInformation.RowDataBound 
    Try 
     If e.Row.RowType = DataControlRowType.DataRow Then 

      Dim btnSave As Button = DirectCast(e.Row.FindControl("ButtonSave"), Button) 
      btnSave.Attributes.Add("onclick", "return gvValidate()") 
     End If 
    Catch ex As Exception 
     Common.WriteLog(ex.Message) 
     Common.WriteLog((ex.StackTrace)) 
     Response.Redirect("..\Errors.aspx", False) 
    End Try 
End Sub 
+0

你做什麼都行的意思? –

+0

其實我有GridView,其中每行有2個文本框和一個保存按鈕。我想驗證保存按鈕被點擊的特定記錄。 – Priyansh

+0

你爲什麼不使用驗證控制,如:使用RequiredFieldValidator –

回答

2

最後我得到了我problem..I的解決方案剛剛通過的GridView的行指數爲JavaScript功能。

下面的代碼

function gvValidate(rowIndex) { 

var grid = document.getElementById('<%= GridViewCTInformation.ClientID %>'); 
if(grid!=null) { 
    var Inputs = grid.rows[rowIndex + 1].getElementsByTagName("input"); 
    for(i = 0; i < Inputs.length; i++) 
    { 
     if(Inputs[i].type == 'text') 
     { 
        if (Inputs[i].value == "") { 
        alert("Enter values,blank is not allowed"); 
        return false; 
       } 

     } 
    } 
    return true; 
} 

}

Protected Sub GridViewCTInformation_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewCTInformation.RowDataBound 
    Try 
     If e.Row.RowType = DataControlRowType.DataRow Then 
      Dim btnSave As Button = DirectCast(e.Row.FindControl("ButtonCTInfoSave"), Button) 
      btnSave.Attributes.Add("onclick", "return gvValidate(" + e.Row.RowIndex.ToString() + ")") 
     End If 
    Catch ex As Exception 
     Common.WriteLog(ex.Message) 
     Common.WriteLog((ex.StackTrace)) 
     Response.Redirect("..\Errors.aspx", False) 
    End Try 
End Sub 
+0

如果您的答案是正確的,請接受它,以便將來的用戶知道。 – Yatrix

1

不檢查ID。只需檢查空白值。

if(Inputs[i].type == 'text') 
{ 

      if (Inputs[i].value == "") { 
       alert("Enter values,blank is not allowed"); 
       return false; 
      } 

    } 
+0

感謝烏爾answer..But將不必要的檢查所有的輸入控件,如果我想驗證只有文本框,其保存按鈕被點擊了什麼? – Priyansh

+0

您可以從按鈕對象中獲取該行的引用,然後在該行中檢查輸入框。 (我相信兩者都在同一行)如果你發佈輸出的HTML文件,這將有助於瞭解網格的DOM結構。 –

0
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" ControlToValidate="the name of your textbox to be validated" ForeColor="Red"></asp:RequiredFieldValidator> 

嘗試可以幫助ü您可以使用驗證控件驗證任何輸入

相關問題