i am having trouble catching errors in an AJAX panel. Even when i throw an exception in the c# code behind the front end completely ignores it.
Here is the code i have setup, can anyone see why?
I ideally want to show a js alert window on error.
Code Behind:
protected void btnX_Click(object sender, EventArgs e)
{
throw new ApplicationException("test");
}
protected void ScriptManager_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
{
ScriptManager.AsyncPostBackErrorMessage = e.Exception.Message;
}
Markup:
<script type="text/javascript" language="javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function EndRequestHandler(sender, e)
{
window.alert(e.get_error().name);
}
</script>
<asp:ScriptManager ID="ScriptManager" runat="server" AllowCustomErrorsRedirect="true" OnAsyncPostBackError="ScriptManager_AsyncPostBackError" />
I think you should be displaying the 'message' error property and not 'name'
see http://msdn.microsoft.com/en-us/library/bb383810.aspx
function EndRequestHandler(sender, e)
{
if (e.get_error() != undefined)
{
var errorMessage = e.get_error().message;
e.set_errorHandled(true);
alert(errorMessage)
}
}
Related
I have successfully set the text of asp:textbox using jQuery val() function, now I want the same value of the textbox on click of asp:button on the server side code.
$("#textboxId").val('some text');
protected void button_Click(object sender, EventArgs e)
{
// getTheText is blank
string getTheText = textboxId.Text.Trim();
}
<script type="text/javascript">
$(document).ready(function () {
$('#<%= TextBox1.ClientID %>').val("my value");
});
</script>
and in code behind on button click use
protected void Button1_Click(object sender, EventArgs e)
{
var value = TextBox1.Text;
}
this will work. it work for me i test it.
I also had the same problem and finally found a solution.
string getheText =Page.Request.Form["textboxId"].ToString().Trim();
But be careful if you use "Content" in master page the id must be like that
string gettheText = Page.Request.Form["ctl00$ContentPlaceHolder1$textboxId"].ToString().Trim();
if your textbox is an aspx server control then you can directly set Text by using
textboxId.Text = "Some Value";
i have tried to close window(radWindow) using asp.net C#, but error on java script function is that 'object required' ,How can i solve this?
My java script and asp.net code is
<script type="text/JavaScript">
function Close() {
GetRadWindow().Close();
}
</script>
Asp.net code is
<asp:Button ID="btnSubmit" runat="server" Text="Submit Request" Height="27px" OnClick= "btnSubmit_Click" OnClientClick="Close();return false;" />
Optional try using C# for same operation as
C# code for button is
protected void btnSubmit_Click(object sender, EventArgs e)
{
//Server code
ScriptManager.RegisterStartupScript(this,GetType(), "close", "Close();", true);
}
You call GetWindow() but you don't have this function .
Just add this code ,
function GetWindow()
{
var oWindow = null;
if (window.radWindow) oWindow = window.radWindow;
else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
return oWindow;
}
This post may help you lot
http://www.telerik.com/community/forums/aspnet-ajax/window/getradwindow-close-cause-unspecified-error.aspx
i had same issues in the past but manged by the logic from the above post
using asp.net | C#
I want my ImageButton to open a URL when I click it. I finally have my Image loading and it clicks but when I click it nothing happens. Here is the code I have so far:
aspx page
<asp:ImageButton ID="Button1" runat="server" ImageUrl="~/images/button.gif"
onclick="Open_Click"></asp:ImageButton>
aspx.cs page
protected void Open_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
try
{
System.Diagnostics.Process.Start("http://www.website.com");
}
catch { }
}
You want to do a redirect, not start a process. Try this:
protected void Open_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
try
{
Response.Redirect("http://www.website.com");
}
catch { }
}
Additionally, you could just set the PostBackUrl attribute on the control and have no need for a server side event.
You can do it on the client-side:
This will open in another window:
<asp:ImageButton OnClientClick="window.open('/xxx/xxx.aspx');
OR this will open in same window, javascript needs to return false so server code won't run:
<script>
function ReDirect() {
location.href = '/xxx/xxx.aspx';
return false;
}
</script>
asp:ImageButton OnClientClick="javascript:return(ReDirect());" />
I've got this button
<asp:Button runat="server" ID="btnReviewDocs" CssClass="btnReviewDocs" data-theme="b"
Text="Review Documents" OnClick="btnReviewDocs_Click" OnClientClick="clickHyperlink();"/>
And in 'OnClick' event I'm assembling an URL that I need to set to asp:Hyperlink and at the end of the 'OnClick' I'm setting this URL to the 'NavigateURL' propery of the 'asp:Hyperlink'. Once the 'asp:Hyperlink' has the correct URL I need to call the 'clickHyperlink()' function.
function clickHyperlink() {
var href = $('#hlnkID').attr('href');
if (typeof href !== "undefined") {
$.mobile.showPageLoadingMsg();
window.location.href = href;
}
}
But the 'OnClientClick' event is executed always before the 'OnClick'. Any suggestions for a workaround?
I'm doing all this stuff, because I've got problems with JQuery Mobile and 'Response.Redirect(url);' is changing the page, but not the URL.
I believe that you don't really need to involve the Hyperlink control in the JS part.
Modify your JS function and remove the OnClientClick attribute from the btnReviewDocs button:
<script type="text/javascript">
function clickHyperlink(href) {
$.mobile.showPageLoadingMsg();
window.location.href = href;
}
</script>
On the server, in the btnReviewDocs_Click method:
protected void btnReviewDocs_Click(object sender, EventArgs e)
{
// TODO: set the url, maybe append some params to the
// hlnkID.NavigateUrl value
var url = "http://stackoverflow.com/";
ClientScript.RegisterStartupScript(Page.GetType(),
"clickHyperlink",
"clickHyperlink('" + url + "');",
true);
}
Use the RegisterStartupScript in the ClientScript object to run the code after postback--->
protected void btn_Click(object sender, EventArgs e)
{
//some code
this.ClientScript.RegisterStartupScript(this.GetType(), "clintClick", "clickHyperlink", true);
}
try this
protected void btnReviewDocs_Click(object sender, EventArgs e)
{
//something doing here
Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "<script type='text/javascript'>clickHyperlink()</script>");//call javascript function
}
The answer is mentioned by #Alex Filipovici.
But first you should ask yourself do you really need to go back to the client side to do a redirect ?
Why not call :
Response.Redirect("MyURL");
How to get input from user using a confirm box in asp.net and store the input in a C# variable.
as whether it is a ok or cancel button. pls help me forward.
For Example i am using like this,
Response.Write("confirm('Testing FiileAlready existing Replace it?')");
instead i need the output of the confirm box in c# variable...
Try this:
Your javascript function:
<script type="text/javascript" language="javascript">
function ConfirmOnReplace() {
if (confirm("Are you sure?") == true)
return true;
else
return false;
}
</script>
The ASP.NET button:
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return ConfirmOnReplace();" />
And the code-behind(YourPage.aspx.cs) file:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Button1.Click += new EventHandler(Button1_Click);
}
void Button1_Click(object sender, EventArgs e)
{
// If you are here, your javascript method returned true
// TODO: Replace the file, or do what you want
}
}