如何在Panel中动态创建textBox和Masked Edit扩展器。 我的代码是这样的: 在ASPX页面中:
<asp:Repeater Id = "Repeater1" runat="server" DataSource="Function1" OnitemDataBound="ShowProducts_OntemDataBound">
<ItemTemplate>
<asp: Panel Id= "Panel1" runat="server">
<cc1:MaskedEditExtender Id="MskEdit" Mask="(999)-999-9999">
</cc1:MaskedEditExtender>
</asp:Panel>
</ItemTemplate>
</asp:Repeater>
在Aspx.Cs页面
Private DataView Function1()
{
Dataview dv =new dataview();
return dv;
}
Private void ShowProducts_OntemDataBound(object sender, RepeaterEventItem e)
{
//Consider For the First Iteration of the Repeater I am Creating a Simple Text Box Dynamically
Textbox txt = new textbox();
txt.Text = "8888888888";
txt.Id = "TextBox1";
//Consider For the Second Iteration of the Repeater I am Creating another TextBox and a
Textbox txt1 = new textBox();
txt1.text="2223334444";
txt1.Id = "TextBox2";
MaskedEditExtender mskEdit = (MaskedEditExtender)e.Item.FindControl("MskEdit");
mskEdit.TargetControlId = txt1.Id;
Panel panel1 = (Panel)e.item.Findcontrol("Panel1");
panel1.Controls.Add(txt1);
}
当运行上面的代码时,它给了我“MaskedEditExtender的空引用异常”。请给我一些建议。
答案 0 :(得分:0)
检查e.ItemFindControl(“MskEdit”)是否为null,因为在页眉,页脚行中,它将是。 这是代码:
if(e.Item.FindControl("MskEdit")!=null)
{
MaskedEditExtender mskEdit = (MaskedEditExtender)e.Item.FindControl("MskEdit"); mskEdit.TargetControlId = txt1.Id;
}