一个关于控件的属性访问问题(前所未有的难度),望各位高手赐教

时间:2021-06-06 14:22:41
一个上穿文件的页面,使用代码分离来放置控件,如下:
up.aspx.vb
------------------------------------------------------------------
 Page.Response.Write("<body><form enctype='multipart/form-data' METHOD=POST ACTION="" ><input type='file' id='PersonImage' NAME='PersonImage' runat=server><input value=' 添  加 ' type=submit iD='submit' name=submit></form></body>")
-------------------------------------------------------------------
虽然form不是runat=server,但是如果这段代码放置在up.aspx里是通过的。现在是把它放置在up.aspx.vb里,用response.write 输出。
显示没有任何问题。问题在于处理文件上传的代码也是在up.aspx.vb里。如下:
------------------------------------------------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

If Request("submit") <> "" Then
            Dim intImageSize As Int64
            Dim strImageType As String
            Dim ImageStream As System.IO.Stream
            ' 获得图片的大小
            intImageSize = PersonImage.PostedFile.ContentLength
            ......

--------------------------------------------------------------------
大家注意这一行:
intImageSize = PersonImage.PostedFile.ContentLength
其中的personImage是文件上传控件的名称。
显然这个控件是存在的,它是使用response.write 来生成的。但是在up.aspx.vb里它并不知道,所以根本无法通过生成。
而这些代码如果放置在up.aspx里,不使用代码分离,通过。因为这个up.aspx就不需要生成了。
如何解决这种动态生成的控件的属性被访问?

8 个解决方案

#1


在后台类里定义一个 
protected System.Web.UI.HtmlControls.HtmlInputFile PersonImage;
试试看

#2


it is a very  bad style to use Page.Response.Write to output <form> like that, but since the id/name are for client side only, you can not use PersonImage, just try

Page.Response.Write("<body><form enctype='multipart/form-data' METHOD=POST ACTION="" ><input type='file' id='PersonImage' NAME='PersonImage'><input value=' 添  加 ' type=submit iD='submit' name=submit></form></body>")


..

HttpPostedFile hpf = Request.Files[0];
intImageSize = hpf.ContentLength;

#3


请问 saucer(思归) 有没有好的方法处理这样的问题了?
以前的时候在asp中习惯了这种写法,我也遇到了楼主同样的问题。
在asp.net中是通过什么机制来解决这种问题了呢?

#4


HtmlForm form=new HtmlForm();
form.EncType="multipart/form-data";
HtmlInputFile ifile=new HtmlInputFile();
ifile.ID="PersonImage";
ifile.Name="PersonImage";
form.Controls.Add(ifile);
Button btn=new Button();
btn.Click=new EventHandler(Button1_Click);
btn.Text="添加";
form.Controls.Add(btn);
Page.Controls.Add(form);

#5


但是这样怎么进行精确的定位了,让他在什么地方显示??因为它不是直接在页面中的,

#6


因为在后台程序中执行,那么怎么进行精确的定位呢?难道他和ASP中的RESPONSE.WRITE一样吗?

#7


没什么难的,假如你要在一个TextBox之间换行,可以再加一个<br>标签,用这样的方法加入:

form.Controls.Add(new LiteralControl("<br>"));

其它任何html标签或css属性等都可以用此方法加入。

#8


但是你这样做根本没有做到,代码也与页面分离!

是不符合微软所推崇的MVC模型的阿

#1


在后台类里定义一个 
protected System.Web.UI.HtmlControls.HtmlInputFile PersonImage;
试试看

#2


it is a very  bad style to use Page.Response.Write to output <form> like that, but since the id/name are for client side only, you can not use PersonImage, just try

Page.Response.Write("<body><form enctype='multipart/form-data' METHOD=POST ACTION="" ><input type='file' id='PersonImage' NAME='PersonImage'><input value=' 添  加 ' type=submit iD='submit' name=submit></form></body>")


..

HttpPostedFile hpf = Request.Files[0];
intImageSize = hpf.ContentLength;

#3


请问 saucer(思归) 有没有好的方法处理这样的问题了?
以前的时候在asp中习惯了这种写法,我也遇到了楼主同样的问题。
在asp.net中是通过什么机制来解决这种问题了呢?

#4


HtmlForm form=new HtmlForm();
form.EncType="multipart/form-data";
HtmlInputFile ifile=new HtmlInputFile();
ifile.ID="PersonImage";
ifile.Name="PersonImage";
form.Controls.Add(ifile);
Button btn=new Button();
btn.Click=new EventHandler(Button1_Click);
btn.Text="添加";
form.Controls.Add(btn);
Page.Controls.Add(form);

#5


但是这样怎么进行精确的定位了,让他在什么地方显示??因为它不是直接在页面中的,

#6


因为在后台程序中执行,那么怎么进行精确的定位呢?难道他和ASP中的RESPONSE.WRITE一样吗?

#7


没什么难的,假如你要在一个TextBox之间换行,可以再加一个<br>标签,用这样的方法加入:

form.Controls.Add(new LiteralControl("<br>"));

其它任何html标签或css属性等都可以用此方法加入。

#8


但是你这样做根本没有做到,代码也与页面分离!

是不符合微软所推崇的MVC模型的阿