Pagemethods in asp.net - asp.net

My Pagemethod implementation is not working in Chrome browser.
I have ASP.NET 3.5 web application developed in VS 2008.
The code below not working in chrome or Safari:
function FetchDataOnTabChange(ucName)
{
PageMethods.FetchData(ucName, OnSuccessFetchDataOnTabChange, OnErrorFetchDataOnTabChange);
}
function OnErrorFetchDataOnTabChange(error)
{
//Do something
}
function OnSuccessFetchDataOnTabChange(result)
{
//Do something
}

This should work in all browsers by following the steps below:
The page method must have the
System.Web.Services.WebMethod
attribute. [WebMethod]
The page method must be public.
[WebMethod] public ...
The page method must be static.
[WebMethod] public static ...
The page method must be defined on
the page (either inline or in the
code-behind). It cannot be defined
in a control, master page, or base
page.
The ASP.NET AJAX Script Manager must
have EnablePageMethods set to true.
This is from a working application
aspx page:
/* the script manager could also be in a master page with no issues */
<asp:ScriptManager ID="smMain" runat="server" EnablePageMethods="true" />
<script type="text/javascript">
function GetDetails(Id) {
PageMethods.GetDetails(doorId);
}
</script>
code behind:
[System.Web.Services.WebMethod]
public static void GetDetails(string Id)
{
}

Related

Intellisense for Templated User Control Tags

I've written a templated user control, MinimalTemplate, which currently does nothing other than render the HTML passed into its "ContentTemplate" placeholder. I want Visual Studio 2008 to have the same intellisense features for MinimalTemplate that it has for built-in templated controls such as Repeater.
Possibly related: I can manually type out my ContentTemplate tags, and it will build and run properly, but I get a validation error. I have already deleted the contents of my ReflectedSchemas folder, as suggested in this question.
Complete source for Minimal Template:
MinimalTemplate.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="MinimalTemplate.ascx.cs" Inherits="MyProject.MinimalTemplate" %>
<asp:placeholder runat=server id="contentPlaceHolder" />
MinimalTemplate.ascx.cs
using System.Web.UI;
namespace MyProject
{
[ParseChildren(false)]
public partial class MinimalTemplate : System.Web.UI.UserControl
{
[TemplateContainer(typeof(MessageContainer))]
[TemplateInstance(TemplateInstance.Single)]
public ITemplate ContentTemplate
{ get; set; }
void Page_Init()
{
if (ContentTemplate != null)
{
MessageContainer container = new MessageContainer();
ContentTemplate.InstantiateIn(container);
contentPlaceHolder.Controls.Add(container);
}
}
public class MessageContainer : Control, INamingContainer { }
}
}
What changes can I make to my MinimalTemplate code so that Visual Studio will validate and autocomplete its ContentTemplate tag?
Related.
Add [PersistenceMode(PersistenceMode.InnerProperty)] to ContentTemplate's attribute list. After adding it and rebuilding, the validation error disappeared and "ContentTemplate" appeared as expected in the Intellisense dropdown.
During my investigation, I'm certain I tried adding this property two or three times to no effect, so I expect the VS validator is a bit flaky. It smacks of voodoo programming, but do a Clean/Rebuild All and wait a few seconds before seeing whether the validation error persists.
(Also, you don't need the ParseChildren attribute for this control.)

Calling an ASPX method from an ASCX control

I have a method HasAdminRole() on home.aspx that I call from an ASCX control. And the ASCX works using (Home)this.Page).HasAdminRole() with the user control on home.aspx
Now, when I add that same ASCX to a different ASPX page Adminuser.aspx, I get an error when the ASCX tries to call (Home)this.Page).HasAdminRole() from a different ASPX page than when on home.aspx
I get the error Unable to cast object of type 'ASP.adminuser_aspx' to type 'CALE.Home'.
How do I resolve this when using the same ASCX on multiple ASPX's and the user control tries to call the single method on Home.aspx
Suggest moving your admin/roles logic out from your .aspx and into a class that's accessible from both your .aspx and .ascx. It's a bit of 'separation of concerns' and a bit of tidiness in having the logic in common areas. Basically -- only keep page related logic in your page code-behind.
public static class Roles
{
public static HasAdminRole(User someUser)
{
// implementation
}
}
Call it from any page's code-behind or other helper class:
User foo;
bool isAdmin = Roles.HasAdminRole(foo);
So I just figured out a solution to this problem.
You can create a client side trigger in the aspx page and call it with javascript from your ascx page.
e.g.
aspx page:
<script>
function doSomething()
{
//set hidden field values or do whatever else you might need to do here
$('#<%= btnDoSomething.ClientID%>').click();
}
</script>
<asp:Button runat="server" ID="btnDoSomething" OnClick="btnDoSomething_Click" CssClass="hidden" />
aspx code behind:
protected void btnDoSomething_Click(object sender, EventArgs e)
{
//do whatever you need to on the aspx page
}
Now you can call that javascript function from any ascx page that you like.
e.g.
ascx page:
<button type="button" onclick="doSomething()">do something on your aspx page<button/>

How to redirect to silverlight page from asp.net

