服务端按钮OnClick点两次才能执行。。。。

gwf25sz 2011-04-08 11:41:41
如题,高手请解答,围观的请顶起!!!
下面是html代码,C#代码(见1楼):
<%@ Page Language="C#" AutoEventWireup="true" Codebehind="FileList.aspx.cs" Inherits="Honeywell.FileList" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Honeywell HSM E-PR</title>
<link href="~/Css/PublicStyle.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="../../dialogs/lhgcore.min.js"></script>

<script type="text/javascript">
var dg = frameElement.lhgDG;


window.onload = function()
{
dg.addBtn('close', 'Close' ,dg.cancel);
}

function Search()
{
__doPostBack('lbtnSearch','');
}

function setValue(){
document.getElementById("ffFile").value = document.getElementById("fupload").value;
}
</script>

</head>
<body>
<form id="form1" runat="server">
<asp:HiddenField ID="hfPRNumber" runat="server" />
<div>
<div id="dvUp" runat="server">
Files:<asp:TextBox ID="ffFile" runat="server"></asp:TextBox>
<asp:FileUpload ID="fupload" runat="server" Style="display: none;" onchange="setValue()" />
<input type="button" value="Browsing" onclick="document.getElementById('fupload').click();"
class="ltBtn" />
<br />
Description:<br />
<asp:TextBox ID="txtDescription" runat="server" Height="85px" TextMode="MultiLine"
Width="409px"></asp:TextBox>
<br />
<%-- *****************************下面这个按钮要点两次才能执行OnClick事件***************************** --%>
<asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Upload" CssClass="ltBtn" />
</div>
<asp:DataList ID="dlList" DataKeyField="ID" runat="server" OnItemDataBound="dlList_ItemDataBound"
OnItemCommand="dlList_ItemCommand">
<HeaderTemplate>
<table class="listTable" style="width: 100%;" cellpadding="0" cellspacing="0" summary="The technical specifications of the Apple PowerMac G5 series">
<caption>
</caption>
<tr>
<th>
File Name</th>
<th>
Size</th>
<th>
Description</th>
<th>
Uploaded By</th>
<th>
Date Modified</th>
<th>
Edit</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%#Eval("FileName") %>
</td>
<td>
<span style="color: Red;">
<%#Eval("Size") %>
</span>KB
</td>
<td>
<%#Eval("Description") %>
</td>
<td>
<asp:Label ID="upBy" Text='<%#Eval("EName")%>' runat="server"></asp:Label>
<asp:HiddenField ID="hfUpBy" Value='<%#Eval("UploadBy")%>' runat="server" />
</td>
<td>
<%#Eval("Created")%>
</td>
<td>
<asp:HiddenField ID="hfAttachment" Value='<%#Eval("Attachment") %>' runat="server" />
<a href='<%#Eval("Attachment") %>' target="_blank">Open</a>
<asp:LinkButton ID="lbtnDelete" CommandName="delete" runat="server" Text="Delete"
OnClientClick="return confirm('Are sure delete this data?')"></asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
...全文
208 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
gwf25sz 2011-04-08
  • 打赏
  • 举报
回复
放到后台,结果一样
子夜__ 2011-04-08
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 gwf25sz 的回复:]

引用 3 楼 sandy945 的回复:
<input type="button" value="Browsing" onclick="document.getElementById('fupload').click();"
class="ltBtn" />


onclick="document.getElementById('fupload').click();"

是这句的问……
[/Quote]
function setValue(){
document.getElementById("ffFile").value = document.getElementById("fupload").value;
}

解决方案 把这个逻辑在后台实现
ycproc 2011-04-08
  • 打赏
  • 举报
回复
要不你放在后台的 方法里面 注册进去
消失的尘芥 2011-04-08
  • 打赏
  • 举报
回复
嘿嘿,
不用改,按两次安全一点,像有点交易信息、、、、、、
gwf25sz 2011-04-08
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sandy945 的回复:]
<input type="button" value="Browsing" onclick="document.getElementById('fupload').click();"
class="ltBtn" />


onclick="document.getElementById('fupload').click();"

是这句的问题
[/Quote]

的确是这个问题,但解决方案????
heaventohell17 2011-04-08
  • 打赏
  • 举报
回复
顶起....这个问题我也碰到过,不知如何解决
阿非 2011-04-08
  • 打赏
  • 举报
回复
<input type="button" value="Browsing" onclick="document.getElementById('fupload').click();"
class="ltBtn" />


onclick="document.getElementById('fupload').click();"

是这句的问题
阿非 2011-04-08
  • 打赏
  • 举报
回复
不是按钮的问题



function setValue(){
document.getElementById("ffFile").value = document.getElementById("fupload").value;
}

的问题

input file 由于安全的原因,不允许 js 或其他方式的赋值,只能是用户自己点击

gwf25sz 2011-04-08
  • 打赏
  • 举报
回复

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.WebControls;
using System.IO;

