0

我有一个 FormView,我需要访问其中的一些 Div 和其他控件。我的 apsx 代码与此类似:

 <asp:FormView ID="Edit_FV" runat="server" DataKeyNames="IDproceso" DefaultMode="Edit" DataSourceID="SqlDS_Procesos">
            <EditItemTemplate>
                <div id="second_info" runat="server">
                    <div id="second_info_left" runat="server">
                        <div id="alcance" class="report_field" runat="server">
                            <p class="container-title">
                                Alcance:</p>
                            <asp:TextBox ID="TextBox14" runat="server" TextMode="multiline" Width="400px" Height="120px" Text='<%# Bind("alcance") %>' />
                        </div> 
                    </div>
                    <div id="second_info_right" runat="server">
                    <div class="valores-container" id="tipo_ahorro" runat="server">
                        <asp:CheckBox ID="ahorro_state" runat="server" Checked='<%# Bind("tipo_ahorro") %>'  />
                    </div>
                </div>
            </EditItemTemplate>
        </asp:FormView>

现在,假设我想访问CheckBoxwith id = ahorro_state,我尝试了 withEdit_FV.FindControl("ahorro_state")并获得了 Null 引用。我也尝试过,Edit_FV.FindControl("MainContent_Edit_FV_ahorro_state")因为这是在最终 HTML 文档中实际命名 ID 的方式,但我也得到了 Null 引用。当我尝试访问任何 div(使用 ID 、 等)时,也会发生同样的second_info情况tipo_ahorro。我觉得我犯了一个愚蠢的错误,但我环顾四周并没有找到答案。

任何想法如何解决这个问题?

编辑:在我调用 FindControl 的地方添加了代码。

我尝试从 Page_Load() 调用 DataBind():

protected void Page_Load(object sender, EventArgs e)
        {

            DataBind();
            if (Edit_FV.CurrentMode == FormViewMode.Edit)
            {
                Control c = Edit_FV.FindControl("ahorro_state");//c is null here.
            }
        }

并且还尝试设置 Edit_FV 的 OnDataBound 属性:OnDataBound="onBound"

   protected void onBound(object sender, EventArgs e)
            {
                if (Edit_FV.CurrentMode == FormViewMode.Edit)
                {
                    ControlCollection a = Edit_FV.Controls;
                    Control c = Edit_FV.FindControl("ahorro_state");//c is null here
                }

            }
4

2 回答 2

1

尽管默认模式设置为“编辑”,但表单视图在控件为 DataBound 之前不会切换到该模式。尝试DataBind()先调用,然后使用元素的 ID(不是 ClientID,如您在第二个示例中尝试的那样)使用 FindControl。

有关将 FindControl 逻辑放置在何处的示例,请参阅FormView.FindControl(): object reference error

编辑:

您的数据源也有可能没有返回任何数据。这将导致 EditItemTemplate 为空,这可能会解释您的空引用错误。Edit_FV.DataItemCount > 0在切换到编辑模式之前尝试检查 a 。

于 2012-08-08T16:53:29.877 回答
1

I have had similar problems with 'FindControl'. I found a piece of code that has helped me
a) Find controls recursively, and
b) the debug statement has been very help to see why I am not finding the control in question.
To help me find the controls I have to give them ID values when I am looking for them if they don't have one by default:

    public static class General_ControlExtensions
{
    //From: http://www.devtoolshed.com/content/find-control-templatefield-programmatically
    /// <summary>
    /// recursively finds a child control of the specified parent.
    /// USAGE: 
    /// Control controlToFind = DetailsView1.fn_ReturnControl_givenControlID("txtName");
    /// </summary>
    /// <param name="rootControl"></param>
    /// <param name="ID"></param>
    /// <returns></returns>
    public static Control fn_ReturnControl_givenControlID(this Control rootControl, string ID)
    {
        if (rootControl.ID == ID)
        {
            return rootControl;
        }
        foreach (Control control in rootControl.Controls)
        {

            Debug.WriteLine("FindByID - child.id: " + control.ID);
            Control foundControl = fn_ReturnControl_givenControlID(control, ID);
            if (foundControl != null)
            {
                return foundControl;
            }
        }
        return null;
    }

Here is an example of its usage:

using System.Diagnostics;   // for debug 

TextBox txt_LastName = (TextBox)fv_NewHire_DetailsForm.fn_ReturnControl_givenControlID("INSERT_txt_LastName");

In addition, I have found it helpful for this type of problem to preface the controls in the 'insertitemtemplate' with 'INSERT_", and controls in the 'edititemtemplate' with 'EDIT_" to quickly tell them apart in the debug output.

于 2014-07-08T17:53:49.423 回答