1

这个具有 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" />
                      &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

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!";
    }
}

我知道这是一个古老的问题,但我认为发布答案不会有什么坏处

于 2014-01-03T14:48:21.310 回答