【问题标题】:Access values from dynamic controls in asp.net从 asp.net 中的动态控件访问值
【发布时间】:2015-09-15 19:17:05
【问题描述】:

我正在尝试在 asp.net 的动态控件中访问用户输入数据。

我没有在 page_load 中创建控件,因为它们基于一个 sql 查询存在,该查询仅在用户从下拉列表中选择一个值后才会解析。

我生成了 ID,因此它们是一致的,并且有一个刷新的提交按钮,我也在使用 ajax 更新面板。我缺少的是在回发之前实际保存动态控件状态的任何代码,并且自然地通过页面加载点它们还不存在。

我需要做些什么来确保控制值延续?

     protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {

        loadDD1();

    }

}

   private void loadDD1()
{
    try
    {
        ListItemCollection lic = DataAccessHelper.stNumsForName(ddaddressname.SelectedValue.ToString());
        ddaddressnumber.DataSource = lic;
        ddaddressnumber.DataTextField = "Text";
        ddaddressnumber.DataValueField = "Value";
        ddaddressnumber.DataBind();

        if (tbaddaddressnumber.Text != "")
        {

            ddaddressnumber.SelectedValue = tbaddaddressnumber.Text;
            tbaddaddressnumber.Text = null;
        }

    }
    catch (Exception es)
    {

    }
}

 protected void btret_Click(object sender, EventArgs e)
  {
    loadAll();      // Once a value from dropdown is selected
  }



private void loadAll()
  {
int FDDkey = int.Parse(ddaddressnumber.Items[ddaddressnumber.SelectedIndex].Value);

    BusinessObjects.ResidenceFDD res = new BusinessObjects.ResidenceFDD();
try
        {
           AnnArborFDDModel.AnnArborFDDEntities1 com = new AnnArborFDDModel.AnnArborFDDEntities1();

        // Communication com = new Communication();

        var query = from a in com.Communications 
                    where a.fddkey == res.FDDKey
                    select a;
        var querydd = (from b in com.Communications
                       where !String.IsNullOrEmpty(b.subject)
                      select b.subject).Distinct();


        var collectiondetails = new List<object>();

       // collectiondetails.Add(querydd);
        TextBox notestbbase = new TextBox();
        DropDownList notesddbase = new DropDownList();
        TextBox notestbcomments = new TextBox();

        TableRow tr01 = new TableRow();
        tr01.Cells.Add(new TableCell());
        tr01.Cells.Add(new TableCell());
        tr01.Cells[0].Text = "Date:";
        tr01.Cells[1].Text = "Correspondent:";
        this.tblnotes.Rows.Add(tr01);

        TableRow tr02 = new TableRow(); // Not sure if new row needs to be instantiated or not.
        tr02.Cells.Add(new TableCell());
        tr02.Cells.Add(new TableCell());
        notestbbase.ID = "notestbbase";

        tr02.Cells[0].Controls.Add(notestbbase);

        notesddbase.ID = "notesddbase";
        notesddbase.DataSource = querydd.ToList();

        notesddbase.DataBind();
        notesddbase.Items.Insert(0, emptyItem);
        notesddbase.SelectedIndex = 0;
        tr02.Cells[1].Controls.Add(notesddbase);

        this.tblnotes.Rows.Add(tr02);

        TableRow tr03 = new TableRow(); // Not sure if new row needs to be instantiated or not.
        tr03.Cells.Add(new TableCell());
        notestbcomments = new TextBox();
        notestbcomments.ID = "notestbcomments";
        notestbcomments.Width = 150;
        notestbcomments.Height = 150;
        notestbcomments.Wrap = true;
        notestbcomments.TextMode = TextBoxMode.MultiLine;

        tr03.Cells[0].Controls.Add(notestbcomments);

        this.tblnotes.Rows.Add(tr03);
}
catch (Exception ex)
{}

}
 protected void btupdate_Click(object sender, EventArgs e)
{
    try
    {
        Dictionary<string, string> idAndValueDictionary = new Dictionary<string, string>();
        TextBox tbdatetoadd = (TextBox)FindControl("notestbbase");   //Communications table
        DropDownList ddcorrespondanttoadd = (DropDownList)FindControl("notesddbase");  // Communications table
        TextBox tbnotestoadd = (TextBox)FindControl("notestbcomments");
}
// There is a catch statement here I am trying not to spam with too much     code.
}

【问题讨论】:

  • 贴出你的代码,以防万一。
  • 我添加了代码。问题仍然存在,我想我在什么时候才能获得用户输入框中的数据?我创建了这些框,然后在用户点击保存之前没有代码进行任何更改,并且在保存事件运行时,如果我试图让控件显示为 null

标签: c# asp.net


【解决方案1】:

试试这个,我没有测试过这个...

Dictionary<string, string> idAndValueDictionary = new Dictionary<string, string>();

填写字典以及何时将数据保存到视图状态。

idAndValueDictonary.Add(control id, value); // in a loop

ViewState["idAndValueDictionary"] = idAndValueDictionary;

在回发时:

idAndValueDictionary = ViewState["idAndValueDictionary"] as Dictionary<string, string>;

然后循环,根据 id 相应地设置值。

【讨论】:

  • 如果 @Julius 使用 SQL 查询来检索数据,我认为他需要将更新/新数据发送回 SQL 数据库。
  • 我们可以完成,但他需要发布一些代码。
  • 我可以处理的 sql 部分,唯一让我苦苦挣扎的是从动态控件中获取值。
  • 我会在一天结束之前为您提供解决方案。我现在没有时间编写代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-02
  • 1970-01-01
  • 2013-01-19
  • 1970-01-01
  • 2012-11-03
  • 2012-04-24
相关资源
最近更新 更多