1

Iam having a UserControl for showing Success/Warning/Error message. For all these kind of messages we have one UserControl "Message.ascx" Currently using for other aspx pages without Jquery

As we are trying to maintain standard message alerts. Now as iam using Jquery and JSON calls in my aspx page for the first time., i want to show these success message from my jquery.,

In general aspx codebehind i have used the User control as

//register User Control
<UM1:UM runat="server" ID="Message" /> 
 ....
 public void insert(){
 .. Some logic.., after success
 Message.ShowSuccess(" Inserted Successfully");
 }

But here in Jquery how do i call these ShowSuccess() which is in ascx.cs my ajax call

     $.ajax({
                 type: "POST",
                 contentType: "application/json; charset=utf-8",
                 url: "Voyage.aspx/Context_Update",
                 data: "{'ID':''1}",
                 dataType: "html",
                 success: function (html) {
                     try {                          
                        // Want to Show    Message.ShowSuccess(" Inserted Successfully");                     
                     } catch (ex) {
                         alert("ErrCode:1");
                     }

Iam not getting any idea and have not found any results too.., Please help me out

4
  • Why do you want to use servercode to show the message on the client? Commented Apr 15, 2013 at 8:50
  • For all other pages we haved the same but all other pages are not in Jquery.., we just want to maintain standard Messages.. Commented Apr 15, 2013 at 8:52
  • Is that Not possible to fetch the servercode from ascx?? Commented Apr 15, 2013 at 8:53
  • JQuery is clientside code(javascript) which runs in the browser. Your UserControl is a server control which runs on the server. If you already know the message on the client, why do you need to execute servercode at all? That'll cause unnecessary traffic. Commented Apr 15, 2013 at 9:00

1 Answer 1

2

You can't make a call to a User-Control in ASP .NET as it isn't something that is directly served to the outside world but instead something that the server combines into your page during the page life-cycle.

If you want to call something on the server you need to add the [WebMethod] attribute to the server side method, this allows you to call from jQuery. You could have a [WebMethod] on your page that then calls some code in your User-Control, but not directly access your User-Control.

So, something like this:

MyPage.aspx

[WebMethod]
public static string GetMessageFromWebPage()
{
    return MyUserControl.GetMessageFromUserControl();
}

MyUserControl.ascx

public static string GetMessageFromUserControl()
{
    return "Hello World!"
}

jQuery

$.ajax({
  type: "POST",
  url: "MyPage.aspx/GetMessageFromWebPage",
  data: "{}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(msg) {
    // Do something with the page method's return.
    $("#Result").text(msg.d);
  }
});
Sign up to request clarification or add additional context in comments.

5 Comments

So for this Message i need to have one WebMethod which calls ascx servercode and use this WebMethod ?
Yes, WebMethod on Page and normal method on user control. The Page calls the method on the user controls and passes back to caller.
@Pink - I've expanded to include pretty much full example.
Thanks a Lot..., But here one doubt.., i want to call these GetMessageFromUserControl for the success of one insert so in the Success of that i need to again call this ajax call??
I am not sure what you mean? Once you've made your call to Insert the record or whatever it is you are doing you can then just show an alert('Successful') or similar. Unless you need to make another call to the server then I wouldn't as you want to keep the round-trips to the server to a minimum. If you are calling it lots of times why not bundle your request up into one request with one result? I.e. InsertRecord(1,2,3,4) = Success or Failure?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.