问题描述
我希望有一个在服务器端运行但不回发的功能。所以我想出了以下代码。问题是代码执行正常,直到它到达String dir = Server.MapPath(files / News /+ FileUpload1.PostedFile.FileName),我不知道为什么这是happing,我不知道我怎么能解决这个问题因此它也会上传文件。
代码背后
protected void btn_Click( object sender,EventArgs e)
{
String name = TextBox1.Text;
String desc = TextBox2.Text;
// 代码在这里停止,不执行此点
字符串 dir = Server.MapPath( files /新闻/ + FileUpload1.PostedFile.FileName);
FileUpload1.PostedFile.SaveAs(dir);
字符串 SQL = 字符串 .Format( INSERT INTO SF_news
+ (newsHeader,newsDescription,newsSrc,newsDate,newsYear)
+ VALUES
+ ('{0}','{1}','{2 }','{3}'),
name,desc,FileUpload1.PostedFile.FileName);
new DBHelper()。runSQL(SQL);
}
HTML Code
< asp:ScriptManager ID = ScriptManager2 runat = server> < / asp:ScriptManager >
< asp:UpdatePanel ID = UpdatePanel1 runat = server跨度>>
< ContentTemplate>
< table>
< tr id = hiddenForm style = visibility:hidden;>
< td colspan = 4>
< div>
< span>新闻标题:< / span > < span>< asp:TextBox ID = TextBox1 runat = server> < / asp:TextBox > < / span >
< span>新闻说明:< / span > < span>< asp:TextBox ID = TextBox2 runat = server> < / asp:TextBox > < / span >
< span>新闻档案:< / span & GT; 跨度><跨度>
< dx:ASPxUploadControl ID = FileUpload1 runat = server UploadMod = auto宽度= 280px> < / dx:ASPxUploadControl > < / span >
< div>
< asp:按钮ID = btn runat = server Text = 按钮 OnClick = btn_Click />
< input type = button id = jsBtn />
< / div >
< / div >
< / td >
< / tr >
< / table >
< / ContentTemplate >
< / asp:UpdatePanel >
文件上传是一个在更新面板中不起作用的过程。
在您的代码中,您实际上是在支持数据,但是当您添加更新面板,因此页面的一部分只是在内部刷新。但请记住它实际上支持数据。
解决方案1:您可以使用触发器来执行操作。
< 触发器 >
< asp:PostBackTrigger ControlID = btn / >
< / Triggers >
[]
解决方案2:您可以使用Ajax控件工具包中的AsyncFileUpload
[]
并添加你的sql代码将其插入db。
I want to have a function that runs on server side but does not do a post back. So i have come up with the following code. The problem is that the code executes fine until it gets to the String dir = Server.MapPath("files/News/" + FileUpload1.PostedFile.FileName), im not sure why this is happing and i dont know how i can fix this so it also uploads the file.
Code Behind
protected void btn_Click(object sender, EventArgs e) { String name = TextBox1.Text; String desc = TextBox2.Text; //CODE STOPS HERE, DOES NOT EXECUTE PASS THIS POINT String dir = Server.MapPath("files/News/" + FileUpload1.PostedFile.FileName); FileUpload1.PostedFile.SaveAs(dir); String SQL = String.Format(" INSERT INTO SF_news " + " (newsHeader, newsDescription, newsSrc, newsDate, newsYear) " + " VALUES " + " ('{0}', '{1}', '{2}', '{3}')", name, desc, FileUpload1.PostedFile.FileName); new DBHelper().runSQL(SQL); }
HTML Code
<asp:ScriptManager ID="ScriptManager2" runat="server"></asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <table> <tr id="hiddenForm" style="visibility:hidden;"> <td colspan="4"> <div> <span>News Header: </span><span><asp:TextBox ID="TextBox1"runat="server"> </asp:TextBox></span> <span>News Description: </span><span><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></span> <span>News File: </span><span> <dx:ASPxUploadControl ID="FileUpload1" runat="server" UploadMod="auto" Width="280px"></dx:ASPxUploadControl></span> <div> <asp:Button ID="btn" runat="server" Text="Button" OnClick="btn_Click" /> <input type="button" id="jsBtn" /> </div> </div> </td> </tr> </table> </ContentTemplate> </asp:UpdatePanel>
这篇关于如何在没有回发的情况下使用文件上载插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!