I am using the RadAlert called from code behind to to mimic a modal messagebox and it is working quite well.
public void ShowRadAlert(string p_msg, string p_title) |
{ |
//RadAlert(msg, height, width, title) |
ScriptManager.RegisterStartupScript(Page, typeof(Page), |
"ShowAlert", |
string.Format(@" |
function _showAlert() {{Sys.Application.remove_load(_showAlert); |
radalert('{0}', null, 100, '{1}');}}; |
Sys.Application.add_load(_showAlert);", p_msg, p_title), true); |
} |
The question I have is this: Is there anyway to set focus to a control on the calling page after the OK button is clicked on the RadAlert. For example, I have validation on the main page to check to see if the value in a textbox (tbEmployeeCount) is entered and if it isn't display the RadAlert and then set tbEmployeeCount.focus() so the user can enter a value.
Thanks -
6 Answers, 1 is accepted
What you can do is to hook to the close eventhandler of the dialog and when radalert is closed - to call another function that will set the focus to the desired element on the client.
e.g:
var oAlert = radalert("my alert message");
oAlert.add_close(focusFn);
where focusFn is the name of the second JavaScript function.
Sincerely yours,
Georgi Tunev
the Telerik team
Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Hi Georgi -
Thank-you for the quick response. Your suggestion works quite well if I create the JavaScript function in the markup of the aspx page and then associate that function to the add_close event handler of the RadAlert, but what I need to do is create all of this JavaScript programmatically from the code behind. When I try to add to add the add_close call programmatically (as shown below - tbEmployees would be the control's client id that I want to set focus to after the rad alert window is closed) I get an error "Sys.ArgumentUndefinedException: Value cannot be undefined. Parameter name: handler". Do you have any sample code of achieving this js functionality from codebehind? Thanks so much.
public void ShowRadAlert(string p_msg, string p_title) |
{ |
ScriptManager.RegisterStartupScript(Page, typeof(Page), |
"ShowAlert", |
string.Format(@"function _showAlert() {{Sys.Application.remove_load(_showAlert); |
var oAlert = RadAlert('{0}', null, 100, '{1}'); |
oAlert.add_close(document.getElementById(""tbEmployees"").focus());}}; |
Sys.Application.add_load(_showAlert);", p_msg, p_title), true); |
} |
e.g.
add_close(function(){your code here})
Thanks for your response. Unfortunately, I could not get the add_close(function(){your code here}) working from the code behind. The add_close didn't seem to like anything besides a function name so I was unable to include any parenthesis. I did however get it to work from the code behind by creating a new JS function and just calling that in the add_close. Here is my code if anyone else runs into this issue. Thanks to everyone for their help.
/// <summary> |
/// Displays a RadAlert modal window to simulate a messagebox. |
/// You must have a RadWindowManager on your page in order to use this functionality. |
/// </summary> |
/// <param name="p_msg">Message to appear in RadAlert</param> |
/// <param name="p_title">Title to be displayed in RadAlert</param> |
/// <param name="p_controlClientId">If you want a specific control to get focus after |
/// the RadAlert is closed then pass the control's Client id else pass null</param> |
public void ShowRadAlert(string p_msg, string p_title, string p_controlClientId) |
{ |
string radOnCloseScript = string.Empty; |
//create JS that will set focus to the desired control after RadAlert window is |
//closed (If applicable) |
if (p_controlClientId != null) |
{ |
if (p_controlClientId.Length > 0) |
{ |
string setFocusString = @"function SetFocusToControl() |
{document.getElementById(""" + p_controlClientId + @""").focus();}"; |
ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "SetFocusToControl", setFocusString, true); |
//the RadWindow's eventhandler add_close will be used to facilitate setting focus to the desired control |
radOnCloseScript = "oAlert.add_close(SetFocusToControl);"; |
} |
} |
//create JS that will display the RadWindow |
ScriptManager.RegisterStartupScript(Page, typeof(Page), |
"ShowAlert", |
string.Format(@" |
function _showAlert() {{Sys.Application.remove_load(_showAlert); |
var oAlert = radalert('{0}', null, 100, '{1}');" + radOnCloseScript + @"}}; |
Sys.Application.add_load(_showAlert);", p_msg, p_title), true); |
} |
This is lavanya. I used the following code to set the focus. But if i press enter then again another alert is opening. Can you provide solution for default focus for button and textbox?
Page.Form.DefaultButton = this.btnLogin.UniqueID;
Thanks and Regards,
Lavanya
Since you have set a default button and also the radalert button is on the same page, the behavior you get is the expected on and it is not directly related to RadControls and radWindow. You can search the net to find different approaches on how to change the default button with javascript, e.g the solution suggested below:
http://www.daniweb.com/forums/thread98980.html
In your case this solution will consist of disabling the button when the radalert is shown and adding a handler for the Enter key. You can use the add_close syntax to revert the default behavior when the radalert is closed.
However, if you do not really need to have this button as default but just to focus it when needed, you can use the javascript focus() method - this will solve the issue.
Sincerely yours,
Svetlina
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.