在FormView中无法使用FindControl查找字段

时间:2013-07-08 16:22:21

标签: c# asp.net asp.net-dynamic-data

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

我有一个使用动态数据创建的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中的字段,但我无法理解如何做到这一点。

非常感谢任何帮助 谢谢

2 个答案:

答案 0 :(得分:0)

FindControl()收到您要查找的控件的ID。从您发布的ASPX代码我发现您没有看到您要使用ID设置DynamicControl的{​​{1}}属性。这个方法的调用:

FindControl()

传递TextBox itemsCostTextBox = (TextBox)row.FindControl("ItemCosts"); 属性值而不是DataField。将ID属性添加到您要查找的所有ID

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

答案 1 :(得分: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
        }
    }