1

FormView コントロールを持つこの Asp.net ページ。Nortwind データベース製品テーブルによって FormView コントロールにデータを入力しています。しかし、フォームビュー コントロール内に動的にラベルを入力したかったので、デザイナーの .cs ファイルはラベルで更新されません。また添付コードビハインド.FindControl.で試しましたが、常にNull Exceptionが発生します。

コードビハインド ファイル

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" />
                      &nbsp;<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" />
                      &nbsp;<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>
4

1 に答える 1