2012年4月17日

在UpdatePanel上使用FileUpload上传文件(转载)

首先我很遗憾的告诉大家,因为微软的偷懒,目前UpdatePanel还不支持文件上传。我查了下,有两个办法,我已测试过了,真得很好用。

方案一的解决办法就是UpdatePanel中设置PostBackTrigger:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="Button1" />
</Triggers>
</asp:UpdatePanel>
而如果你又想在这个UpdatePanel上做点花样,比如加了一个asp:Panel, 可以通过按钮事件触发隐藏或显示的,你会发现FileUpload1并不能找到文件。。。

其实道理很简单,UpdatePanel中的内容是通过XmlHttp实时填充的,在你让他显示之前,查看页面源代码里面是空的。一个动态控件更新普通数据没问题,但上传文件就不行了,我的解决办法是用普通div代替asp:Panel,并写了2个函数来动态发送控制脚本,按钮事件中只要调用该函数即可:

<div id="Panel1"></div>

private void ShowPanel()
{
string script = "document.getElementByIdx_x('Panel1').style.display='';";
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "ShowPanel", script, true);
}
private void ClosePanel()
{
string script = "document.getElementByIdx_x('Panel1').style.display='none';";
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "ClosePanel", script, true);
}
[转]http://hi.baidu.com/honfei/blog/item/f0902b1190b3787bcb80c46c.html

方案二:

转:http://www.cnblogs.com/dlwang2002/archive/2006/07/11/447722.html

上次说到(和Atlas专家探讨Atlas的一些Bug和疑问),在Atlas里面使用FileUpload上传附件有困难:UpdatePanel每次回发的只是一个XmlHttp的请求和ViewState,并没有回发整个页面,所以,在server端无法获得FileUpload的文件。

首先想到的解决方法就是不使用UpdatePanel:弹出一个新窗口,上传文件。新窗口里面不使用UpdatePanel。这个方法当然可以。

今天忽然想起来,GMail里面的附件上传:后台上传文件,还不影响你前边邮件的编辑,也不整体页面刷新。这个模式够帅!不用打开新窗口,一个页面搞定,页面也不用刷新。

对比了一下,好象第二种方法更好一点。于是FireBug(AJAX程序的跟踪工具:fireBug0.4)了一下GMail,发现了里面的处理是这样的
<iframe scrolling="auto" frameborder="0" onload="try{if(top.js.init)top.js._IF_OnLoaded('v1')
<input type="file" onchange="top.js._CM_OnAttach(window,this)" name="f_ephki04a" size="50"/>
他的上传附件是放在一个iframe 里面的,然后控件的onchange事件里面有些处理。

果然是个不错的方法。
于是我做了这样的处理方案:
1:主页面中使用UpdatePanel,然后UpdatePanel里面放置的不是FileUpload控件,而是一个Iframe
2:这个iframe在链接一个新的页面,那个页面里面有FileUpload控件。
3:上传完毕后,告诉主页面上传得结果

先看一个直接使用FileUpload的例子:这个例子里面,服务端是无法找到上传文件的。
            <atlas:UpdatePanel ID="up1" Mode="Conditional" runat="server">
                <ContentTemplate>
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                 </ContentTemplate>
            </atlas:UpdatePanel>

看看,我们该如何实现
1:新建主页面Default.aspx
      在适当的位置,放置一个上传附件的UpdatePanel区域
    
            <atlas:UpdatePanel ID="up_attachment" Mode="Conditional" runat="server">
                <ContentTemplate>
                   <iframe id="file" name="file" src="attachment.aspx"></iframe>
                </ContentTemplate>
            </atlas:UpdatePanel>
2:新建上传文件的页面attachment.aspx,然后放上FileUpload控件
<div>
    <asp:FileUpload ID="FileUpload1" runat="server" />
          <asp:Button ID="Button1" runat="server" Text="OK" OnClick="Button1_Click" />
    </div>
3:在attachment.aspx里面,上传文件之后调用主页面的js,报告上传情况。这是函数原型:
    <script>
      window.top.callBack(fileName);
    </script>
4:Default.aspx主页面里面增加这个函数,处理返回值

    <script>
     function callBack(fileName)
     {
        document.getElementByIdx_x('Attach1').innerHTML=fileName;
     }
    </script>


然后,搞定。
这个方案,虽然还是两个页面,但是看起来处理都在一个页面里面,页面处理无刷新。

