可以';无法在FormView中使用FindControl查找字段

本文关键字:FindControl 字段 查找 FormView 可以 | 更新日期: 2023-09-27 18:21:36

我想知道是否有人能帮我。我一直在寻找答案,到目前为止,但我觉得我错过了什么。

我有一个使用动态数据创建的FormView。在FormView中,我有三个字段,ItemCosts、AdditionalCosts和TotalCosts。我希望能够在表单上放置一个按钮,将ItemsCost和AdditionalCosts添加在一起,并将其显示在TotalCosts文本框中。很简单。。。所以我想。

我发现我需要使用ItemCommand,因为FormView在发布时使用此命令。这是我写的:

HTML

 <asp:Panel ID="DetailsPanel" runat="server">
                <br /><br />
                <asp:FormView ID="FormView1" runat="server" DataSourceID="DetailsDataSource" RenderOuterTable="false"
                    OnPreRender="FormView1_PreRender" OnModeChanging="FormView1_ModeChanging" OnItemUpdated="FormView1_ItemUpdated"
                    OnItemInserted="FormView1_ItemInserted" OnItemDeleted="FormView1_ItemDeleted" OnItemCommand="FormView1_ItemCommand" OnDataBinding="FormView1_DataBind">
                    <HeaderTemplate>
                        <table id="detailsTable" class="DDDetailsTable" cellpadding="6">
                    </HeaderTemplate>
                    <ItemTemplate>
                    <tr class="td">
                        <td class="DDLightHeader">Order No</td>
                        <td><asp:DynamicControl ID="OrderNo" runat="server" DataField="OrderNo" /></td>
                    </tr>
                    <tr class="td">
                        <td class="DDLightHeader">Item Costs</td>
                        <td><asp:DynamicControl runat="server" DataField="ItemCosts" /></td>
                    </tr>
                    <tr class="td">
                        <td class="DDLightHeader">AdditionalCosts</td>
                        <td><asp:DynamicControl runat="server" DataField="AdditionalCosts" /></td>
                    </tr>
                    <tr class="td">
                        <td class="DDLightHeader">Total Costs</td>
                        <td><asp:DynamicControl runat="server" DataField="TotalCosts" /></td>
                    </tr>
                    <tr class="td">
                           <td colspan="2">
                                <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit" Text="Edit" />
                                <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Delete" Text="Delete"
                                    OnClientClick='return confirm("Are you sure you want to delete this item?");' />  
 </td>
                        </tr>
                    </ItemTemplate>
                    <EditItemTemplate>
                     <tr class="td">
                        <td class="DDLightHeader">Order No</td>
                        <td><asp:DynamicControl ID="OrderNo" runat="server" DataField="OrderNo" Mode="ReadOnly"/></td>
                    </tr>
                    <tr class="td">
                        <td class="DDLightHeader">Item Costs</td>
                        <td><asp:DynamicControl runat="server" DataField="ItemCosts" Mode="Edit" /></td>
                    </tr>
                    <tr class="td">
                        <td class="DDLightHeader">Additional Costs</td>
                        <td><asp:DynamicControl runat="server" DataField="AdditionalCosts" Mode="Edit" /></td>
                    </tr>
                    <tr class="td">
                        <td class="DDLightHeader">Total Costs</td>
                        <td><asp:DynamicControl runat="server" DataField="TotalCosts" Mode="Edit"  /></td>
                        <td><asp:Button runat="server" ID="btnCalculateTotalCosts" Text="Calculate total costs" CommandName="Calculate" /></td>
                    </tr>                    
                    <tr class="td">
                        <td class="DDLightHeader">View Items</td>
                        <td><asp:DynamicControl runat="server" DataField="tblCateringOrdersDetailsItems" Mode="Edit" /></td>
                    </tr>
                       <tr class="td">
                            <td colspan="2">
                                <asp:LinkButton ID="LinkButton4" runat="server" CommandName="Update" Text="Update" />
                                <asp:LinkButton ID="LinkButton5" runat="server" CommandName="Cancel" Text="Cancel" CausesValidation="false" />
                            </td>
                        </tr>
                    </EditItemTemplate>
                    <InsertItemTemplate>
                        <asp:DynamicEntity ID="DynamicEntity3" runat="server" Mode="Insert" />
                        <tr class="td">
                            <td colspan="2">
                                <asp:LinkButton ID="LinkButton6" runat="server" CommandName="Insert" Text="Insert" />
                                <asp:LinkButton ID="LinkButton7" runat="server" CommandName="Cancel" Text="Cancel" CausesValidation="false" />
                            </td>
                        </tr>
                    </InsertItemTemplate>
                    <FooterTemplate>
                        </table>
                    </FooterTemplate>
                </asp:FormView>
                <asp:EntityDataSource ID="DetailsDataSource" runat="server" EnableDelete="true" EnableInsert="true" EnableUpdate="true" />
                <asp:QueryExtender ID="QueryExtender1" TargetControlID="DetailsDataSource" runat="server">
                    <asp:ControlFilterExpression ControlID="GridView1" />
                </asp:QueryExtender>
            </asp:Panel>

