PDA

View Full Version : دسترسی به مقدار متغیر سمت سرور در جاوا اسکریپت



mahdidabaghi
شنبه 25 آذر 1391, 00:11 صبح
سلام
ایا راهی هست متغیری که سمت سرور مقدار گرفته رو بتونیم از مقدارش توی جاوا اسکرپت استفاده کنیم؟(مثلا مقدارشو alert کنیم)

malloc
شنبه 25 آذر 1391, 00:20 صبح
ScriptManager.RegisterStartupScript(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('" + txtPassword.Text + "')", true);

که تو میتونی جای txtpassword.text هر مقداری و بزاری

mahdidabaghi
شنبه 25 آذر 1391, 00:47 صبح
مهندس من کد شما رو در یه function قرار دادم(که این تابع توی تگ <script> قرار داره)ولی جواب نداد.حتی تو اون قسمت alert کدتون یه مسیج متنی گذاشتو ولی بازم نشد.مشکل از کجایه بنظر شما؟

micro_bhk
شنبه 25 آذر 1391, 08:56 صبح
تو کدت متغیر سراسر رو به صورت public یا protected تعریف کن و جایی که می خوای استفاده کنی با <%= متغیر %> میتونی محتواشو بخونی، مثلا:

protected int test = 0;

و تو JS

var num = <%= test %>

samira3
شنبه 25 آذر 1391, 09:33 صبح
سلام
من راه حل microرفتم جواب مي ده مي توني session هم تعزيف كني . اين قسمت كد نويسي را دوست دارم . شما حتي مي تونيد از داخل كد جاوا تابع سي را هم صدا بزنيد بعد مقدار محاسبه شده در تابع د ركد جاوا استفاده كنيد.

mahdidabaghi
یک شنبه 26 آذر 1391, 00:36 صبح
سلام
من راه حل microرفتم جواب مي ده مي توني session هم تعزيف كني . اين قسمت كد نويسي را دوست دارم . شما حتي مي تونيد از داخل كد جاوا تابع سي را هم صدا بزنيد بعد مقدار محاسبه شده در تابع د ركد جاوا استفاده كنيد.مرسی
میشه بیشتر توضیح بدبد.مخصوصا که بشه تابع سی شارپ رو با جاوا فراخونی کرد.اگه یه سمپل هم بذارین که دیگه...

malloc
یک شنبه 26 آذر 1391, 00:52 صبح
مهندس من کد شما رو در یه function قرار دادم(که این تابع توی تگ <script> قرار داره)ولی جواب نداد.حتی تو اون قسمت alert کدتون یه مسیج متنی گذاشتو ولی بازم نشد.مشکل از کجایه بنظر شما؟
احتمالا باید یه ScriptManager به پروژه اضافه کنی

samira3
یک شنبه 26 آذر 1391, 12:41 عصر
سلام


function ShowPopup(str) {


$find('mpext21').show();
// var myvar = str;
// '<%#Session["day"] ="' + myvar + '"%>';

document.getElementById('Label2').innerHTML = str.value + " " + document.getElementById('Label1').innerHTML;
// $get('Button4').click();
// document.getElementById('Text1').innerHTML = PageMethods.SetDownloadPath(str);
PageMethods.SetDownloadPath(str.value,onSucceeded, onFailed)
// document.getElementById('Label4').innerHTML = str + " " + document.getElementById('Label1').innerHTML + "";
// str.className = " styleMeevent";
}


اين كد جاوا كه تابع سي صدا زدم

اين هم كد سي

[System.Web.Services.WebMethod(true)] // true if you're using Session
//[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)] // httpget isn't necessary but often a good idea
public static string[] SetDownloadPath(string day )
{
Page objp = new Page();
string bolean="true";
string user = "";
objp.Session["day"] = day;
EventBL calevent = new EventBL();
calevent.Year = Year;
calevent.Month = Month;
calevent.Day = day;
DataSet ds = calevent.Fill();
// Text1.Attributes.Add("text", "kghkghk");
TextBox Text = new TextBox();

// objp.Session["sCountry"] = ds.Tables[0].Rows[0][1].ToString();
string textt="";
if (ds.Tables[0].Rows.Count > 0)
{
textt = ds.Tables[0].Rows[0][1].ToString();
// page = HttpContext.Current.Handler as Page;
//if (page != null)
//{
//if (HttpContext.Current != null)
//{
// Page page = (Page)HttpContext.Current.Handler;
//TextBox TextBox1 = (TextBox)page.FindControl("Text2");
TextBox p = new TextBox(); // You now have an instance.
//TextBox TextBox1 = (TextBox)page.FindControl("Text2");
// TextBox1.Text = "asdasd";
p.Text = "dfsdfsd";
// page.Controls.Add(p);

p.Text = "Fred";
p.ID = "s";

if (objp.Session["id"].ToString() == ds.Tables[0].Rows[0][6].ToString()) { bolean="true"; } else { bolean="false"; }
if (objp.Session["id"].ToString() == "2") { bolean = "true"; }
//TextBox1.Text = ds.Tables[0].Rows[0][1].ToString();
// }
// Text1.Text = ds.Tables[0].Rows[0][1].ToString();
// }
user = ds.Tables[0].Rows[0][7].ToString();
}
// return textt;
if (textt.Length >50)
// textt = textt.Insert( 50,"</br>");
if (textt.Length>80)
textt = textt.Substring(0, 80);

return new string[] { textt,bolean,user };

// return textt;
}

samira3
یک شنبه 26 آذر 1391, 12:42 عصر
[System.Web.Services.WebMethod(true)] // true if you're using Session
//[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)] // httpget isn't necessary but often a good idea
public static string[] SetDownloadPath(string day )
{
Page objp = new Page();
string bolean="true";
string user = "";
objp.Session["day"] = day;
EventBL calevent = new EventBL();
calevent.Year = Year;
calevent.Month = Month;
calevent.Day = day;
DataSet ds = calevent.Fill();
// Text1.Attributes.Add("text", "kghkghk");
TextBox Text = new TextBox();

// objp.Session["sCountry"] = ds.Tables[0].Rows[0][1].ToString();
string textt="";
if (ds.Tables[0].Rows.Count > 0)
{
textt = ds.Tables[0].Rows[0][1].ToString();
// page = HttpContext.Current.Handler as Page;
//if (page != null)
//{
//if (HttpContext.Current != null)
//{
// Page page = (Page)HttpContext.Current.Handler;
//TextBox TextBox1 = (TextBox)page.FindControl("Text2");
TextBox p = new TextBox(); // You now have an instance.
//TextBox TextBox1 = (TextBox)page.FindControl("Text2");
// TextBox1.Text = "asdasd";
p.Text = "dfsdfsd";
// page.Controls.Add(p);

p.Text = "Fred";
p.ID = "s";

if (objp.Session["id"].ToString() == ds.Tables[0].Rows[0][6].ToString()) { bolean="true"; } else { bolean="false"; }
if (objp.Session["id"].ToString() == "2") { bolean = "true"; }
//TextBox1.Text = ds.Tables[0].Rows[0][1].ToString();
// }
// Text1.Text = ds.Tables[0].Rows[0][1].ToString();
// }
user = ds.Tables[0].Rows[0][7].ToString();
}
// return textt;
if (textt.Length >50)
// textt = textt.Insert( 50,"</br>");
if (textt.Length>80)
textt = textt.Substring(0, 80);

return new string[] { textt,bolean,user };

// return textt;
}

samira3
یک شنبه 26 آذر 1391, 12:43 عصر
ديگه صدا زدن تابع جاوا هم

OnClientClick="javascript:ShowPopup(document.getElementById('Bt7' ).value); "

samira3
یک شنبه 26 آذر 1391, 12:46 عصر
اميدوارم ديگه متوجه شده باشيد راستي قسمت اصلي داشت يادم مي رفت اونجايي كه مقدار از سي در جاوا نمايش مي دادم


function onSucceeded(result, userContext, methodName) {
// $get('div1').innerHTML = result;


if ($get('a').style.overflow != "hidden") $get('a').style.overflow = "hidden";
if ($get('a').style.display != "none") $get('a').style.display = "none";
if ($get('a').style.position != "absolute") $get('a').style.position = "absolute";


$get('Text1').value = result[0];
if (result[0] != "") {
$get('a').innerHTML = result[0];
$get('Label6').innerHTML = result[2];
$get('a').style.overflow = "visible";
$get('a').style.display = "block";


// $get('a').style.position = "absolute";
//// $get('a').style.display = block;
// $get('a').style.top = newPosy + "px";
// $get('a').style.left = newPosx + "px";
if (result[1] == "true") {
document.getElementById("Button24").style.display = "inline";
document.getElementById("Button23").style.display = "inline";
}
else {
document.getElementById("Button24").style.display = "none";
document.getElementById("Button23").style.display = "none";

}

}
else {
$get('a').innerHTML = "";
document.getElementById("Button24").style.display = "none";
// $get('a').style.overflow = "hidden";
// $get('a').style.display = "none";
// $get('a').style.position = "absolute";
// $get('a').style.display = "none";
// $get('a').style.width = "5px";
// $get('a').style.height = "5px";
}


// document.getElementById("Button24").style.display = "inline";
// document.getElementById("Button23").style.display = "inline";

// $get('a2').val = result;
}
function onFailed(error, userContext, methodName) {
// alert("An error occurred")
}