【发布时间】: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