accessing aspx page control id from a class in c# - c#

I have a class which has a method, and a web page (default.aspx) with textboxes buttons etc. They both are in the same namespace.
I need to access to default.aspx' s control ID's (for example a textbox ID) from the class method. Because I need to get the values from default.aspx controls to do some checks with the database, and it needs to be done in the class method.
have any ideas?

Related

How to get a Page from a class in asp website

sorry for my bad English.
I have a Default.aspx page in my project containing a textbox and a button. So when the client clicks on the button, the browser calls a method that is in my web service WebService.asmx.cs whit the textbox.Text as parameter.
(e.g NameSpace.WebService.Say("Hi"); in js)
But I need to report the results ("Hi") to my Default page for showing them in an UpdatePanel and I don't know how to get the page.
I tried (Default)HttpContext.Current.CurrentHandler but it was null.
Is there any other way to get that page?
You can't access the Page object while calling a static(webmethod) method. Static methods cant access instance of the Class it is part of.
Options you have are:
1. Save the value in Session. (HttpContext.Current.Session should be accessible) and show it on the next page load, after accessing it from session. This is a roundabout way and you will have to decorate the webmethod with this attribute:[WebMethod(EnableSession = true)]
2. Instead of using webservice, just update the label in Client side itself using Javascript.

Get dynamically generated Controls of current Page

I have a MasterPage website. I'll use the Facebook example to simplify understanding of my purpose.
In default.aspx, i add many DIVs dynamically from code-behind (ie. each div is a SQL result).
In each DIV there's a Label, and also a "Like" HyperLink, listened by a "onclick" JS function that changes "Like" to "Dislike" text in LinkButton and triggers an AJAX call that calls MyMethod(userid, postid) method.
The MyMethod() method changes the Like/Dislike status for the user in SQL.
I want MyMethod() to update the Label too, so MyMethod() would do:
((Label)((Page)HttpContext.Current.Handler).FindControl("lbl_XX")).Text = "foo";
The problem is that i can't get any of the controls of the page.
I also tried a EventHandler added to the LinkButton, but eventually the MyMethod() never gets the Label by FindControl because Page object seems to come without controls.
How can i access the controls of the current page from a method?

How to call user control's(.ascx) method in .aspx page

I have created user control, in that user control i have one method and I want to call this method in .aspx. I have registered this user control in aspx
For example:
Below is method in user control.
public void SetGridData()
{
}
I want to call above method in .aspx.cs file.
How can we call this method?
Somewhere in the ASPX page's code you should have a reference to the user control object. For example, if the user control is called MyUserControl then somewhere at the class level for the page (possibly in a separate partial class designer file) should be:
protected MyUserControl myUserControl1;
or something similar to that. That's the instance of the user control for the page's class. The page life cycle should instantiate it by the time Page_Load is reached, so from then on you can use that object:
myUserControl1.SetGridData();
If this is purely an example, then you can call methods in codefiles with the following syntax:
<%= SetGridData(); %>
However, just be aware of the notes I put in the comments above.

calling a method on the parent page from a user control

I am using a user control that I created (just a .cs file not an .ascx file), that does some magic and depending on a value generated by the control, I need it to do something on the parent page that is 'hosting' the control. It needs to call a method under certain circumstances (method is on the parent control).
the control is placed on the parent page like so:
<customtag:MyControl ID="something" runat="server" />
I'm dynamically creating buttons etc on the control itself but when a button is clicked, let's say for example that there's a text box on the control and if the value of the textbox is "bob" it needs to call a method on the page that's housing the control...how can I accomplish this?
You could do what casperOne suggested, but I wouldn't advise it. This is tightly coupling your user control to your parent page, which kind of defeats the purpose of a user control.
Personally, I'd add an event to the user control (say, ButtonClicked) that the parent page can handle. In the event handler in your parent, deal with the event however you see fit. This way you can plug the user control into a different page at a later date and not have to worry about logic in the user control that requires a specific kind of parent page.
You should be able to get the Page hosting the control through the Parent property. However, that's going to be returned to you as a Control. You have to cast it to the type of your page in order to access any methods on the page which you have defined.
I think that casperOne is on the right track, but you need to go a step further. I'm giong to make the assumption that this user control will be used on more then on page. (I normally write VB.Net, sorry if my C# is malformed)
Make a base page class (you can store it in your App_Code directory or in a project):
public class PageToInheritFrom : System.Web.UI.Page {
public void SpecialFunction() {
}
}
Now make sure that all of your pages inherit from this page in your code behind file:
public partial class _Default : PageToInheritFrom {
}
Now in your user control you know what the page type is and can call
((PageToInheritFrom)this.Page).SpecialFunction();

asp.net - variable pass between ascx and page

Have any ways to keep a variables (object) in a page scope? I need pass some object from master page to all ascx files. Normally, i passing by using a method or structure with parameters. But now im asking another way to do this. Store it in session is a bad idea because my object should exists in a page scope only.
One way is to set up properties in your user controls. You have access to read and set these from all pages that implement them.
Another alternative is to store the shared object(s) in the HttpContext.Items collection.
You could expose your variables a public properties of the master page:
public string MyVariable { get; set; }
then access them from the user controls by referencing the master page, and casting to its specific type:
((MyMasterPageType)Page.Master).MyVariable
So you have masterpage, user controls and the page itself.
The page is the container for the masterpage and the user controls, hence it's the only one that 'knows' the 2 parties. So I'll suggest (if you haven't) you have the page instance to facilitate the object/variable passing amongst them.
If your variable's value is going to be changed on per page basis then i would recommend you to write that code in base page (or user control) and inherit all the page (or usercontrol),if the values are going to be similar for all pages(or user control) you can use cache object as well.
In a more better approach if you feel you can even create one helper class and call it from your base page (or user control), so you can separate variable assignment code from your page.

Categories

Resources