作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在更新面板中使用 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();
}
}
关于c# - 在 updatepanel asp.net c# 中的 AsyncFileUpload 中上传文件后显示 gridview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18682909/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!