访问 GridView 控件内的不同控件






3.41/5 (29投票s)
2005年10月20日
1分钟阅读

434459
如何访问 GridView 控件内的不同控件。
引言
我收到很多邮件询问如何访问位于 GridView
控件内的特定控件。 在本文中,我将向您展示如何访问 GridView
控件内的不同控件。 我们将看到如何访问 TextBox
控件、DropDownList
控件和 ListBox
控件。 如果您正在使用 ASP.NET 1.X,那么您可能需要查看我的文章 访问 DataGrid 控件内的不同控件。
将控件添加到 GridView 控件
您可以通过简单地使用 <ItemTemplate>
选项将多个控件添加到 GridView
控件。
填充 ListBox 和 DropDownList
接下来的任务是填充 ListBox
和 DropDownList
控件。 让我们创建一个简单的服务器端方法来填充 ListBox
和 DropDownList
。
C# 代码
// This method populates the DropDownList and the ListBox control
public DataSet PopulateControls()
{
SqlConnection myConnection = new SqlConnection(GetConnectionString());
SqlDataAdapter ad = new SqlDataAdapter("SELECT [Name] FROM tblPerson",
myConnection);
DataSet ds = new DataSet();
ad.Fill(ds, "tblPerson");
return ds;
}
VB.NET 代码
' This method populates the DropDownList and the ListBox control
Public Function PopulateControls() As DataSet
Dim myConnection As SqlConnection = New SqlConnection(GetConnectionString())
Dim ad As SqlDataAdapter = New SqlDataAdapter("SELECT " & _
"[Name] FROM tblPerson",myConnection)
Dim ds As DataSet = New DataSet()
ad.Fill(ds, "tblPerson")
Return ds
End Function
现在我们需要在 HTML 视图中绑定此方法。 请查看 DropDownList
的以下代码,您可以对 ListBox
控件重复相同的过程。
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" DataTextField="Name"
DataValueField = "Name" DataSource= '<%# PopulateControls() %>' runat="server">
</asp:DropDownList>
</ItemTemplate>
现在您的 DropDownList
和 ListBox
控件已填充了一些数据。 现在让我们看看如何访问 GridView
内部的不同控件。
访问 GridView 控件内的不同控件
在 Button
点击事件中,我们将尝试打印出输入(TextBox
)或选择(DropDownList
和 ListBox
)的值。 让我们看看如何做到这一点。
C# 代码
protected void Button1_Click(object sender, EventArgs e)
{
// Iterates through the rows of the GridView control
foreach (GridViewRow row in GridView1.Rows)
{
// Selects the text from the TextBox
// which is inside the GridView control
string textBoxText = _
((TextBox)row.FindControl("TextBox1")).Text;
Response.Write(textBoxText);
// Selects the text from the DropDownList
// which is inside the GridView control
string dropDownListText = ((DropDownList)
row.FindControl("DropDownList1")).SelectedItem.Value;
Response.Write(dropDownListText);
// Selects items from the ListBox
// which is inside the GridView control
ListBox myListBox = (ListBox)row.FindControl("ListBox1");
foreach(ListItem selectedItem in myListBox.Items)
{
// Checks if the item in the ListBox is selected or not
if (selectedItem.Selected)
{
// Print the value of the item if its selected
Response.Write(selectedItem.Value);
}
}
}
VB.NET 代码
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
' Iterates through the rows of the GridView control
For Each row As GridViewRow In GridView1.Rows
' Selects the text from the TextBox
' which is inside the GridView control
Dim textBoxText As String = _
CType(row.FindControl("TextBox1"),TextBox).Text
Response.Write(textBoxText)
' Selects the text from the DropDownList
' which is inside the GridView control
Dim dropDownListText As String = _
CType(row.FindControl("DropDownList1"), _
DropDownList).SelectedItem.Value
Response.Write(dropDownListText)
' Selects items from the ListBox
' which is inside the GridView control
Dim myListBox As ListBox = _
CType(row.FindControl("ListBox1"),ListBox)
For Each selectedItem As ListItem In myListBox.Items
' Checks if the item in the ListBox is selected or not
If selectedItem.Selected Then
' Print the value of the item if its selected
Response.Write(selectedItem.Value)
End If
Next
Next
End Sub
我们在上面的代码中所做的一切就是使用 GridViewRow
对象遍历 GridView
控件的所有行。 接下来,我们使用 FindControl
方法找到控件并打印出控件的值。
希望您喜欢这篇文章,祝您编码愉快!