在我的应用程序中,我有两个ImageButtons。在他们的OnClientClick事件中,我调用js函数以上的js。作为参数,我给出了像' en'或者' de'
//Blocks the submit of the form and changes the language
function changeLanguage(languageIdentifier)
{
var pageUrl = '<%= ResolveUrl("default.aspx/changeLanguage") %>';
var parameter = '{ "languageIdentifier": languageIdentifier }';
//Call the C# Methods
$.ajax({ type: "POST", url: pageUrl, data: parameter, contentType: "application/json; charset=utf-8", dataType: "json", failure: function (response) { alert(response.data) } }).done(function () { alert("Language was changed.") }).fail(function (jqXHR, errorText, errorThrown) { alert("An error has occurred: " + errorThrown); });
return false;
}
[System.Web.Services.WebMethod]
public static bool changeLanguage(string languageIdentifier)
{
try
{
//Get the whole formular
Page thisPage = (Page)HttpContext.Current.Handler;
//Get the informations from the formular
Brain.UserRequest = _default.getUserGivenInformation(thisPage);
//Change session values
switch (languageIdentifier)
{
case "DE":
case "de-DE":
case "de": HttpContext.Current.Session["uiculture"] = "de";
HttpContext.Current.Session["culture"] = "de-DE";
break;
case "EN":
case "en-GB":
case "en":
default: HttpContext.Current.Session["uiculture"] = "en";
HttpContext.Current.Session["culture"] = "en-GB";
break;
}
//Reloads the page
HttpContext.Current.Server.Transfer(HttpContext.Current.Request.Url.PathAndQuery, false);
return true;
}
catch
{
return false;
}
}
单击ImageButton后,不会调用C#方法。只有错误消息如&#34;发生了错误:内部服务器错误&#34;出现。有谁知道为什么会有这个问题?
现在我已经替换了HttpContext.Current.Server.Transfer
代码并插入了两个响应写入方法。
[System.Web.Services.WebMethod]
public static string changeLanguage(string languageIdentifier)
{
HttpContext.Current.Response.Write("Test #default.changeLanguage");
try
{
//Get the whole formular
Page thisPage = (Page)HttpContext.Current.Handler;
//Get the informations from the formular
Brain.UserRequest = _default.getUserGivenInformation(thisPage);
//Change session values
switch (languageIdentifier)
{
case "DE":
case "de-DE":
case "de": HttpContext.Current.Session["uiculture"] = "de";
HttpContext.Current.Session["culture"] = "de-DE";
break;
case "EN":
case "en-GB":
case "en":
default: HttpContext.Current.Session["uiculture"] = "en";
HttpContext.Current.Session["culture"] = "en-GB";
break;
}
//Create url for redirecting
string url = HttpContext.Current.Request.Url.AbsoluteUri;
return url;
}
catch (Exception e)
{
HttpContext.Current.Response.Write(e.Message + " #default.changeLanguage");
return "";
}
}
但是没有出现C#Code的失败者。我从chrome
中找到了此错误消息对我而言,找不到C#功能。
答案 0 :(得分:0)
JavaScriptCode
function changeLanguage(languageIdentifier) {
var pageUrl = '<%= ResolveUrl("/demo.aspx/changeLanguage") %>';
var params={};
params.languageIdentifier=languageIdentifier;
var jsonString = JSON.stringify(params);
//Call the C# Methods
$.ajax({
type: "POST",
url: pageUrl,
data: jsonString,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {
alert("Language was changed.");
}
})
return false;
}
.aspx代码
<div>
<asp:ImageButton ID="imgbutn" runat="server" Height="50" Width="50" OnClientClick="return changeLanguage('en')" />
</div>
页面背后的代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) // to check page is post back or not
{
// do stuff here
}
if (Request.Headers["X-Requested-With"] == "XMLHttpRequest") // this is to check whether it's an AJAX call or not
{
// do stuff here..
}
}
[System.Web.Services.WebMethod]
public static bool changeLanguage(string languageIdentifier)
{
// do logic here....
return true;
}
我刚检查了它的工作正常。 希望它可以帮到你。