我知道很多人都迷惑,怎样处理动态添加的控件的事件..我这里有个简单的例子:

Lostinet 2003-12-12 10:39:17
<%@ Page language="c#" Codebehind="FileManager.aspx.cs" AutoEventWireup="false" Inherits="LWSampleCS.FileManager" %>
<%@ Register TagPrefix="LWS" Namespace="LWSampleCS" Assembly="LWSampleCS" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>FileManager</title>
<style> BODY { FONT-SIZE: 9pt } TABLE { FONT-SIZE: 9pt } </style>
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="FlowLayout">
<form id="Form1" method="post" runat="server">
<P>
<asp:Button id="ButtonRefresh" runat="server" Text="Refresh"></asp:Button></P>
<P>
<LWS:MyPanel id="panel" runat="server">
Here will fill with table
</LWS:MyPanel></P>
<P><INPUT id="File1" type="file" name="File1" runat="server">
<asp:Button id="ButtonUpload" runat="server" Text="Upload"></asp:Button></P>
</form>
</body>
</HTML>


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using System.IO;

namespace LWSampleCS
{
public class FileManager : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
protected System.Web.UI.WebControls.Button ButtonUpload;
protected System.Web.UI.WebControls.Button ButtonRefresh;
protected LWSampleCS.MyPanel panel;

private void InitializeComponent()
{
this.ButtonRefresh.Click += new System.EventHandler(this.ButtonRefresh_Click);
this.ButtonUpload.Click += new System.EventHandler(this.ButtonUpload_Click);

}

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

if(Files==null)
{
ResetTable();
}
else
{
RestoreTable();
}
}

public void FillFiles()
{
string dir=Server.MapPath("Files");
if(!Directory.Exists(dir))
Directory.CreateDirectory(dir);
Files=Directory.GetFiles(dir);
}

public string[] Files
{
get
{
return (string[])ViewState["Files"];
}
set
{
ViewState["Files"]=value;
}
}

public void ResetTable()
{
FillFiles();
panel.Reset();
FillTable();
}

public void RestoreTable()
{
panel.Reset();
FillTable();
}

public void FillTable()
{
Table table=new Table();
table.Attributes["border"]="1";
table.Style["border-collapse"]="collapse";
table.CellPadding=2;
table.CellSpacing=0;
table.Width=Unit.Percentage(100);

TableRow hrow=new TableRow();

TableCell hcell;
hcell=new TableCell();
hcell.Text="FileName";
hrow.Cells.Add(hcell);
hcell=new TableCell();
hcell.Text="CreationTime";
hrow.Cells.Add(hcell);
hcell=new TableCell();
hcell.Text="Delete";
hrow.Cells.Add(hcell);

table.Rows.Add(hrow);

foreach(string file in Files)
{
TableRow row=new TableRow();


TableCell cell;

//FileName
cell=new TableCell();
cell.Text=file;
row.Cells.Add(cell);

//CreationTime
cell=new TableCell();
cell.Text=File.GetCreationTime(file).ToString("yyyy-MM-dd HH:mm:ss");
row.Cells.Add(cell);

//Delete
cell=new TableCell();

row.Cells.Add(cell);

Button ButtonDelete=new Button();
ButtonDelete.Click+=new EventHandler(ButtonDelete_Click);
ButtonDelete.Text="Delete";
ButtonDelete.Attributes["filename"]=file;
cell.Controls.Add(ButtonDelete);

table.Rows.Add(row);
}

panel.Controls.Add(table);

}

private void ButtonRefresh_Click(object sender, System.EventArgs e)
{
ResetTable();
ButtonRefresh.Text="Refresh @ "+DateTime.Now.ToLongTimeString();
}

private void ButtonUpload_Click(object sender, System.EventArgs e)
{
if(File1.PostedFile.FileName!="")
{
string newpath=Path.Combine(Server.MapPath("Files"),Path.GetFileName(File1.PostedFile.FileName));
byte[] buf;
using(Stream stream=File1.PostedFile.InputStream)
{
buf=new byte[stream.Length];
stream.Read(buf,0,buf.Length);
}
using(FileStream fs=new FileStream(newpath,FileMode.Create,FileAccess.Write))
{
fs.Write(buf,0,buf.Length);
}
ResetTable();
ButtonUpload.Text="Upload @ "+DateTime.Now.ToLongTimeString();
}
else
{
ButtonUpload.Text="File? @ "+DateTime.Now.ToLongTimeString();
}
}

private void ButtonDelete_Click(object sender, EventArgs e)
{
Button Btn=(Button)sender;
File.Delete(Btn.Attributes["filename"]);
ResetTable();
}
}

public class MyPanel : Panel , INamingContainer
{
public void Reset()
{
Controls.Clear();
ClearChildViewState();
}
}
}


------------
其实动态添加的控件,最重要是用ViewState保持它的状态,并且在回发后能恢复它们.

这个例子应该打开两个窗口来测试。

其中一个窗口新建了文件,
第二个窗口不选择文件直接Upload是不会见到第一窗口上传的文件的。
(就好象不重新绑定就看不到数据库新的东西一样)

其中 Files 就是储存了某个时刻的文件列表。.

...全文
98 36 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
36 条回复
切换为时间正序
请发表友善的回复…
发表回复
azev 2003-12-16
  • 打赏
  • 举报
回复
收藏
lpc007 2003-12-16
  • 打赏
  • 举报
回复
继续收藏,谢谢~
zhaozhongju 2003-12-16
  • 打赏
  • 举报
回复
学习,偶也碰到过这种情况,没成功。MARK
elite2018 2003-12-12
  • 打赏
  • 举报
回复
谢谢,收藏~
Edison621 2003-12-12
  • 打赏
  • 举报