namespace Honeywell
{
public partial class FileList : System.Web.UI.Page
{
private HttpCookie cookie = null;
private string userAccount = "";
private string sql = "";
private string sqlPath = "\\Uploads\\";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}

/// <summary>
/// Bind datalist
/// </summary>
private void BindData()
{
if (Request.QueryString["prnum"] != null && Request.QueryString["prnum"].Trim() != "")
{
hfPRNumber.Value = Request.QueryString["prnum"].Trim();
sql = "SELECT *,(SELECT EnglishName FROM PR_Users WHERE USERID=UploadBy) as EName FROM PR_Attachment WHERE PRNumber = '" + hfPRNumber.Value.Trim() + "'";
DataTable dt = DBBase.GetDataTable(sql);
dlList.DataSource = dt;
dlList.DataBind();
}
if (Request.QueryString["status"] != null)
{
dvUp.Style.Add("display", "none;");
}
}

protected void dlList_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
HiddenField hfUpBy = (HiddenField)e.Item.FindControl("hfUpBy");

cookie = Request.Cookies["adUser"];
userAccount = cookie == null ? "test" : cookie["account"].Trim();
LinkButton lbtn = (LinkButton)e.Item.FindControl("lbtnDelete");
lbtn.Visible = hfUpBy.Value.Trim() == userAccount ? true : false;
lbtn.Visible = Request.QueryString["status"] != null ? false : true;
}
}

protected void dlList_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "delete")
{
sql = "DELETE PR_Attachment WHERE ID = '" + dlList.DataKeys[e.Item.ItemIndex].ToString() + "'";
DBBase.Update(sql);
BindData();
HiddenField hf = (HiddenField)e.Item.FindControl("hfAttachment");
File.Delete(Server.MapPath(hf.Value));
WebPage.ShowMessage("Delete success!");
}
}

protected void btnSave_Click(object sender, EventArgs e)
{
if(ffFile.Text == "")
{
WebPage.ShowMessage("Please select the file.");
return;
}
if (txtDescription.Text == "")
{
WebPage.ShowMessage("Please add descriptions to the file you have uploaded.");
return;
}

string Path = Server.MapPath(@"~/Uploads/");

//如果路径不存在,则创建
if (System.IO.Directory.Exists(Path) == false)
{
System.IO.Directory.CreateDirectory(Path);
}

//组合路径,file.GetName()取得文件名
Path = Path + fupload.FileName;
sqlPath += fupload.FileName;
//保存
fupload.SaveAs(Path);

cookie = Request.Cookies["adUser"];
userAccount = cookie == null ? "test" : cookie["account"].Trim();
string pNo = Request.QueryString["pno"] != null ? Request.QueryString["pno"].Trim() : "test";
sql = "INSERT INTO PR_Attachment VALUES('" + pNo + "','" + fupload.FileName + "','" + fupload.PostedFile.ContentLength / 1024 + "','" + sqlPath + "','" + txtDescription.Text.Trim() + "','" + userAccount + "','" + DateTime.Now.ToString("MM/dd/yy") + "')";
DBBase.Update(sql);
BindData();
WebPage.ShowMessage("Upload file success!");
}

protected void btnSave_Command(object sender, CommandEventArgs e)
{
if (ffFile.Text == "")
{
WebPage.ShowMessage("Please select the file.");
return;
}
if (txtDescription.Text == "")
{
WebPage.ShowMessage("Please add descriptions to the file you have uploaded.");
return;
}

string Path = Server.MapPath(@"~/Uploads/");

//如果路径不存在,则创建
if (System.IO.Directory.Exists(Path) == false)
{
System.IO.Directory.CreateDirectory(Path);
}

//组合路径,file.GetName()取得文件名
Path = Path + fupload.FileName;
sqlPath += fupload.FileName;
//保存
fupload.SaveAs(Path);

cookie = Request.Cookies["adUser"];
userAccount = cookie == null ? "test" : cookie["account"].Trim();
string pNo = Request.QueryString["pno"] != null ? Request.QueryString["pno"].Trim() : "test";
sql = "INSERT INTO PR_Attachment VALUES('" + pNo + "','" + fupload.FileName + "','" + fupload.PostedFile.ContentLength / 1024 + "','" + sqlPath + "','" + txtDescription.Text.Trim() + "','" + userAccount + "','" + DateTime.Now.ToString("MM/dd/yy") + "')";
DBBase.Update(sql);
BindData();
WebPage.ShowMessage("Upload file success!");
}
}
}
gwf25sz 2011-04-08
  • 打赏
  • 举报
回复
找到完美方案:
http://www.cnblogs.com/Charles2008/archive/2008/07/20/1247084.html

结贴~
阿非 2011-04-08
  • 打赏
  • 举报
回复
原因已经和你说过了,更改浏览按钮的样式是不能了,只能用div做个层放在file 上

让用户以为点的不是file ,从而实现文件上传

http://www.cnblogs.com/cloudgamer/archive/2008/10/20/1314766.html

还有很多jq插件 或 采用flash 上传的组件
gwf25sz 2011-04-08
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 sandy945 的回复:]
http://blog.csdn.net/cpp2017/archive/2009/08/06/4418202.aspx

给你贴一个慕白兄的例子吧 ~

类似的有很多,可以自己搜索下。
[/Quote]
我主要是想把浏览按钮的样式改掉。。。。没想到会有这么多问题。。。
我现在后台可以获取路径,但保存到服务器只能用 SaveAs,用File.Copy 是不行的。。。File流写入也不行。。。。。 而 FileUpload.FileName 永远是 ""
阿非 2011-04-08
  • 打赏
  • 举报
回复
http://blog.csdn.net/cpp2017/archive/2009/08/06/4418202.aspx

给你贴一个慕白兄的例子吧 ~

类似的有很多,可以自己搜索下。
静_心 2011-04-08
  • 打赏
  • 举报
回复
应该是有冲突导致

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