I have been trying to call a method from my .ascx.cs to my .aspx.cs codebehind. The routine will allow a check box on the .aspx page to hide/show a Textbox and Label on my user control page. Here is what I have so far. I have the visibility of Label and the Textbox set to false in properties.
control Page code behind:
public partial class Controls_udc : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
private bool MakeVisible(object sender, EventArgs e)
{
return (labelComments.Visible == true) && (textComments.Visible == true);
}
code behind for .aspx page:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
Controls_udc.MakeVisible();
}
}
The compiler error I receive is Compiler Error Message: CS0117: 'Controls_udc' does not contain a definition for 'MakeVisible'.
I believe I am on the right track but just need a couple pointers.
MakeVisible method must be public but in your code it is private. It should be changed.
Related
I want to run some code every time an instance of System.Web.UI.WebControls.Button is clicked.
Is there a way I can drop this functionality into the default OnClick method for the button class, or will I have to make a new class that inherits from System.Web.UI.WebControls.Button?
For example everytime I click a button, I want the code
Debug.WriteLine ("hello world")
// other code here
to run,
but do not want to declare all that code in the OnClick method of every button on my page
protected void Button1_Click(object sender, EventArgs e)
{
//when I get here, I want the code above to have run without having to paste it all into this method
}
protected void Button2_Click(object sender, EventArgs e)
{
//Another button, don't want to be duplicating all that code again
}
Thanks to Mike Cheel who confirmed that I do indeed need to make a new class to achieve this
public class MyButton : System.Web.UI.WebControls.Button
{
protected override void OnClick(EventArgs e)
{
Debug.WriteLine("hello");
}
}
which is then instantiated in the designer file
protected global::WebApplication7.MyButton Button1;
and in the cs file for the page I can add my button specific behaviour
protected void Button1_Click(object sender, EventArgs e)
{
Debug.WriteLine("Goodbye");
}
I'm currently having two master pages M1 and M2 and several content pages.
M2 has a calendar control and I want to call a content page function each time the selection on the calendar changes.
Here's the code I have in my master page:
public partial class Master2 : BaseMasterPage
{
public event EventHandler CalendarSelectionChanged;
public void Calendar_OnSelectionChanged(object sender, EventArgs e)
{
if (CalendarSelectionChanged != null)
CalendarSelectionChanged(this, EventArgs.Empty);
}
}
And here's the code in the content Page C1:
protected void Page_PreInit(object sender, EventArgs e)
{
Master.CalendarSelectionChanged += new EventHandler(OnMainCalendarSelectionChanged_SubContent);
}
private void OnMainCalendarSelectionChanged_SubContent(object sender, EventArgs e)
{
DoSomething();
}
but the CalendarSelectionChanged is always null and hence the function isn't called.
After some serious researching and dabbling around, I've finally made the calendar in the Master page to trigger an event in the content page.
The Code in the Content Page is the same as mentioned in the question.
The Code in the Second Master page(Nested) is Changed to :
public partial class Master2 : BaseMasterPage
{
public event EventHandler CalendarSelectionChanged;
public void Calendar_OnSelectionChanged(object sender, EventArgs e)
{
OnCalendar_SelectionChanged_CustomEvent(e);
///.....
}
public virtual void OnCalendar_SelectionChanged_CustomEvent(EventArgs e)
{
if (Calendar_SelectionChanged_CustomEvent != null)
Calendar_SelectionChanged_CustomEvent(this, EventArgs.Empty);
}
}
Add the virtual reference path in the content page:
<%# MasterType VirtualPath="~/MasterPage.master" %>
I have question from ASP.NET
on every aspx page I need to have similar action / method and I don't want replicate code.
My method is checking if user has Session, if he doesn't have - browser will go to Login Page.
Maybe I should use some global method?
public partial class FirstPage : Page
{
protected void Page_Init(object sender, EventArgs e)
{
...
}
protected void Page_Load(object sender, EventArgs e)
{
sessionHelper = new SessionHelper();
if (sessionHelper.isUserLogged())
{
Response.Redirect("Login.aspx");
}
}
}
I have searched extensively on this, but cannot find the solution to my problem. I am trying to call a function in the code behind of a page from a user control on that page.
I have a web application that uses a master page. I am adding a user control that I wrote to one of the content pages. I added the user control to the aspx page by dragging and dropping it from the toolbox. I am able to see the user control from the code behind, but I cannot access the public functions. To fix that problem, I created an object of the user control in the code behind and used the LoadControl function. All of that seems to work fine.
The problem I am having is when I am trying to hook the into the EventHandler from the aspx page to the user control. Everything compiles and runs just fine, but I am not seeing anything happen on the page. I think the issue is that the EventHandler is always null.
User Control Code
public partial class ucBuyerList : System.Web.UI.UserControl
{
public delegate void BuyerSelectedEventHandler(object sender, EventArgs e);
public event BuyerSelectedEventHandler BuyerSelected;
private string name = "";
public string Name
{
get { return name; }
set { name = value; }
}
private string auid = "";
public string AUID
{
get { return auid; }
set { auid = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
private void OnBuyerSelected(EventArgs e)
{
if (BuyerSelected != null)
{
BuyerSelected(this, new EventArgs());
}
}
protected void lbBuyerList_SelectedIndexChanged(object sender, EventArgs e)
{
SetNameAndAUID();
OnBuyerSelected(e);
}
private void SetNameAndAUID()
{
name = lbBuyerList.SelectedItem.Text;
auid = lbBuyerList.SelectedItem.Value;
}
}
Parent Page Code
public partial class frmBuyerInformation : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Master.changePageTitle("Buyer Information");
buyerList.BuyerSelected += new ucBuyerList.BuyerSelectedEventHandler(buyerListControl_BuyerSelected);
}
void buyerListControl_BuyerSelected(object sender, EventArgs e)
{
DisplayBuyerInformation();
}
public void DisplayBuyerInformation()
{
tbName.Text = buyerList.Name;
tbAUID.Text = buyerList.AUID;
}
}
Can anyone see what I am doing wrong?
EDIT: This issue has been resolved. The code snippits above are now functional. If anyone runs into the issue I had, you can model the code above. Make sure that AutoEventWireup="true" in both the aspx and ascx pages. Thank you June Paik for your solution. Thank you Diego De Vita for your input as well.
I've been struggling with events for quite a while as well. Nowadays I always create them this way 'cause it's the only way I know it works. Haven't tested it with your code but here it goes anyway:
public partial class ucBuyerList : System.Web.UI.UserControl
{
public delegate void BuyerSelectedEventHandler(object sender, EventArgs e);
public event BuyerSelectedEventHandler BuyerSelected;
public string Name;
public string AUID;
protected void Page_Load(object sender, EventArgs e)
{
//Select the first buyer in the list when the user control loads
if (!IsPostBack)
{
lbBuyerList.SelectedIndex = 0;
}
}
private void OnBuyerSelected(EventArgs e)
{
BuyerSelectedEventHandler handler = BuyerSelected;
if (handler != null)
{
handler(this, new EventArgs());
}
}
protected void lbBuyerList_SelectedIndexChanged(object sender, EventArgs e)
{
Name = lbBuyerList.SelectedItem.Text;
AUID = lbBuyerList.SelectedItem.Value;
OnBuyerSelected(e);
}
}
In the parent page you can just call your function the same way you're doing it already.
It could be that Page_Load is too late in the page lifecycle to be using LoadControl and subscribing to the event. What happens if you move that code to the Page_Init method?
BreakPoint not working in Init, InitComplate, PreLoad events in ASP.NET page with C# in VS2008. But it is working for Page_Load event.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void InitializeComponent()
{
this.PreLoad += new System.EventHandler(this._Default_PreLoad);
this.InitComplete += new System.EventHandler(this._Default_InitComplete);
this.Init += new System.EventHandler(this._Default_Init);
this.PreRender += new System.EventHandler(this._Default_PreRender);
this.PreInit += new System.EventHandler(this._Default_PreInit);
this.SaveStateComplete += new System.EventHandler(this._Default_SaveStateComplete);
}
protected void _Default_InitComplete(object sender, EventArgs e)
{
........
}
protected void _Default_Init(object sender, EventArgs e)
{
.........
}
protected void _Default_PreLoad(object sender, EventArgs e)
{
..........
}
}
EDIT: move your handlers adding into the OnInit instead of InitializeComponent method:
override protected void OnInit(EventArgs e)
{
// move your initializers here
}
But actually you don't need these initializers at all because of all these handlers could be hooked automatically with AutoEventWireUp=true e.g.:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Page_PreLoad(object sender, EventArgs e)
{
.........
}
protected void Page_InitComplete(object sender, EventArgs e)
{
........
}
protected void Page_Init(object sender, EventArgs e)
{
.........
}
protected void Page_PreRender(object sender, EventArgs e)
{
.........
}
protected void Page_SaveStateComplete(object sender, EventArgs e)
{
.........
}
}
EDIT II: As far as I remember, InitializeComponent is for VS 2003, .NET v1.1. Back then, the InitializeComponent was a place where the IDE serialized construction of the WebForm.
Now this method get never called from your code, so there are no event handlers you expected (and supposed to be added). Now there are 2 options to add hanlers: with AutoEventWireUp=true for general Page events and e.g. in overrided OnInit method.
Try this to help solve your problem of some breakpoints working, some not:
In the Debug menu, select "Delete All Breakpoints"
Save your solution and close Visual Studio
Open your solution, and re-establish your breakpoints in your events.
This should ensure that your breakpoints are set and hit properly.