I'm building a custom asp.net server control and I want to fire an OnSave method and pass on a parameter to the page implementing the control (best analogy would be the RenderContents method).
The idea is to generate an XML string containing the saved fields and pass it to the developer implementing the control through this OnSave method (in the case of RenderContents method it would be HtmlTextWriter output) so they can access the string in the OnSave method and process it (say save it to the database).
So my question is two-fold:
how do I force the page which implements my control to override my OnSave method?
how can I pass my XML string to some variable in that method so developer can access it via page's codebehind?
Related
I have a method in an aspx web page which is accessed from another non webpage .cs class. This method contains a ClientScript to create a popup.
I get this error below when trying to make it a static.
An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.ClientScript.get'
So I am wondering if there is a way to access this method from another .cs class or a solution.
Basically when an error trips on the .cs class I want to display a popup that says the error.
public static void message(string popupinformation)
{
ClientScript.RegisterStartupScript(this.GetType(), "", "popupinformation", true);
}
In .cs
AddUI.message("alert('Error can not add information');");
tl;dr - You need to add "System.web" reference in that library.
I understand that, you have a separated cs file in some another library that invokes a function sitting in asp.net webform. If that is true, then how you are calling that function because it will be inside a page class which inherits from system.web.ui.page.
Lets assume you have make it work by placing that in App_Code. Then used that class say Web_work.cs in some other library say lib.cs.
To use ClientScript... in the library with lib.cs you need to add "System.web" reference in that library.
Best practice is to do all validation in library, but use such kind of display method in webform only. Take an example, lets say you want to use windows form now, which uses MessageBox.Show, in that case your library needs change with current approach.
I have a method in a class like below:
public class ActionHelper
{
[DirectMethod]
public string DeleteComment()
{
...
return "Delete";
}
}
and I want to call it from grid command like this:
<Command Handler="Ext.net.DirectMethod.request({url: '/Classes/ActionHelper/DeleteComment', cleanRequest: true}});" />
but it's not working! how can i do that??? I use ext.net 2.2 and .netframework 4.5
look at this example
http://examples.ext.net/#/Events/DirectMethods/ID_Mode/
it can be helpful
Put a [DirectMethod] in your code behind wich calls that class and use
App.direct.<Method>(); instead
You can't, you either define the method inside the Page, User Control or any Custom Control, or you define a static method inside the Page class.
Here is a quote defining direct methods from a post in the Ext.net forums:
DirectMethod (none static, must be public): server side handler is
raised when you call special javascript method (basically, proxy
method is generated by Ext.Net toolkit). None static direct method can
be defined inside Page, User Control or any Custom Control. Please
note, if direct method is defined inside user control (master page
placeholders are user controls also) or custom control then ClientID
of that control will be added to proxy method 1
Ext.net.DirectMethods.ClientIDOfTheControl.DirectMethodName(); You can
use DirectMethodProxyID attribute for the class to define own alias or
completely remove ClientID prefix Really, none static direct method is
direct event. Single difference, that direct method has no relation
with any widget (and its events) and can be raised by developer from
javascript (as javascript method)
Static DirectMethod (must be public): similar ASP.NET PageMethods, can be defined inside Page class only. With static page method the
Page life cyle is not execued therefore access to ASP.NET control is
not possible but response time much better (depends from your method
logic only)
Users will get to my site using a specific parameter, e.g. :
http://www.mysite.com/whatever/?keepTrackOfThisValue=foo
or
http://www.mysite.com/who/cares/?keepTrackOfThisValue=bar
I would like to store the value of this peculiar parameter in Session everytime I found it in the QueryString. I'm currently using the Session_Start event in Global.asax in order to store this but I would like to override the value each time the parameter value change, which is not possible my way.
Where would you do this ?
Try moving it from Session_Start to Application_BeginRequest
There are [at least] two ways you could go about this...
1) Use an HttpModule to hook in to the application's BeginRequest method, as outlined in this page.
2) Create a base class (inheriting from System.Web.Page) from which all the pages in your application inherit. Then use the Page_Load method in this base class to push the same functionality into all your pages. This also allows you to add any other common functionality you may need across all your pages on Load or in any other event. You can also use this to define common properties to all your pages.
I've never tried #1, but I use #2 in EVERY web application I write. For example, in my PageBase class, I provide a read-only base property which exposes a database-connection-getting mechanism, so each page has easy access to the db without needing to write it's own code for getting a connection from the pool. It can result in MUCH cleaner code overall.
It would depend on how I was using it, but one thing I've done similarly is a small wrapper property on a static class, which, when the getter was called, would optionally set the Session if the querystring was different, and then return the session
Do it during the page_load on every page where this querystring could occur
if(Session["keyName"] != null)
{
Session["permitError"] = querysting;
}
else
{
Session.Add("keyName", value);
}
string StrQuery;
StrQuery=Request.QueryString["value"].ToString();
Session["SessionValue"]=StrQuery;
Why don't you just store it on the Page_Load event if it exists? If it doesn't refer to the Session value.
You could even create a base page that all your pages inheirit from that checks for the existance of the QueryString you are interested in.
A MasterPage with a a public property to access this value would also be useful such as:
// in your master page
public string YourSessionVariable
{
get
{
string yourValue = "SomeDefault"; // if session expired and param not passed
if (Request.QueryString["yourQueryParamName"] != null)
{
Session["yourSessionParamName"] = Request.QueryString["yourQueryParamName"];
}
return (Session["yourSessionParamName"] == null) ?
yourValue : Session["yourSessionParamName"].ToString();
}
}
// Then in your pages your can just fetch it anytime with
Master.YourSessionVariable
Recently I faced few interview questions.The interviewer asked the to give the detailed answer.
1)Can we override a WCF service (Its is not OOPS overriding) ?.Explain the reason on either end. (WCF Related).
2)Can we override Page events (Page_Load())?.Explain reason.(ASP.NET related).
3)What is the primary responsibility of Pre_Init( page) event ,apart from user preference
setting,skinning?
4) Can we override Static methods.Explain the reason.(C# related)
can anyone help me to understand the reasons?
You can't really override WCF service operations. If your Service Contract class has two Service Operation methods with the same name but different parameters (i.e. legitimate C# overloads), WCF will throw an InvalidOperationException when the service is started. If you really want to do this, you can change the exposed operation name of one of the methods in the OperationContract attribute:
[OperationContract(Name = "GetDataWithString")]
public string GetData(string input)
{
...
}
[OperationContract(Name = "GetDataWithNumber")]
public string GetData(int input)
{
...
}
You can override Page events in ASP.Net; this is pretty widely used and usually pretty crucial. You can either explicitly override the methods from the Page class your custom page inherits from, or you can name your methods in such a way that ASP.Net knows that they're to be treated as overrides. For example, declaring a method in a page's code-behind with the signature below will automatically override the Page_Init method.
void Page_Init(object sender, EventArgs e)
The Page_Init method is where ASP.Net starts tracking ViewState. This means that anything done to any of the page's controls is now marked as Dirty in the ViewState StateBag, and so will be base-64 encoded and sent down to the client in the ViewState hidden input field, and therefore sent back to the server on a postback. Changing controls' values before ViewState is being tracked will help stop ViewState getting too large. See this seminal article for more details.
Only class instance methods can be marked as virtual, as the compiler-created v-table is attached to class instances. Static class members are not attached to instances but to the class itself, therefore there's no way to override them. This article explains this in more detail, and gives some workarounds.
is there a way to reference the page control in a webservice? something like this:
[WebService(Namespace = "http://test.org/")]
public class Search : System.Web.Services.WebService
{
public Search()
{
Page.Controls.Add(new Control()); // can I get a reference to Page?
}
}
This seems like a very odd design approach. In general a called method should have no knowledge about, or dependencies on the caller. In this case the web method would need knowledge about the page calling it. I don't think this is possible, and even if it was, consider the possibility that it may not even be a page calling the web service. It could be any kind of application.
What you are trying to do (at least the way you are trying to do it) is impossible. The web service cannot, at server side, modify the page by modifying the control tree, server side. The page object that was rendered to the user does not exists any more.
I think that what you should be doing instead is using an update panel. That will allow you to do exactly what you want to.