您可以使用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
which can ven validate 在客户端。
例如:
<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>