我的ASPX页面出现问题。根据列表框中的选择,网站(动态)生成适当的控件。
稍后在ButtonClick
事件中,我需要获取几个文本框的文本值。这就是问题,我已经识别出具有适当属性(如ID)的文本框但不其Text
值的代码。
以下是我使用的代码:
//This is part of the company framework and is merely to provide context.
Dal.Customer newCus = new Dal.Customer();
//It recognizes the textboxes, but doesn't find the text inside it. (there is text in the textboxes)
TextBox txtCusName = (TextBox)Page.FindControl("ctl00$ContentPlaceHolder1$txtCusName");
TextBox txtCusCode = (TextBox)Page.FindControl("ctl00$ContentPlaceHolder1$txtCusCode");
newCus.DisplayName = txtCusName.Text;
newCus.CustomerCode = txtCusCode.Text;
我希望知道我在这里做错了什么,我错过了哪些步骤,或者是否有其他(更好)的方式来获得我需要的东西。
提前致谢。
修改
根据请求,这是在从列表框中进行选择后“生成”(或更好地说“写入”)页面上的相应控件的代码。
private void CreateCustomerTemplate(Control control)
{
PlaceHolder ph = new PlaceHolder();
TextBox txtCusName = new TextBox();
TextBox txtCusCode = new TextBox();
txtCusName.ClientIDMode = ClientIDMode.Static;
txtCusCode.ClientIDMode = ClientIDMode.Static;
txtCusName.ID = "txtCusName";
txtCusCode.ID = "txtCusCode";
ph.Controls.Add(new LiteralControl("<table><tr><td>" +
"Customer Name: </td><td>"));
ph.Controls.Add(txtCusName);
ph.Controls.Add(new LiteralControl("</td></tr><tr><td>" +
"Customer Code: </td><td>"));
ph.Controls.Add(txtCusCode);
ph.Controls.Add(new LiteralControl("</td></tr>"));
ph.Controls.Add(new LiteralControl("</table>"));
control.Controls.Add(ph);
}
以下是选择ListBox并最终调用相应控件的创建的代码。这发生在SelectedIndexChanged
- 事件(使用AutoPostBack=true
)。
switch (ddlType.SelectedItem.Value)
{
case "null":
btnSend.Visible = false;
break;
case "Customer":
new Template(TemplateType.Customer, spantempcontent);
btnSend.Visible = true;
templateType = TemplateType.Customer;
break;
//The switch goes on
}