查找Panel里面动态添加的控件
我在一个Panel里边动态添加HtmlInputFile控件,代码如下:
for(int i=1;i <=Convert.ToInt32(this.DDLExtraNum.SelectedValue);i++)
{
System.Web.UI.HtmlControls.HtmlInputFile hf=new HtmlInputFile();
hf.Attributes.Add( "runat ", "server ");
hf.ID= "hf "+i.ToString();
this.PLExtra.Controls.Add(hf);
为什么在查找这个控件的时候,会出错呢,查找控件的代码如下:
FullFileName=((HtmlInputFile)this.PLExtra.FindControl( "hf "+i.ToString())).PostedFile.FileName;
------解决方案--------------------postback问题
--aspx
<asp:DropDownList ID=ddl runat=server AutoPostBack= "True " OnSelectedIndexChanged= "ddl_SelectedIndexChanged ">
</asp:DropDownList>
<asp:Panel ID=PLExtra runat=server> </asp:Panel>
<asp:Button ID=btn runat=server OnClick= "btn_Click " />
--cs
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ddl.Items.Add(new ListItem( "1 ", "1 "));
ddl.Items.Add(new ListItem( "2 ", "2 "));
}
else
{
fill();
}
}
protected void btn_Click(object sender, EventArgs e)
{
string FullFileName = ((HtmlInputFile)this.PLExtra.FindControl( "hf1 ")).PostedFile.FileName;
Response.Write(FullFileName);
}
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void fill()
{
for (int i = 1; i <= Convert.ToInt16(ddl.SelectedValue); i++)
{
System.Web.UI.HtmlControls.HtmlInputFile hf = new HtmlInputFile();
hf.Attributes.Add( "runat ", "server ");
hf.ID = "hf " + i.ToString();
this.PLExtra.Controls.Add(hf);
}
}