我在EditTemplate部分添加了一个btnCalculateTotalCosts按钮。

在后面的代码中,我创建了一个ItemCommand控件

protected void FormView1_ItemCommand(object sender, FormViewCommandEventArgs e)
        {
            if (e.CommandName == "Calculate")
            {
                FormViewRow row = FormView1.Row;
                decimal itemCosts;
                decimal additionalCosts;
                TextBox itemsCostTextBox = (TextBox)row.FindControl("ItemCosts");
                TextBox additionalCostsTextBox = (TextBox)row.FindControl("AdditionalCosts");
                TextBox totalCostsTextBox = (TextBox)row.FindControl("TotalCosts");
                Decimal.TryParse(itemsCostTextBox.Text, out itemCosts);
                Decimal.TryParse(additionalCostsTextBox.Text, out additionalCosts);
                totalCostsTextBox.Text = (itemCosts + additionalCosts).ToString();
}

但我一直得到一个"错误对象引用没有设置为对象的实例"。我读到你必须首先将字段绑定到FormView,所以我尝试创建以下

 protected void FormView1_DataBind(object sender, EventArgs e)
        {
            if (FormView1.CurrentMode == FormViewMode.Edit)
            {
                TextBox itemsCostTextBox = (TextBox)FormView1.FindControl("ItemCosts");
                TextBox additionalCostsTextBox = (TextBox)FormView1.FindControl("AdditionalCosts");
                TextBox totalCostsTextBox = (TextBox)FormView1.FindControl("TotalCosts");
            }
        }

并将标记中的DataBind引用为OnDataBinding="FormView1_DataBind",但这也不起作用,我得到了同样的错误。

我真的试过解决这个问题,并意识到FindControl并没有"看到"FormView中的字段,但我就是不知道如何做到这一点。

如有任何帮助,我们将不胜感激感谢

可以';无法在FormView中使用FindControl查找字段

FindControl()接收您要查找的控件的ID。从您发布的ASPX代码中,我没有看到您正在为尝试使用FindControl()查找的DynamicControl设置ID属性。对该方法的调用:

TextBox itemsCostTextBox = (TextBox)row.FindControl("ItemCosts");

正在传递DataField属性值而不是ID。将ID属性添加到要查找的所有DynamicControl

还要考虑到FindControl()不执行递归搜索,它只在控件的子级之间查找具有指定ID的控件。

如果它能帮助其他人:

    protected void FormView1_ItemCommand(object sender, FormViewCommandEventArgs e)
    {
        if (e.CommandName == "Calculate")
        {
            // ...            
            //instead of this...
            TextBox itemsCostTextBox = (TextBox)row.FindControl("ItemCosts");
            //(no need to use row here, just use the reference to the FormView;
            //also no need to add an ID to DynamicControl because it creates
            //its own ID out of the field name and template control name)
            //cast like this...
            var itemsCostTextBox = (TextBox)FormView1.FindFieldTemplate("ItemCosts").TemplateControl.FindControl("txtTextBox1");
            //txtTextBox1 is the name of the specific web UI control inside of
            //the DynamicData *.ascx template that you want access to
        }
    }