Эта страница Asp.net, которая имеет FormView Control. Я заполняю элемент управления FormView таблицей продуктов базы данных Nortwind. Но я хотел заполнить динамическую метку внутри элемента управления formview. Дизайнер .cs файл не будет обновляться с помощью метки. Также прилагается код позади. Я пытался с FindControl.but, он всегда дает мне Null Exception.Dynamicaly populate Label под Asp .net Formview Control
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>
По какой причине вы используете элементы InsertItemTemplate? Я думаю, вам нужно использовать ProductNameLabel, который находится в шаблоне элемента. – robot
Я хотел заполнить этот текст ярлыка динамически –
user2500796
даже я проверяю весь режим но я не могу найти метку. Он всегда возвращается как Null (FormViewMode.ReadOnly, режим редактирования, режим вставки) – user2500796