Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamicaly populate Label under Asp .net Formview Control
    text
    copied!<p>This Asp.net page which has FormView Control.I am populating the FormView control by Nortwind database product table.But i wanted to populate a label dynamicaly inside the formview control.so the designer .cs file will not updated with the label .Also attached the Code behind.I tried with FindControl.but it always gives me Null Exception.</p> <p>Codebehind file</p> <pre><code>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"; } } </code></pre> <p><strong>Aspx.page</strong></p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" &gt; &lt;head id="Head1" runat="server"&gt; &lt;title&gt;FormView Example&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;h3&gt;FormView Example&lt;/h3&gt; &lt;table cellspacing="10"&gt; &lt;tr&gt; &lt;td valign="top"&gt; &lt;asp:FormView ID="ProductsFormView" DataSourceID="ProductsSqlDataSource" AllowPaging="True" DefaultMode="Insert" runat="server" DataKeyNames="ProductID" OnDataBound="DataBound"&gt; &lt;EditItemTemplate&gt; ProductID: &lt;asp:Label ID="ProductIDLabel1" runat="server" Text='&lt;%# Eval("ProductID") %&gt;' /&gt; &lt;br /&gt; ProductName: &lt;asp:TextBox ID="ProductNameTextBox" runat="server" Text='&lt;%# Bind("ProductName") %&gt;' /&gt; &lt;br /&gt; CategoryID: &lt;asp:TextBox ID="CategoryIDTextBox" runat="server" Text='&lt;%# Bind("CategoryID") %&gt;' /&gt; &lt;br /&gt; QuantityPerUnit: &lt;asp:TextBox ID="QuantityPerUnitTextBox" runat="server" Text='&lt;%# Bind("QuantityPerUnit") %&gt;' /&gt; &lt;br /&gt; UnitPrice: &lt;asp:TextBox ID="UnitPriceTextBox" runat="server" Text='&lt;%# Bind("UnitPrice") %&gt;' /&gt; &lt;br /&gt; &lt;asp:TextBox ID="ProductNameTextBox1" runat="server"&gt;&lt;/asp:TextBox&gt; &lt;asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" /&gt; &amp;nbsp;&lt;asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" /&gt; &lt;asp:Label ID="lblSubmit" runat="server" Text=" "&gt;&lt;/asp:Label&gt; &lt;/EditItemTemplate&gt; &lt;HeaderStyle forecolor="white" backcolor="Blue" /&gt; &lt;InsertItemTemplate&gt; ProductName: &lt;asp:TextBox ID="ProductNameTextBox" runat="server" Text='&lt;%# Bind("ProductName") %&gt;' /&gt; &lt;br /&gt; CategoryID: &lt;asp:TextBox ID="CategoryIDTextBox" runat="server" Text='&lt;%# Bind("CategoryID") %&gt;' /&gt; &lt;br /&gt; QuantityPerUnit: &lt;asp:TextBox ID="QuantityPerUnitTextBox" runat="server" Text='&lt;%# Bind("QuantityPerUnit") %&gt;' /&gt; &lt;br /&gt; UnitPrice: &lt;asp:TextBox ID="UnitPriceTextBox" runat="server" Text='&lt;%# Bind("UnitPrice") %&gt;' /&gt; &lt;br /&gt; &lt;asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" /&gt; &amp;nbsp;&lt;asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" /&gt; &lt;/InsertItemTemplate&gt; &lt;ItemTemplate&gt; ProductID: &lt;asp:Label ID="ProductIDLabel" runat="server" Text='&lt;%# Eval("ProductID") %&gt;' /&gt; &lt;br /&gt; ProductName: &lt;asp:Label ID="ProductNameLabel" runat="server" Text='&lt;%# Bind("ProductName") %&gt;' /&gt; &lt;br /&gt; CategoryID: &lt;asp:Label ID="CategoryIDLabel" runat="server" Text='&lt;%# Bind("CategoryID") %&gt;' /&gt; &lt;br /&gt; QuantityPerUnit: &lt;asp:Label ID="QuantityPerUnitLabel" runat="server" Text='&lt;%# Bind("QuantityPerUnit") %&gt;' /&gt; &lt;br /&gt; UnitPrice: &lt;asp:Label ID="UnitPriceLabel" runat="server" Text='&lt;%# Bind("UnitPrice") %&gt;' /&gt; &lt;br /&gt; &lt;/ItemTemplate&gt; &lt;PagerTemplate&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt;&lt;asp:LinkButton ID="FirstButton" CommandName="Page" CommandArgument="First" Text="&lt;&lt;" RunAt="server"/&gt;&lt;/td&gt; &lt;td&gt;&lt;asp:LinkButton ID="PrevButton" CommandName="Page" CommandArgument="Prev" Text="&lt;" RunAt="server"/&gt;&lt;/td&gt; &lt;td&gt;&lt;asp:LinkButton ID="NextButton" CommandName="Page" CommandArgument="Next" Text="&gt;" RunAt="server"/&gt;&lt;/td&gt; &lt;td&gt;&lt;asp:LinkButton ID="LastButton" CommandName="Page" CommandArgument="Last" Text="&gt;&gt;" RunAt="server"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/PagerTemplate&gt; &lt;/asp:FormView&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;asp:SqlDataSource ID="ProductsSqlDataSource" SelectCommand="SELECT [ProductID], [ProductName], [CategoryID], [QuantityPerUnit], [UnitPrice] FROM [Products]" connectionstring="&lt;%$ ConnectionStrings:NorthwindConnectionString %&gt;" RunAt="server"/&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload