gpt4 book ai didi

c# - 在 updatepanel asp.net c# 中的 AsyncFileUpload 中上传文件后显示 gridview

转载 作者:行者123 更新时间:2023-11-30 12:46:50 24 4
gpt4 key购买 nike

我想在更新面板中使用 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
}

最佳答案

在绑定(bind) GridView [GrdMadarek.DataBind()] 的每一行之后添加以下行

UpdatePanel1.Update();

此外,尝试使用 AsyncFileUpload 控件的事件 onuploadedcomplete 来保存您的文件。

protected void AsyncFileUpload1_UploadedComplete
(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{

if (AsyncFileUpload1.HasFile)
{
string strPath = MapPath("~/UploaddedFiles/") + Path.GetFileName(e.filename);
AsyncFileUpload1.SaveAs(strPath);
...
...
GrdMadarek.DataBind();
UpdatePanel1.Update();

}
}

引用 article here.

关于c# - 在 updatepanel asp.net c# 中的 AsyncFileUpload 中上传文件后显示 gridview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18682909/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com