回复
谢谢,收藏~
Lostinet 2003-12-12
  • 打赏
  • 举报
回复
to: saucer(思归)
上面算是两个例子了.
如果把创建表格那段缩短,并且稍微减少函数的数量
那应该算简单了吧?


-----
不过那两个例子都有问题。
吃完饭后传个好点的上来.
myzig 2003-12-12
  • 打赏
  • 举报
回复
谢谢
houlinghouling 2003-12-12
  • 打赏
  • 举报
回复
谢谢了!
saucer 2003-12-12
  • 打赏
  • 举报
回复
呵呵,你这个例子要算简单的话,:-)
bullnade 2003-12-12
  • 打赏
  • 举报
回复
.....................................保存下来研究一下!谢
tianweima 2003-12-12
  • 打赏
  • 举报
回复
谢谢,收藏
ffjing 2003-12-12
  • 打赏
  • 举报
回复
虽然没用到,弄下来学习学习
SunofNight 2003-12-12
  • 打赏
  • 举报
回复
收藏先
悟空师傅来了 2003-12-12
  • 打赏
  • 举报
回复
谢谢啦。。。
^_^
Lostinet 2003-12-12
  • 打赏
  • 举报
回复
这里帖上,
如果是做成Custom Control的结果..

(其实恢复控件在CreateChildControls里做更好)
(但是更好的是在LoadViewState后做)

namespace LWSampleCS
{
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using System.IO;

public class FilesControl : Control,INamingContainer
{
Button BtnRefresh;
Button BtnUpload;
HtmlInputFile InputFile;

//Create - or Restore
protected override void CreateChildControls()
{
base.CreateChildControls();

if(Files==null)
{
ResetTable();
}
else
{
FillTable();
}
}

public void FillFiles()
{
string dir=Context.Server.MapPath("Files");
if(!Directory.Exists(dir))
Directory.CreateDirectory(dir);
Files=Directory.GetFiles(dir);
}

public string[] Files
{
get
{
return (string[])ViewState["Files"];
}
set
{
ViewState["Files"]=value;
}
}

public void ResetTable()
{
FillFiles();
Controls.Clear();
ClearChildViewState();
FillTable();
}

public void RestoreTable()
{
Controls.Clear();
ClearChildViewState();
FillTable();
}

public void FillTable()
{
Table table=new Table();
table.Attributes["border"]="1";
table.Style["border-collapse"]="collapse";
table.CellPadding=2;
table.CellSpacing=0;
table.Width=Unit.Percentage(100);

TableRow hrow=new TableRow();

TableCell hcell;
hcell=new TableCell();
hcell.Text="FileName";
hrow.Cells.Add(hcell);
hcell=new TableCell();
hcell.Text="CreationTime";
hrow.Cells.Add(hcell);
hcell=new TableCell();
hcell.Text="Delete";
hrow.Cells.Add(hcell);

table.Rows.Add(hrow);

foreach(string file in Files)
{
TableRow row=new TableRow();


TableCell cell;

//FileName
cell=new TableCell();
cell.Text=file;
row.Cells.Add(cell);

//CreationTime
cell=new TableCell();
cell.Text=File.GetCreationTime(file).ToString("yyyy-MM-dd HH:mm:ss");
row.Cells.Add(cell);

//Delete
cell=new TableCell();

row.Cells.Add(cell);

Button ButtonDelete=new Button();
ButtonDelete.Click+=new EventHandler(ButtonDelete_Click);
ButtonDelete.Text="Delete";
ButtonDelete.Attributes["filename"]=file;
cell.Controls.Add(ButtonDelete);

table.Rows.Add(row);
}

//this
Controls.Add(table);

BtnRefresh=new Button();
BtnRefresh.Click+=new EventHandler(BtnRefresh_Click);
BtnRefresh.Text="Refresh";
Controls.Add(BtnRefresh);

InputFile=new HtmlInputFile();
Controls.Add(InputFile);

BtnUpload=new Button();
BtnUpload.Click+=new EventHandler(BtnUpload_Click);
BtnUpload.Text="Upload";
Controls.Add(BtnUpload);
}

private void BtnRefresh_Click(object sender, EventArgs e)
{
ResetTable();
BtnRefresh.Text="Refresh @ "+DateTime.Now.ToLongTimeString();
}

private void BtnUpload_Click(object sender, EventArgs e)
{
if(InputFile.PostedFile.FileName!="")
{
string newpath=Path.Combine(Context.Server.MapPath("Files"),Path.GetFileName(InputFile.PostedFile.FileName));
byte[] buf;
using(Stream stream=InputFile.PostedFile.InputStream)
{
buf=new byte[stream.Length];
stream.Read(buf,0,buf.Length);
}
using(FileStream fs=new FileStream(newpath,FileMode.Create,FileAccess.Write))
{
fs.Write(buf,0,buf.Length);
}
ResetTable();
BtnUpload.Text="Upload @ "+DateTime.Now.ToLongTimeString();
}
else
{
BtnUpload.Text="File? @ "+DateTime.Now.ToLongTimeString();
}
}

private void ButtonDelete_Click(object sender, EventArgs e)
{
Button Btn=(Button)sender;
File.Delete(Btn.Attributes["filename"]);
ResetTable();
}
}
}
GreenSpring 2003-12-12
  • 打赏
  • 举报
回复
mark too
aiailove 2003-12-12
  • 打赏
  • 举报
回复
mark
wyfwyf2000 2003-12-12
  • 打赏
  • 举报
回复
好,收藏
rferen2003 2003-12-12
  • 打赏
  • 举报
回复
先up,
再收藏,
再看看
gannet 2003-12-12
  • 打赏
  • 举报
回复
谢谢~
加载更多回复(16)

62,244

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