这个具有 FormView 控件的 Asp.net 页面。我正在通过 Nortwind 数据库产品表填充 FormView 控件。但我想在 formview 控件中动态填充标签。所以设计器 .cs 文件不会使用标签更新。还附加背后的代码。我尝试使用 FindControl。但它总是给我空异常。
代码隐藏文件
protected void DataBound(object sender, EventArgs e)
{
if (ProductsFormView.CurrentMode == FormViewMode.Insert)
{
TextBox ProductNameTextBox = ProductsFormView.FindControl("ProductNameTextBox1") as TextBox;
ProductNameTextBox.Text = "Hello";
Label lblSubmit = ProductsFormView.FindControl("lblSubmit") as Label;
lblSubmit.Text = "HI";
}
}
页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>FormView Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>FormView Example</h3>
<table cellspacing="10">
<tr>
<td valign="top">
<asp:FormView ID="ProductsFormView"
DataSourceID="ProductsSqlDataSource"
AllowPaging="True"
DefaultMode="Insert"
runat="server" DataKeyNames="ProductID" OnDataBound="DataBound">
<EditItemTemplate>
ProductID:
<asp:Label ID="ProductIDLabel1" runat="server" Text='<%# Eval("ProductID") %>' />
<br />
ProductName:
<asp:TextBox ID="ProductNameTextBox" runat="server" Text='<%# Bind("ProductName") %>' />
<br />
CategoryID:
<asp:TextBox ID="CategoryIDTextBox" runat="server" Text='<%# Bind("CategoryID") %>' />
<br />
QuantityPerUnit:
<asp:TextBox ID="QuantityPerUnitTextBox" runat="server" Text='<%# Bind("QuantityPerUnit") %>' />
<br />
UnitPrice:
<asp:TextBox ID="UnitPriceTextBox" runat="server" Text='<%# Bind("UnitPrice") %>' />
<br />
<asp:TextBox ID="ProductNameTextBox1" runat="server"></asp:TextBox>
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
<asp:Label ID="lblSubmit" runat="server" Text=" "></asp:Label>
</EditItemTemplate>
<HeaderStyle forecolor="white" backcolor="Blue" />
<InsertItemTemplate>
ProductName:
<asp:TextBox ID="ProductNameTextBox" runat="server" Text='<%# Bind("ProductName") %>' />
<br />
CategoryID:
<asp:TextBox ID="CategoryIDTextBox" runat="server" Text='<%# Bind("CategoryID") %>' />
<br />
QuantityPerUnit:
<asp:TextBox ID="QuantityPerUnitTextBox" runat="server" Text='<%# Bind("QuantityPerUnit") %>' />
<br />
UnitPrice:
<asp:TextBox ID="UnitPriceTextBox" runat="server" Text='<%# Bind("UnitPrice") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
ProductID:
<asp:Label ID="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>' />
<br />
ProductName:
<asp:Label ID="ProductNameLabel" runat="server" Text='<%# Bind("ProductName") %>' />
<br />
CategoryID:
<asp:Label ID="CategoryIDLabel" runat="server" Text='<%# Bind("CategoryID") %>' />
<br />
QuantityPerUnit:
<asp:Label ID="QuantityPerUnitLabel" runat="server" Text='<%# Bind("QuantityPerUnit") %>' />
<br />
UnitPrice:
<asp:Label ID="UnitPriceLabel" runat="server" Text='<%# Bind("UnitPrice") %>' />
<br />
</ItemTemplate>
<PagerTemplate>
<table>
<tr>
<td><asp:LinkButton ID="FirstButton" CommandName="Page" CommandArgument="First" Text="<<" RunAt="server"/></td>
<td><asp:LinkButton ID="PrevButton" CommandName="Page" CommandArgument="Prev" Text="<" RunAt="server"/></td>
<td><asp:LinkButton ID="NextButton" CommandName="Page" CommandArgument="Next" Text=">" RunAt="server"/></td>
<td><asp:LinkButton ID="LastButton" CommandName="Page" CommandArgument="Last" Text=">>" RunAt="server"/></td>
</tr>
</table>
</PagerTemplate>
</asp:FormView>
</td>
</tr>
</table>
<asp:SqlDataSource ID="ProductsSqlDataSource"
SelectCommand="SELECT [ProductID], [ProductName], [CategoryID], [QuantityPerUnit], [UnitPrice] FROM [Products]"
connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
RunAt="server"/>
</form>
</body>
</html>