Net application and from one page i have to redirect to silverlight page, i am new to silverlight so i have two question
Can i add silverlight page in asp.net app or i will have to add silverlight application in asp.net solution.
How to redirect from asp.net page to silver.(i want to pass some data from asp.net to silver light)
To communicate between a Silverlight control and javascript in an ASP.net page you can use the HTML bridge. Essentially this allows you call specially marked methods in your Silverlight control as well call javascript routines from Silverlight.
Marking a Silverlight method or attribute as accessible to javascript:
public partial class Page : UserControl
{
void Page_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
// Register this object in the page as Scriptable
// so it can be accessed from within JavaScript
HtmlPage.RegisterScriptableObject("Page", page);
}
[ScriptableMember]
public string HelloFromSilverlight()
{
return "Hello";
}
[ScriptableMemberAttribute]
public int SomeValue
{
get
{ return _someValue;}
}
}
Calling the Silverlight method from javascript:
// Get a reference to the actual Silverlight
// plugin element within the page
var plugin = pluginObject.get_element();
// Call the HelloFromSilverlight method of our Silverlight object
var text = plugin.Content.Page.HelloFromSilverlight();
Also see other examples:
http://www.dotnetspider.com/resources/36450-How-work-with-HTML-DOM-SIlverlight.aspx
http://geekswithblogs.net/PeterTweed/archive/2009/08/08/html-bridge---silverlight-javascript-interop.aspx

Missing Intellisense While Describing Custom Control Properties Declaratively

So, I've been working on this project for a few days now, and have been unable to resolve the issue of getting intellisense support for my custom-defined inner properties for a user control (ascx, mind you).
I have seen the solution to this (using server controls, .cs mind you) many times. Spelled out in this article very well. Everything works for me while using ascx controls except intellisense.
Here's the outline of my code:
[PersistChildren(true)]
[ParseChildren(typeof(BreadCrumbItem))]
[ControlBuilder(typeof(BreadCrumbItem))]
public partial class styledcontrols_buttons_BreadCrumb : System.Web.UI.UserControl
{
...
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public List<BreadCrumbItem> BreadCrumbItems
{
get { return _breadCrumbItems; }
set { _breadCrumbItems = value; }
}
...
protected override void AddParsedSubObject(object obj)
{
base.AddParsedSubObject(obj);
if (obj is BreadCrumbItem)
BreadCrumbItems.Add(obj as BreadCrumbItem);
}
...
public class BreadCrumbItem : ControlBuilder
{
public string Text { get; set; }
public string NavigateURL { get; set; }
public override Type GetChildControlType(string tagName, System.Collections.IDictionary attribs)
{
if (String.Compare(tagName, "BreadCrumbItem", true) == 0)
{
return typeof(BreadCrumbItem);
}
return null;
}
}
}
Here's my mark up (which works fine, just no intellisense on the child object declarations):
<%# Register src="../styledcontrols/buttons/BreadCrumb.ascx" tagname="BreadCrumb" tagprefix="uc1" %>
...
<uc1:BreadCrumb ID="BreadCrumb1" runat="server" BreadCrumbTitleText="Current Page">
<BreadCrumbItem Text="Home Page" NavigateURL="~/test/breadcrumbtest.aspx?iwentsomewhere=1" />
<BreadCrumbItem Text="Secondary Page" NavigateURL="~/test/breadcrumbtest.aspx?iwentsomewhere=1" />
</uc1:BreadCrumb>
I think the issue lies with how the intellisense engine traverses supporting classes. All the working examples I see of this are not ascx, but Web Server Controls (cs, in a compiled assembly).
If anyone could shed some light on how to accomplish this with ascx controls, I'd appreciate it.
I have solved this before by removing all the user controls and directives from the page and then switching the markup to design view. You then drag the ascx file from the solution explorer to the design window. If you swich back to markup view suddenly intelisense picks up all the properties. I am sure there is a better way but i have never found one that works.

render .ascx page into .aspx page using javascript, jquery

i have aspx page which has following js function which is called on button click
<input type="button" onclick="calltemp1()" value="Temp1"/>
<script type="text/javascript">
function calltemp1() {
$("#Renderthisdiv").load("/Views/Templates/_Temp1.ascx");
}
</script>
my _Temp1.ascx page renders another page Temp1.ascx
my _Temp1.ascx contains
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<div>
<%Html.RenderPartial("/Views/Templates/Temp1.ascx"); %>
</div>
when i run the program i get the JavaScript runtime error saying "object expected"
please help me to solve this issue
Your JavaScript call is going to make another trip through the MVC Pipeline. So it will hit routing, a controller, and then a view. Your JavaScript should not try to hit the ascx file directly, but a route that maps to a controller that renders the view.
Your JS should look like this (note this is using a root relative URL, you may have to adjust):
$("#Renderthisdiv").load("/template/temp1");
Alternately, you can use an HTML helper to get the URL, but the JS will have to be in your view:
$("#Renderthisdiv").load("<%= Html.Action("temp1", "template") %>");
That URL will hit the Temp1 action on the TemplateController
public class TemplateController : Controller {
public ViewResult Temp1() {
return View("Temp1");
}
}
Just add the appropriate action to your controller so you can use jQuery to render it:
public class TemplateController : Controller {
public ViewResult Temp1() {
return View("_Temp1")
}
}

Resources