posted @ 2012-04-17 15:45 踏踏实实学编程 阅读(16) 评论(0) 编辑

2012年2月17日

基于C#操作Word文档中的Bookmark

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Office.Interop.Word;
using System.Data;

namespace WebApplication1
{
public static class FunctionHelp
{
#region Parameters
private static Microsoft.Office.Interop.Word._Document MyDoc;
private static Microsoft.Office.Interop.Word._Application MyWord;
private static object Nothing = System.Reflection.Missing.Value;
#endregion

#region Share Methods

private static Microsoft.Office.Interop.Word._Document OpenWord(Microsoft.Office.Interop.Word._Application wordApp,string templatePath)
{
object Visible = false;
object newTemp = false;
wordApp = new Microsoft.Office.Interop.Word.Application();
object templateFile = templatePath + "\\New.doc";
try
{
return wordApp.Documents.Open(ref templateFile, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Visible, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
}
catch
{
return null;
}
}

private static bool SaveWord(Microsoft.Office.Interop.Word._Document word,string savePath)
{
object Visible = false;
object filename = savePath + "\\King.doc";
try
{
word.SaveAs(filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
ref Nothing, ref Nothing, ref Nothing, ref Visible, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
CloseWord(MyDoc,MyWord);
return true;
}
catch (Exception ex)
{
CloseWord(MyDoc, MyWord);
return false;
}
}

private static void CloseWord(Microsoft.Office.Interop.Word._Document word,Microsoft.Office.Interop.Word._Application wordApp)
{
object missingValue = Type.Missing;
object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;
if (word != null)
word.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
if (wordApp != null)
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}

#endregion

#region Static Methods
/// <summary>
/// 根据Word文档内的BookMark添加数据
/// </summary>
/// <param name="dtSource"></param>
/// <param name="templatePath"></param>
/// <param name="savePath"></param>
/// <returns>是否保存成功</returns>
public static bool ExportWord(DataTable dtSource, string templatePath, string savePath)
{
MyDoc = OpenWord(MyWord, templatePath);
if (MyDoc != null)
{
foreach (Microsoft.Office.Interop.Word.Bookmark bm in MyDoc.Bookmarks)
{
bm.Select();
switch (bm.Name)
{
case "书签名":
bm.Range.Text = 根据dtSource给书签赋值
break;
...
}
}
}
return SaveWord(MyDoc, savePath);
}

#endregion

#region Public Methods
#endregion
}
}

posted @ 2012-02-17 10:51 踏踏实实学编程 阅读(135) 评论(0) 编辑

2012年1月31日

2012年2月

因为要读在职研究生,所以只能相对少一点的任务:

1. 帮助完成毕业设计(报销管理系统)

2. 学习CLR VIA C# 1-3章内容

 

仅此对自己的忠告:一定要完成这点任务以便今后任务的顺利实施。

posted @ 2012-01-31 13:57 踏踏实实学编程 阅读(9) 评论(0) 编辑

2012年1月17日

C# 方法中的this参数

先看下面的代码:

 

public static class StringExtension
{
public staticvoid Foo(this string s)
{
  Console.WriteLine("Foo invoked for {0}",s);
 }
}

为什么这里会有一个this关键字,做什么用?其 实这就是扩展方法!这个扩展方法在静态类中声明,定义一个静态方法,其中第一个参数定义可它的扩展类型。Foo()方法扩展了String类,因为它的第 一个参数定义了String类型,为了区分扩展方法和一般的静态方法,扩展方法还需要给第一个参数使用this关键字。

现在就可以使用带string类型的Foo方法了:

string s="Hello"; s.Foo();

结果在控制台上显示Foo invoked for Hello ,因为Hello是传送给Foo方法的字符串。

 

归纳扩展方法可以写入最初没有提供该方法的类中。还可以把方法添加到实现某个接口的任何类中,这样多个类可以使用相同的实现代码。

posted @ 2012-01-17 15:45 踏踏实实学编程 阅读(25) 评论(0) 编辑

2012年1月3日

2011年总结和2012年计划

2011年七月份刚刚从学校毕业。对于刚刚进入职场来说感觉这一路走下来也很不容易。

2011年年初开始正式进入HT实习,由于一直做MIS系统做了将近半年,感觉无聊,果断的走人。在四月份来到了我所期望的公司SS。这是我2011年实现的第一个有意义的目标。

进入SS以后,我很顺利的在毕业之前进入了期望的项目组。项目组的人很好。因为接触的是新的东西。原先在HT做的一直是网页的工作,而且连CSS\DIV都不知道。本人不才,现在JS和JQuery也是一知半解。还好,在新的公司由于是Winform的开发工作,之后又使用了WPF编程,所以学到的东西也挺多。再加上工作不是特别忙,所以上网学习一些架构之类的知识。所以,总体来说,在新的公司里面,我学会了Winform,WPF,CSS\DIV,部分的JS和JQuery。自己觉得半年的时间学了这些知识也差不多了(自己要求不高)。

因为今年的空闲时间比较多,所以我也去考了在职的研究生,考上了期望的浙江大学的软件工程专业。在2012年春入学。

所以,2011年总体来说还是比较的顺利。这也归功于我有个好的leader和好同事。

 

对于2012年的目标,对自己初步的要求:

在工作方面,顺利并且按质按量的完成公司的开发任务,学好.NET架构,WorkFlow和WCF。自己做一个项目软件当做前面学习的知识的练手。

在学习方面,顺利的完成学校的学习任务。

我相信,我行!

posted @ 2012-01-03 10:01 踏踏实实学编程 阅读(127) 评论(0) 编辑

2011年12月12日

javascript备注

摘要: 当用户进入或离开页面时就会触发 onload 和 onUnload 事件。onload 事件常用来检测访问者的浏览器类型和版本,然后根据这些信息载入特定版本的网页。onload 和 onUnload 事件也常被用来处理用户进入或离开页面时所建立的 cookies。例如,当某用户第一次进入页面时,你可以使用消息框来询问用户的姓名。姓名会保存在 cookie 中。当用户再次进入这个页面时,你可以使用另一个消息框来和这个用户打招呼:"Welcome John Doe!"。onFocus, onBlur 和 onChangeonFocus、onBlur 和 onChange 事件通阅读全文

posted @ 2011-12-12 15:09 踏踏实实学编程 阅读(30) 评论(0) 编辑

2011年11月22日

DataTable转换成Json格式

摘要: using System;using System.Collections.Generic;using System.Text;using System.Data;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { Console.Write(DataTableToJson(MyTable())); Console.Read(); } //"{\"totalpage\":\...阅读全文

posted @ 2011-11-22 13:05 踏踏实实学编程 阅读(268) 评论(0) 编辑

2011年9月7日

winform 多表头的实现

摘要: 现在在网上基本上有两种方法实现winform多表头。一种是将winform表头重画,代码如下:privatevoidDataGridViewEx_CellPainting(objectsender,DataGridViewCellPaintingEventArgse){if(e.RowIndex==-1){//intw=dataGridView1.HorizontalScrollingOffset+dataGridView1.TopLeftHeaderCell.Size.Width+dataGridView1.Columns[0].Width+10;RectanglenewRect=newRe阅读全文

posted @ 2011-09-07 13:23 踏踏实实学编程 阅读(242) 评论(0) 编辑

2011年8月15日

ASP.NET 进度条的制作

摘要: 制作进度条往往分为两种:1. 真实的进度条,显示你进程的百分比。这样的话我的做法是给你的初始化函数里面设置几个点,分别取名是干什么的。然后在你跟新的时候进度条并会显示目前正在干什么。当然如果你的电脑很好,初始化很快的话就很难看出来,一闪而过,你可以设置System.Threading.Thread.Sleep(2000)停止两秒来看看效果。例子如下: /*****ProcessMesg为进度页面的一个label显示空间*******/ SetCurrentThreadCulture(); LoginProcess loginPro = new LoginProcess(); loginPro.阅读全文

posted @ 2011-08-15 15:13 踏踏实实学编程 阅读(244) 评论(0) 编辑

2011年8月4日

改善ASP.Net Menu 控制区域的问题

摘要: 原先的Menu只能点击文字才能触发点击事件。如果需要让整块区域点击有效的话,很简单,只需更改StaticMenuItemStyle 和 DynamicMenuItemStyle的css属性。这两个的CSS属性里面只要添加display="block"。然后再适当的调整下CSS就可以了。阅读全文

posted @ 2011-08-04 17:34 踏踏实实学编程 阅读(29) 评论(0) 编辑

仅列出标题  下一页

导航

统计

公告