Asp.net动态添加控件

Asp.net动态添加控件,第1张

是这样的.不光是B/s里.连C/s里都是这样.通过按钮事件是获取不到数据的.你只能这样.通过 *** 作两个,DataTable把行赋值给ListBox.然后把DataTable删除一行.另一个DataTable添加一行,于此同时 *** 作两个ListBox,说白点就是,手动实现两边中没数据交互,ListBox只是用于简单的显示作用,真正的 *** 作是通只手动 *** 作两个DataTable来实现数据交互困埋的,不晓得,您看懂了米 .- -.你还好呗哩.最汪培蚂近..怎么实然搞起这个来哩.?

很简单,比如imgURL

control.imgURL="图片路径"

看这漏模些属性的类型public string imgURL是字符串就填写字符串颤烂格式的路径。private int _juli是int数茄搜漏字类型,那么就是

control.juli=1

在Net程序开发中,有时上传功能会实现让用户上传多个图片或文件,一个一个慧闭上传肯定有点麻烦,而且还不人性化。如果做成死的,一次上次,三个,或是五个的,有时候还不够用,这就很烦了。下面这种方法,在上传的地方加一个按钮,如果用户想上传几个就点几次,这样就会出现多个上传框,让他选择,好了,不多说了。下面是代码:

前台代码

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>文件上传_IT知道网itwis.com</title>

<script language="javascript" type="text/javascript">

function addFile(max)

{

var file = document.getElementsByName("File")

alert(file.length)

if(file.length==1 &&file[0].disabled==true)

{

file[0].disabled = false

return

}

if(file.length<max)

{

var fileButton = '<br /><input type="file" size="50" name="File" />'

alert(fileButton)

document.getElementById("FileList").insertAdjacentHTML("beforeEnd",fileButton)

}

}

</script>

</head>

<body>

<form id="form1" runat="server" enctype="multipart/form-data">

<p id="FileList">

<input type="file" disabled="disabled" size="50" name="File" />

</p>

<input type="button" value='增加一个文件' onclick="addFile(<%=MaxFileCounts%>)" />

<br />

<br />

<br />

<asp:Button ID="Button1" runat="server" Text="添加" OnClick="Button1_Click" />

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

</form>

</body>

</html>

这里有一点要注意的是<input type="button" value='增加一个文件' onclick="addFile(<衫和%=MaxFileCounts%>)" />这里面的MaxFileCounts。这个是用户上传最大数的一个限制。你可以在这里写成死值,也可以在CS进行配置,主要看你的需求。

下面是cs文件了

这里的MaxFileCount是在我配置文件写的,你可以根据你的情前塌裂况写。

public int MaxFileCounts = MaxFileCount

protected void Page_Load(object sender, EventArgs e)

{}

protected void Button1_Click(object sender, EventArgs e)

{

HttpFileCollection fileList = HttpContext.Current.Request.Files

if (fileList == null)

{

return

}

FileImage file = new FileImage()//这是自定义的一个写库的类,可根据实际情况自我定义。

try

{

for (int i = 0i <fileList.Counti++)

{

HttpPostedFile postedFile = fileList[i]

if (postedFile == null)

continue

string fileName = Path.GetFileNameWithoutExtension(postedFile.FileName)

string extension = Path.GetExtension(postedFile.FileName)

if (string.IsNullOrEmpty(extension) == true)

continue

bool flag = false

foreach (string ext in AllowFileList)

{

if (ext == extension.ToLower())

{

flag = true

}

}

if (flag == false)

continue

string storeUrl = DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + extension.ToString()

string Url = storeFilePath + storeUrl

string fullPath = Server.MapPath(Url)

postedFile.SaveAs(fullPath)

Hashtable ht = new Hashtable()

ht.Add("Title",fileName)

ht.Add("imgUrl",storeUrl)

ht.Add("imgType",postedFile.ContentType)

ht.Add("imgSize",postedFile.ContentLength)

file.insertImage(ht) //这里是我的添加语句,你可写成你自己的。

}

}

catch (Exception ex)

{

this.Label1.Text = ex.Message

}

}

本文来自: IT知道网(http://www.itwis.com) 详细出处参考:http://www.itwis.com/html/net/aspnet/20081024/2627.html


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/bake/11996423.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存