我在页面上找不到动态创建的文本框

时间:2013-04-26 12:27:35

标签: asp.net .net

我正试图通过此点击事件在页面上找到我的控件。

编辑澄清:用户将输入一个贷款号码,然后按btnLoanNumber_Click,它将立即呼叫数据库并使用信息填充文本框。然后,用户将按btnEnter_1_Click。

更新文本框

编辑更新:我添加了这行代码,看看如果在创建控件之前清除控件会发生什么,我会收到以下错误:具有相同ID的多个控件

我不明白。我使用UpdatePanel是否重要?

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        PlaceHolder1.Controls.Clear();
        CreateControls();
    }

这一切看起来都很简单,但我相信状态是原因,我不知道如何解决这个问题。

    protected void btnLoanNumber_Click(object sender, EventArgs e)
    {
        // Hydrate properties with Pricing class properties (Pricing DB column names)
        PropertyInfo[] properties = type.GetProperties();

        LoanNumber = tbxLoanNumber.Text;
        Session["LoanNumber"] = LoanNumber;
        for (int i = 0; i < NumberOfControls; i++)
        {
            if (properties[i].GetValue(PricingByLoanNumber, null) != null)
            {
                string id = "tbx" + properties[i].Name;

                var tbx = (TextBox)PlaceHolder1.FindControl(id);

                tbx.Text = properties[i].GetValue(PricingByLoanNumber).ToString();
            }
        }            
    }

tbx始终为null。看起来奇怪的是,如果我尝试在该事件中创建控件,则会出现错误,因为找到了多个具有相同Id的控件。我错过了什么,如何找到我的控件?

以下是完整的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Servicing_Fix.Model;
using Servicing_Fix.DAL;
using System.Data;
using System.ComponentModel;
using System.Reflection;

namespace Servicing_Fix
{
public partial class Servicing_Fix : System.Web.UI.Page
{
    private string loanNumber = "";

    // Get Pricing Metadata
    Type type = typeof(Pricing);

    #region Properties

    protected Pricing PricingByLoanNumber
    {
        get { return GetPricingByLoanNumber(LoanNumber); }
    }

    protected string LoanNumber
    {
        get { return loanNumber.Replace(",", ""); }
        set { loanNumber = value.Replace(",", ""); }
    }

    protected int NumberOfControls
    {
        get { return type.GetProperties().Count(); }
    }

    #endregion

    #region Page Events

    protected void Page_Init(object sender, EventArgs e)
    {

    }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            CreateControls();
        }
        else
        {
            if (Session["LoanNumber"] != null)
            {
                LoanNumber = Session["LoanNumber"].ToString();
            }
        }


    }

    protected void btnLoanNumber_Click(object sender, EventArgs e)
    {
        // Hydrate properties with Pricing class properties (Pricing DB column names)
        PropertyInfo[] properties = type.GetProperties();

        LoanNumber = tbxLoanNumber.Text;
        Session["LoanNumber"] = LoanNumber;
        for (int i = 0; i < NumberOfControls; i++)
        {
            if (properties[i].GetValue(PricingByLoanNumber, null) != null)
            {
                string id = "tbx" + properties[i].Name;

                var tbx = (TextBox)PlaceHolder1.FindControl(id);

                tbx.Text = properties[i].GetValue(PricingByLoanNumber).ToString();
            }
        }            
    }

    protected void btnEnter_1_Click(object sender, EventArgs e)
    {
        // Hydrate properties with Pricing class properties (Pricing DB column names)
        PropertyInfo[] properties = type.GetProperties();

        for (int i = 0; i < NumberOfControls; i++)
        {
            var tbx = (TextBox)PlaceHolder1.FindControl("tbx" + properties[i].Name);

            if (tbx != null)
            {
                properties[i].SetValue(PricingByLoanNumber, tbx.Text);
            }                
        }

        UpdatePricingByLoanNumber(LoanNumber, PricingByLoanNumber);
        tbxLoanNumber.Text = "";
    }

    #endregion        

    #region Helpers

    private Pricing GetPricingByLoanNumber(string loanNumber)
    {
        var pricingDAL = new PricingDAL();

        return pricingDAL.GetPricingByLoanNumber(loanNumber);
    }

    private void UpdatePricingByLoanNumber(string loanNumber, Pricing pricing)
    {
        var pricingDAL = new PricingDAL();

        pricingDAL.UpdatePricingByLoanNumber(loanNumber, pricing);
    }

    private void CreateControls()
    {
        // Hydrate properties with Pricing class properties (Pricing DB column names)
        PropertyInfo[] properties = type.GetProperties();

        // Dynamically create labels and textbox controls
        for (int i = 0; i < NumberOfControls; i++)
        {
            Label lbl = new Label();
            TextBox tbx = new TextBox();

            lbl.ID = "lbl" + properties[i].Name;
            tbx.ID = "tbx" + properties[i].Name;

            lbl.Text = properties[i].Name;               

            PlaceHolder1.Controls.Add(lbl);
            PlaceHolder1.Controls.Add(tbx);
            PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
        }          
    }

    #endregion 
   }
}

2 个答案:

答案 0 :(得分:0)

你应该在创建它们之后尝试找到它们。您可能在创建事件之前调用了find事件

答案 1 :(得分:0)

看看这个ASP.NET Modifying Control Tree Dynamically

尝试使用viewstate存储您的值。