在线急求,c# 前台JS调用CS后台变量问题
protected string mediaTitleClass = null;
protected string mediaSortString = null;
protected string mediaName = null;
protected string mediaDesc = null;
protected string mediaManID = null;
protected string mediaManName = null;
protected string mediaOrgName = null;
protected string mediaInstrument = null;
protected string mediaType = null;
protected string mediaImportance = null;
protected string mediaAddTime = null;
protected string mediaShortTime = null;
protected string mediaHitNum = null;
protected string mediaDownNum = null;
protected string mediaFileUrl = null;
protected string strfile = null;
protected void Page_PreInit(object sender, EventArgs e)
{
object mediaIDStr = Request.QueryString["id"];
if (mediaIDStr == null)
{
Response.Redirect("403.htm");
}
int meidaID;
//取到了媒体的ID
if (Int32.TryParse(mediaIDStr.ToString().Trim(), out meidaID))
{
Dictionary<string, object> propertyTable = MediaHelper.GetMediaProperty(mediaIDStr as string, new List<string>() { "MediaSort" });
int mediaSort = Int32.Parse(propertyTable["MediaSort"].ToString());
//判断需要多少权限,可以看 视频/音频/图片
GrantPoint wannaPower = 0;
switch (mediaSort)
{
case 1:
wannaPower = GrantPoint.VIDEO_PLAY;
break;
case 2:
wannaPower = GrantPoint.AUDIO_PLAY;
break;
case 3:
wannaPower = GrantPoint.PHOTO_PLAY;
break;
default:
break;
}
if (!UserProfileHelper.JudgePermission(Session["userinfo"], wannaPower))
{
Response.Redirect("403.htm");
}
else
{
List<string> propNames = new List<string>() {
"MediaSort", "Name", "FileUrl", "Description",
"PoliceManNum", "InstrumentID", "Importance", "Type" ,
"AddedTime","ShootTime","IsPermanent","ManName",
"OrgName","HitNum","DownNum"
};
propertyTable = MediaHelper.GetMediaProperty(mediaIDStr as string, propNames);
//页面变量赋值开始
mediaTitleClass = mediaSort == 1 ? "type_video" : (mediaSort == 2 ? "type_audio" : "type_pic");
mediaSortString = mediaSort == 1 ? "视频" : (mediaSort == 2 ? "音频" : "图片");
mediaName = propertyTable["Name"] as string;
mediaDesc = propertyTable["Description"] as string;
mediaManID = propertyTable["PoliceManNum"] as string;
mediaManName = propertyTable["ManName"] as string;
mediaOrgName = propertyTable["OrgName"] as string;
mediaInstrument = propertyTable["InstrumentID"] as string;
mediaImportance = propertyTable["Importance"] as string;
mediaType = propertyTable["Type"] as string;
mediaAddTime = String.Format("{0:yyyy-MM-dd HH:mm:ss}", (propertyTable["AddedTime"] as DateTime?));
mediaShortTime = String.Format("{0:yyyy-MM-dd HH:mm:ss}", (propertyTable["ShootTime"] as DateTime?));
mediaHitNum = propertyTable["HitNum"].ToString();
mediaDownNum = propertyTable["DownNum"].ToString();
mediaFileUrl = propertyTable["FileUrl"].ToString();
//增加一个访问量
MediaHelper.IncreaseMediaHitNum(mediaIDStr.ToString().Trim());
}
}
//媒体ID参数非法了
else
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('请不要尝试非法操作'); ", true);
}
}
<script type="text/javascript">
/* *初始化绑定
*/
$(document).ready(function() {
var mUrl = "<%=mediaFileUrl%>";
alert(mUrl);
});
public override void RenderControl(HtmlTextWriter writer)
{
base.RenderControl(writer);
RenderScript(writer);
}
private void RenderScript(HtmlTextWriter writer)
{
url = "test";
StringBuilder script = new StringBuilder();
script.Append("<script language="javascript" type="text/javascript">");
script.Append("$(function(){(alert('" + url + "'));});");
script.Append("</script>");
writer.Write(script.ToString());
}
那楼主可以通过css这个span设置成隐藏,然后再通过jquery调用这个变量,例如:
<span id="s1" style="display:none"><%=mediaFileUrl%></span>
<script type="text/javascript">
$(document).ready(function() {
var mUrl =$("#s1").val() ;
alert(mUrl);
});
[解决办法]
哦,纠正一下 var mUrl =$("#s1").val() ;应该改成 var mUrl =$("#s1").text() ;