本帖最后由 Lisa 于 2013-6-13 11:33 编辑
这是用空项目做的例子
test.aspx- <body>
- <x:PageManager ID="PageManager1" runat="server" AutoSizePanelID="SimpleForm1"/>
- <form id="form1" runat="server">
- <x:SimpleForm ID="SimpleForm1" BodyPadding="5px" runat="server" EnableBackgroundColor="true"
- ShowBorder="True" Title="表单" Width="350px" ShowHeader="True">
- <Items>
- <x:Image ID="imgPhoto" CssClass="photo" ImageUrl="~/images/blank.png" runat="server">
- </x:Image>
- <x:FileUpload runat="server" ID="filePhoto" ShowRedStar="false" LabelSeparator=""
- ButtonText="上传个人头像" ButtonOnly="true" Required="false" ShowLabel="true" Label=" "
- AutoPostBack="true" OnFileSelected="filePhoto_FileSelected">
- </x:FileUpload>
- <x:TextBox runat="server" Label="用户名" ID="tbxUserName" Required="true" ShowRedStar="true">
- </x:TextBox>
- <x:TextBox runat="server" Label="邮箱" ID="tbxEmail" Required="true" RegexPattern="EMAIL"
- ShowRedStar="true">
- </x:TextBox>
- <x:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" ValidateForms="SimpleForm1"
- Text="提交表单">
- </x:Button>
- </Items>
- </x:SimpleForm>
- <x:Label ID="labResult" CssClass="result" EncodeText="false" runat="server">
- </x:Label>
- </form>
- </body>
复制代码 test.aspx.cs- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void filePhoto_FileSelected(object sender, EventArgs e)
- {
- if (filePhoto.HasFile)
- {
- string fileName = filePhoto.ShortFileName;
- fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\", "_").Replace("/", "_");
- fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;
- filePhoto.SaveAs(Server.MapPath("~/upload/" + fileName));
- imgPhoto.ImageUrl = "~/upload/" + fileName;
- // 清空文件上传组件
- filePhoto.Reset();
- }
- }
- protected void btnSubmit_Click(object sender, EventArgs e)
- {
- if (imgPhoto.ImageUrl == "~/images/blank.png")
- {
- filePhoto.MarkInvalid("请先上传个人头像!");
- Alert.ShowInTop("请先上传个人头像!");
- return;
- }
- labResult.Text = "<p>用户名:" + tbxUserName.Text + "</p>" +
- "<p>邮箱:" + tbxEmail.Text + "</p>" +
- "<p>头像地址:" + imgPhoto.ImageUrl + "</p>";
- // 清空表单字段(注意,不要清空imgPhoto,否则就看不到上传的头像了)
- filePhoto.Reset();
- tbxEmail.Reset();
- tbxUserName.Reset();
- }
复制代码 就是在线示例“上传控件(自动回发)”的改动。
http://fineui.com/demo/#/demo/form/fileupload_autopostback.aspx |