Javascript 将值从服务器传递到客户端不起作用
Javascript to pass value from server to client doesnt work
我有一个文件上传控件和一个按钮 webform.When 我在选择文件上传后单击按钮,它应该保存图像并用 GUID 重命名它。
protected void btnUpload_Click(object sender, EventArgs e)
{
string fileName = string.Empty;
string filePath = string.Empty;
string extension = string.Empty;
try
{
//Check if Fileupload control has file in it
if (FileUpload1.HasFile)
{
// Get selected image extension
extension = Path.GetExtension(FileUpload1.FileName).ToLower();
//Check image is of valid type or not
if (extension == ".jpg" || extension == ".jpeg" || extension == ".png" || extension == ".gif" || extension == ".bmp")
{
//Cretae unique name for the file
fileName = Guid.NewGuid().ToString() + extension;
//Create path for the image to store
HiddenField1.Value = fileName;
filePath = Path.Combine(Server.MapPath("~/Images"), fileName);
//Save image in folder
FileUpload1.SaveAs(filePath);
//Show the panel and load the uploaded image in image control.
//pnlCrop.Visible = true;
}
上面的代码工作正常,保存图像并将 GUID 传递给 hiddenfield.Now 我想将 hiddenfield 的值传递给客户端变量,然后将其显示为警报。
<script type="text/javascript">
function showfilename() {
setTimeout(function () {
var dpGUID = document.getElementById('<%= HiddenField1.ClientID %>').value;
alert(dpGUID);
},3000)
}
</script>
使用超时的原因?因为我想在单击按钮时为它分配值后读取 hiddenfield 的值。
注意:我在 Buttonclick.One 客户端和服务器端使用两个函数,如下所示:
<asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Button" OnClientClick="showfilename()" />
可能吗?如果是,可能是什么导致了问题?
尝试使用 RegisterClientScriptBlock
protected void btnUpload_Click(object sender, EventArgs e)
{
/*All your code here*/
//instead of HiddenField1.Value = fileName; write the line below
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "callJsFunction", "alert('" + fileName + "');", true);
}
现在您可以去掉 OnClientClick="showfilename()
和 HiddenField
我有一个文件上传控件和一个按钮 webform.When 我在选择文件上传后单击按钮,它应该保存图像并用 GUID 重命名它。
protected void btnUpload_Click(object sender, EventArgs e)
{
string fileName = string.Empty;
string filePath = string.Empty;
string extension = string.Empty;
try
{
//Check if Fileupload control has file in it
if (FileUpload1.HasFile)
{
// Get selected image extension
extension = Path.GetExtension(FileUpload1.FileName).ToLower();
//Check image is of valid type or not
if (extension == ".jpg" || extension == ".jpeg" || extension == ".png" || extension == ".gif" || extension == ".bmp")
{
//Cretae unique name for the file
fileName = Guid.NewGuid().ToString() + extension;
//Create path for the image to store
HiddenField1.Value = fileName;
filePath = Path.Combine(Server.MapPath("~/Images"), fileName);
//Save image in folder
FileUpload1.SaveAs(filePath);
//Show the panel and load the uploaded image in image control.
//pnlCrop.Visible = true;
}
上面的代码工作正常,保存图像并将 GUID 传递给 hiddenfield.Now 我想将 hiddenfield 的值传递给客户端变量,然后将其显示为警报。
<script type="text/javascript">
function showfilename() {
setTimeout(function () {
var dpGUID = document.getElementById('<%= HiddenField1.ClientID %>').value;
alert(dpGUID);
},3000)
}
</script>
使用超时的原因?因为我想在单击按钮时为它分配值后读取 hiddenfield 的值。
注意:我在 Buttonclick.One 客户端和服务器端使用两个函数,如下所示:
<asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Button" OnClientClick="showfilename()" />
可能吗?如果是,可能是什么导致了问题?
尝试使用 RegisterClientScriptBlock
protected void btnUpload_Click(object sender, EventArgs e)
{
/*All your code here*/
//instead of HiddenField1.Value = fileName; write the line below
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "callJsFunction", "alert('" + fileName + "');", true);
}
现在您可以去掉 OnClientClick="showfilename()
和 HiddenField