2012-10-17 36 views
-1

我有一個gridview有兩列。我想檢查第一行和第一列應該是強制性的。如果第一列爲空,則不應允許保存並且應顯示消息爲如何檢查使用asp.net的網格視圖的第一行和列

一行應該是強制性的。

如何檢查?

我的保存按鈕點擊編碼是如下

Dim row As GridViewRow 
For Each row In GridView1.Rows 
If row.RowType = DataControlRowType.DataRow Then 
    Dim t1 As String = CType(row.FindControl("TextBox2"), TextBox).Text 
    Dim dd As String = CType(row.FindControl("DropDownList3"), DropDownList).SelectedItem.Text 
    If Trim(t1) = "" Or (Trim(dd)= "" then 
     label6.Text = "HospNo,Date,SurgeryCode/Method are Mandatory" 
    End If 

但這種檢查所有網格列,我想只檢查第一行和列。

+0

你應該告訴我們你的'GridView'和[你試過什麼(http://mattgemmell.com/2008/12/08/what-have-you-試圖/)。 –

+0

我的保存按鈕點擊編碼是如下 昏暗行作爲GridViewRow 對於每行中GridView1.Rows 如果row.RowType = DataControlRowType.DataRow然後 昏暗T1作爲字符串= CTYPE(row.FindControl(「TextBox2中」),文本框).Text dim dd As String = CType(row.FindControl(「DropDownList3」),DropDownList).SelectedItem.Text if Trim(t1)=「」Or(Trim(dd)=「」then label6.Text = 「HospNo,日期,手術代碼/方法是強制性的」 結束如果但這檢查所有網格列我想檢查只有第一行和第列 – user1702346

+0

有一個編輯按鈕(已經爲你完成) –

回答

0
For Each row In GridView1.Rows 
If row.RowType = DataControlRowType.DataRow Then 
Dim t1 As String = CType(row.FindControl("TextBox2"), TextBox).Text 
Dim dd As String = CType(row.FindControl("DropDownList3"),DropDownList).SelectedItem.Text 
If Trim(t1) = "" Or (Trim(dd)= "" then 
    label6.Text = "HospNo,Date,SurgeryCode/Method are Mandatory" 
End If 
Exit For 
End For 
+0

你可以給我完全的想法,我didnt立即回覆感謝 – user1702346

+0

@ user1702346,代碼已更新特德檢查它 – andy

+0

在我的網格我顯示2行,在這我想檢查第一行和第一列,然後保存請幫助我DIS是vry arjent – user1702346

0

您可以使用GridView1.Rows(0)得到的第一行:

Dim firstRow = GridView1.Rows(0) 
Dim TextBox2 = DirectCast(firstRow.FindControl("TextBox2"), TextBox) 
Dim DropDownList3 = DirectCast(firstRow.FindControl("DropDownList3"), DropDownList) 
If TextBox2.Text.Trim.Length = 0 OrElse DropDownList3.SelectedIndex = -1 Then 
    label6.Text = "HospNo,Date,SurgeryCode/Method are Mandatory" 
End If 

但是,這不是你應該在ASP.NET中使用的方法。而是使用RequiredFieldValidator,它可以在客戶端進行驗證。

例如:

<asp:GridView ID="GridView1" OnSelectedIndexChanged="Grid1_SelectedIndexChanged" AutoGenerateSelectButton="true" AutoGenerateColumns="false" runat="server" Width="300"> 
     <RowStyle CssClass="GridViewRowStyle" /> 
     <AlternatingRowStyle CssClass="GridViewAlternatingRowStyle" /> 
     <HeaderStyle CssClass="GridViewHeaderStyle" /> 
     <SelectedRowStyle BackColor="Aqua" /> 
     <Columns> 
      <asp:TemplateField HeaderText="Column 1" HeaderStyle-HorizontalAlign="Left"> 
       <ItemTemplate> 
        <asp:TextBox runat="server" ID="TxtColumn1" Text='<%# Bind("Column1") %>'></asp:TextBox> 
        <asp:RequiredFieldValidator id="RequiredFieldValidator1" 
         EnableClientScript="true" 
         ControlToValidate="TxtColumn1" 
         Display="Static" 
         ErrorMessage="HospNo,Date,SurgeryCode/Method are Mandatory" 
         runat="server"/> 
       </ItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
    </asp:GridView> 
+0

我試過,但爲行Dim firstRow = GridView1.Rows.Cast(Of GridViewRow )。首先顯示錯誤'cast'不是'System.Web.UI.WebControls.GridViewRowCollection'的成員' – user1702346

+0

您使用哪個框架?從.NET 3.5開始,它就在那裏。 http://msdn.microsoft.com/en-us/library/bb341406(v=vs.90).aspx –

+0

框架1.1請幫助我 – user1702346

相關問題