这个具有FormView Control的Asp.net页面。我正在通过Nortwind数据库产品表填充FormView控件。但我想在formview控件中动态填充标签。所以设计器.cs文件不会随标签更新。还附上了Code。我尝试使用FindControl.but它总是给我Null Exception。
Codebehind文件
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";
}
}
Aspx.page
<!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>
答案 0 :(得分:0)
如果您想修改仅在EditItemTemplate中的内容,而不是使用Databound事件,则应使用ModeChanged事件。这样你可以检查以确保新模式将是“编辑”(所以你知道控件将存在)。
您需要在标记中处理事件:
<asp:FormView ID="ProductsFormView" DataSourceID="ProductsSqlDataSource"
AllowPaging="True" DefaultMode="Insert" runat="server"
DataKeyNames="ProductID" OnDataBound="DataBound"
OnModeChanged="ProductsFormView_ModeChanged">
然后实际的事件代码如下所示:
protected void ProductsFormView_ModeChanged(Object sender, FormViewModeEventArgs e)
{
if(ProductsFormView.CurrentMode == FormViewMode.Edit)
{
Label lblSubmit = (Label)ProductsFormView.FindControl("lblSubmit");
lblSubmit.Text = "Hi!";
}
}
我知道这是一个古老的问题,但我觉得发布答案不会有什么坏处