本文目录一览:
asp.net如何在后台文件中编写js
我晕~~~~~~ js文件是运行在前台的文件,你在后台怎么可能产生js文件?
下面是在后台代码中生成的随机数字验证码
/// summary
/// 生成一个验证码
/// /summary
using System.Drawing;
using System.Drawing.Imaging;
private void CreateValidateCode()
{
using(Bitmap bitmap = new Bitmap(80, 24))
{
using(Graphics g = Graphics.FromImage(bitmap))
{
Random rand = new Random();
int code = rand.Next(1000, 9999);
string strcode = code.ToString();
g.DrawString(strcode, new Font("宋体", 18), Brushes.Red, 10, 0);
Session["ValidateCode"] = strcode; //使用Session保存数字验证码
ImageButton1.ImageUrl = "source/1.jpg"; // 使用source文件夹下面的图片 1.jpg
}
string abspath = System.Web.HttpContext.Current.Server.MapPath(@"source/1.jpg"); // 使用source文件夹下面的图片 1.jpg, 作为输出内存中的图片的载体
bitmap.Save(abspath, ImageFormat.Jpeg);
}
}
asp.net 后台调用JS
这样做,不太现实。
首先,您的按钮是控件来的,随非您将DIv也加入runat="server"
,把DIV显示出来,不过,这种方式,一般都不行,因为,当你把DIV显示出来了,那button按钮就会刷新页面一次,DIV也不会显示出来了。
您可以对button按钮加入OnClientClick事件,写JS显示出来。
.NET编程,关于后台调用js函数的问题?
1) 如果这段js代码是执行某个操作直接触发的(而不是在之前还要运行额外的后台代码)的话,你可以直接给控件(比如button)的OnClientClick属性中添加对此方法的调用(在前台页面,非代码页面),用法如下asp:Button id="button" Text="button" ... OnClientClick="OpenManagerPage()" .../asp:Button
2) 如果在跳转页面之前还要运行某些前置代码的话,推荐使用Response.Redirct()方法。示例如下:
protectted void button_Click(object sender, EventArgs e)
{
......;//前置代码
Reponse.Redirect("TrainingList.aspx");
//Reponse.Redirect(ResolveUrl("TrainingList.aspx"));
}