无法找到动态创建控件

qingquansmart 2008-04-12 04:06:24


这是前台标签:
<div>
<asp:Panel ID="Panel1" runat="server" Height="100%" Width="302px">
<table>
<tr>
<td style=" height:46px; width: 395px;">
<asp:FileUpload ID="ful" runat="server" Height="18px" Width="480px" />
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"
DataTextField="ZK_Title" DataValueField="ZK_DiscountID" Width="406px">
</asp:DropDownList><br />
<input id="File1" runat="server" type="file" style="width: 480px" /><br />
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource1"
DataTextField="ZK_Title" DataValueField="ZK_DiscountID" Width="406px">
</asp:DropDownList></td></tr></table></asp:Panel>
<asp:TextBox ID="textSum" runat="server" Width="63px"></asp:TextBox>
<asp:Button ID="AddBtn" runat="server" OnClick="AddBtn_Click" Text="增加" />
<asp:Button ID="FileUploadBtn" runat="server" OnClick="FileUploadBtn_Click" Text="全部上传" /><br/>
<br />
<asp:TextBox ID="picUrls" runat="server" Width="673px"></asp:TextBox>
</div>



后台代码:
//插入多张图片
protected void AddBtn_Click(object sender, EventArgs e)
{
DIS.Open();
SqlCommand cmd = new SqlCommand("SELECT [ZK_DiscountID], [ZK_Title] FROM [DiscountInfo] ORDER BY [ZK_DiscountID]", DIS.con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds,"ticketData");
int sum = Convert.ToInt32(textSum.Text);
for (int i = 0; i < sum; i++)
{
System.Web.UI.WebControls.Unit un = new System.Web.UI.WebControls.Unit();
System.Web.UI.WebControls.Unit un1 = new System.Web.UI.WebControls.Unit();
un = 401;
un1 = 480;
fu = new System.Web.UI.WebControls.FileUpload();
DDropDownList = new System.Web.UI.WebControls.DropDownList();
fu.ID = "fu_" + i.ToString();
fu.Width = un1;
DDropDownList.ID = "DropDownList" + (i+3);
DDropDownList.Width = un;

DDropDownList.DataSource = ds.Tables["ticketData"].DefaultView;
DDropDownList.DataTextField = "ZK_Title";
DDropDownList.DataValueField = "ZK_DiscountID";
DDropDownList.DataBind();

Panel1.Controls.Add(fu);
Panel1.Controls.Add(DDropDownList);
}
}
//上传多张图片
protected void FileUploadBtn_Click(object sender, EventArgs e)
{
string f_name, f_size, f_type;
string TimeNow = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Millisecond.ToString();
HttpFileCollection hfc = Request.Files;

// WebControl wc = null;
//DropDownList dd = new DropDownList();
try
{
for (int i = 0; i < hfc.Count; i++)
{
DropDownList obj = (DropDownList)this.FindControl("DropDownList" + (i + 1));

Response.Write("<script>alert('" + obj.SelectedIndex + "');</script>");//此处找不到ID picUrls.Text += obj.SelectedValue.ToString();
}


//for (int i = 0; i < hfc.Count; i++)
//{
// HttpPostedFile hpf = hfc[i];
// f_name = hpf.FileName;
// string ExtenName = f_name.Substring(f_name.LastIndexOf(".") + 1);
// if (ExtenName == "jpg" || ExtenName == "gif" || ExtenName == "jpeg" || ExtenName == "png" || ExtenName == "wbmp" || ExtenName == "bmp")
// {
// f_size = hpf.ContentLength.ToString();
// f_type = hpf.ContentType;
// string filename = System.IO.Path.GetFileName(f_name);
// string path1 = Server.MapPath("~/DIS/Ticket/");
// hpf.SaveAs(path1 + filename);
// MakeThumbnail(Server.MapPath("~/DIS/Ticket/") + filename, Server.MapPath("~/DIS/Ticket/") + TimeNow + "_" + filename, 190, 170, "Cut");
// picUrls.Text += "|" + TimeNow + "_" + filename;
// }
// else
// {
// Response.Write("<script>alert('支持文件格式:jpg,gif,jpeg,png,wbmp,bmp'请重新操作);</script>");
// picUrls.Text = "";
// }
//}
}
catch (Exception eeee)
{
Response.Write("图片上传成功,文本框中即是所添加的图片!请填写文本内容!");
Response.Write(eeee.ToString());
}
}


小弟第一次发帖还请多多关照.
...全文
31 点赞 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
qingquansmart 2008-04-12
附加:
回复
wanghui0380 2008-04-12
1.你问错版块了,你应该去net版问
2.这个问题是个常见问题,即动态添加的control是无法在postback以后再找到的,除非你每次page_load时都动态加载他

解决方法:
1.一般是放弃使用服务端控件,而使用dhtml+js操作
2.如果不想放弃服务端控件,则回传后不能直接使用服务器控件形式访问,而应该使用标准html的处理方式访问,即使用Request["控件的client name名"]访问
回复
相关推荐
发帖
ASP
创建于2007-09-28

2.8w+

社区成员

ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
申请成为版主
帖子事件
创建了帖子
2008-04-12 04:06
社区公告
暂无公告