1

我想知道是否有人可以帮助我解决这个问题。我一直在寻找答案并到目前为止,但我认为我错过了一些东西。

我有一个使用动态数据创建的 FormView。在该 FormView 中,我有 3 个字段,ItemCosts、AdditionalCosts 和 TotalCosts。我希望能够在表单上放置一个按钮,将 ItemsCosts 和 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 中的字段,但我只是不知道如何做到这一点。

任何帮助将不胜感激谢谢

4

2 回答 2

0

FindControl()接收ID您正在寻找的控件。从您发布的 ASPX 代码中,我没有看到您正在IDDynamicControl您尝试使用FindControl(). 对该方法的调用:

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

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

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

于 2013-07-08T16:38:06.047 回答
0

如果它对其他人有帮助:

    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
        }
    }
于 2016-05-23T22:00:53.000 回答