我想在更新面板中使用 AsyncFileUpload
上传文件后,我想在gridview中显示上传的文件。
我使用这种方法,但是上传后我看不到gridview。请帮我看看我的gridview。
我的脚本是:
<script type="text/javascript" language="javascript">
var ifIgnoreError = false;
function uploadError(sender, args) {
if (ifIgnoreError) {
$get("<%=lblStatus.ClientID%>").style.color = "red";
$get("<%=lblStatus.ClientID%>").innerHTML = " File not permitted ";
alert("Allowed file is .jpg ,.jpeg,.png,.gif ");
}
else {
alert(args.get_message());
}
}
function StartUpload(sender, args) {
var fileName = args.get_fileName();
var fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1);
if (fileExtension == 'jpg' || fileExtension == 'jpeg' || fileExtension == 'png' || fileExtension == 'gif') {
$get("<%=lblStatus.ClientID%>").style.color = "green";
$get("<%=lblStatus.ClientID%>").innerHTML = "Uploading...";
}
else {
ifIgnoreError = true;
sender._stopLoad();
}
}
function UploadComplete(sender, args) {
$get("<%=lblStatus.ClientID%>").style.color = "#33CCFF";
$get("<%=lblStatus.ClientID%>").innerHTML = "The file was uploaded successfully";
//If this code is uncomment, I can't see gridview
var btnClick = document.getElementById("btnClick");
btnClick.click();
//If this code uncomment, file uploaded twice
// __doPostBack('UpdatePanel1', '');
}
</script>
和 html 是:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnClick" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:AsyncFileUpload ID="AsyncFileUpload1" runat="server" ClientIDMode="AutoID"
OnClientUploadComplete="UploadComplete" OnClientUploadError="uploadError"
OnClientUploadStarted="StartUpload" ErrorBackColor="#66CCFF" onuploadedcomplete="AsyncFileUpload1_UploadedComplete" ThrobberID="Throbber"
UploaderStyle="Traditional" UploadingBackColor="#66CCFF" Width="400px" />
<asp:Button runat="server" ID="btnClick" Text="Update grid" style="display:none"/>
<asp:Label ID="Throbber" runat="server">
<img src="../../images/Pages/loading19.gif" align="absmiddle" alt="loading" />
</asp:Label>
<asp:Label ID="lblStatus" runat="server" Style="font-family: tahoma; font-size: 11px;"></asp:Label>
<asp:GridView ID="GrdMadarek" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="ImgMadarek" runat="server" Width="50px" Height="40px" ImageUrl='<%#Eval("MadrakFileName","~/files/{0}")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkBtnDelMadrek" runat="server">delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
aspx.cs 中的代码是:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Params.Get("__EVENTTARGET") == "UploadPostback")
{
}
else
{
if (AsyncFileUpload1.HasFile)
{
string FinalFileName = Path.GetExtension(AsyncFileUpload1.FileName);
string strPath = MapPath("~/files/") + FinalFileName;
AsyncFileUpload1.SaveAs(strPath);
if (Session["MadarekList"] != null)
{
var MadarekList = (List<MadrakDetails>)Session["MadarekList"];
MadrakDetails Madrak = new MadrakDetails();
Madrak.MadrakFileName = FinalFileName;
MadarekList.Add(Madrak);
Session["MadarekList"] = MadarekList;
GrdMadarek.DataSource = MadarekList;
GrdMadarek.DataBind();
}
else
{
var MadarekList = new List<MadrakDetails>();
MadrakDetails Madrak = new MadrakDetails();
Madrak.MadrakFileName = FinalFileName;
MadarekList.Add(Madrak);
Session["MadarekList"] = MadarekList;
GrdMadarek.DataSource = MadarekList;
GrdMadarek.DataBind();
}
}
}
}
public class MadrakDetails
{
string madrakFileName = string.Empty;
public string MadrakFileName
{
get { return madrakFileName; }
set { madrakFileName = value; }
}
}
protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
// no code here
}