I am loading my with web controls and loading them based on the URL parameters.
I need to load a gridview if the user is in :&cat=8
My web control has a method which I need to call. One of its parameters is a session which is created only when the category is 8.
Technically I need a way of calling methods within my web control from my page. Placing it on page_load results in an error.
Thanks
You need to bind an event-handler on Load event of the Control itself.
private void Page_Load (object sender, System.EventArgs e)
{
// Add the following code:
yourControl.Load += new EventHandler(yourControl_Load);
}
private void yourControl_Load (object sender, System.EventArgs e)
{
//your code here.
}
Related
I have a user control (e.g. foo.ascx) that doesn't use any asp.net controls. I don't have a button click event or anything I can depend on. So the following isn't an option:
void Page_Load(object sender, EventArgs e) {
MyButton.Click += MyButton_Click; // <-- I can't do this, there are no asp.net controls
}
void MyButton_Click(object sender, EventArgs e) {
OnItemSelected("foo"); // fire my custom event
}
I need to fire an ItemSelectedEvent. I know how to make the methods and events like so
public event EventHandler<MyEventArgs> ItemSelected;
void OnItemSelected(string selectedItem) {
var tmp = ItemSelected;
if (tmp != null)
tmp(this, new MyEventArgs { SelectedItem = selectedItem });
}
What javascript do I need on the client site to indicate i'm firing the ItemSelected event (i.e. how to post back to the server correctly), and what do I need to implement in the code behind page to capture that information and call my OnItemSelected method?
If I am correct and you are using ASP.NET WebForm then you need to implement IPostBackEventHandler interface for your user Control. This interface defines the method(RaisePostBackEvent) which ASP.NET server controls must implement to handle postback events.
// Define the method of IPostBackEventHandler that raises your server side events.
public void RaisePostBackEvent(string eventArgument){
OnItemSelected("foo"); // fire my custom event
}
Then on client side you will able to execute server side event by invoking the following code:
__doPostBack(UniqueID, 'eventArgument');
where UniqueID - Unique ID of your user control. eventArgument can be empty or any value what you want (for example object in json format and etc).
This is how server side postback events can be implemented for custom server control.
I have some buttons in master page (mainpage) and I use the event click of its in other page by map like:
MainPage mp = (MainPage)(((BusinessApplication8.Controls.BusyIndicator)Application.Current.RootVisual).Content);
it's being run more than one and I can not use a e.Handdled to prevent them.
How can I do to control it?
for example we want our code inside below event(ListPish_Emza_Click) runs only once per click :
void ListPish_Emza_Click(object sender, RoutedEventArgs e)
{
if (sender == e.OriginalSource)
return;
Approve_Click(sender, e);
}
but above code runs more than one per click.
Firing an event manually is not recommended approach.
I can't realize your application architecture. But, you should put your code in a method in static class and use it.
How can i access asp.net server control in Ajax.AjaxMethod.
My code is below.
protected void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(Default2));
}
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)]
public void CreateModelPopupForRenameApplication()
{
Timer1.Enabled = true;
}
I am getting error.......on Timer1 (object ref not set)
Ajax methods are not meant to interact with the server controls on your page in this way, because you are not actually running through the page lifecycle like when you normally load the page. That's why it cannot find the Timer1 object.
You should think of these methods as stand-alone functions that calculate a value or do something independently of any page controls.
What I have going on is a Invokemember("Click"), the problem is I want to be able to grab the resulting innerhtml. The problem is i'm unsure of how/if it's possible to wait until the resulting action of the invokemember("click") is resolved. Meaning, in a javascript when you perform this click it will take you ot the next 20 items listed. However, i'm unsure of how to tell when that javascript will be fully loaded. Below is what I'm using.
private void button1_Click(object sender, EventArgs e)
{
HtmlElement button = webBrowser1.Document.GetElementById("ctl08_ctl00_InventoryListDisplayFieldRepeater2_ctl00_BlockViewPaging_Next");
button.InvokeMember("click");
HtmlElement document = webBrowser1.Document.GetElementsByTagName("html")[0];
}
One possible solution is to modify your "click" event handler in javascript so it changes the value of some hidden input field right before exiting the method (after all work is done). You can attach to the event of changing field from C# code and act when it's fired.
// Your init method - you can call it after `InitializeComponent`
// in the constructor of your form
Init() {
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
webBrowser1.Document.GetElementsByTagName("statusField")[0].AttachEventHandler("onchange", WorkDone);
}
void WorkDone(object sender, EventArgs e) {
HtmlElement document = webBrowser1.Document.GetElementsByTagName("html")[0];
}
That's the raw solution, I haven't yet checked whether "onchange" is a correct DOM event.
Also, you can't attach to DOM events before the document is completely loaded, that's why I put the attaching logic in the handler of DocumentCompleted event.
I have a simple user control that contains some buttons that fire events which I want to be handled by the page using the control. I have my code setup as such:
public event EventHandler Cancel;
public event EventHandler Confirm;
public void Confirm_Click(object sender, EventArgs e)
{
if (Confirm != null)
Confirm(this, e);
}
public void Cancel_Click(object sender, EventArgs e)
{
if (Cancel != null)
Cancel(this, e);
}
but when I try to call these from the page that is using the control's page load event I don't get any of the custom events
ASPX Code
<%# Register TagPrefix="btg" TagName="CustomControl" Src="~/Search/CustomControl.ascx" %>
<btg:CustomControl ID="btgControl" runat="server" ></btg:CustomControl>
could this be because my buttons in the user control are within an update panel?
You shouldn't be seeing methods. You should be seeing events.
In your parent page's load, you need to do this:
myUserControl.Cancel += new EventHandler(myUserControl_Cancel);
You can hit tab,tab to auto-generate the method stub. That will look like:
void myUserControl_Cancel(object sender, EventArgs e) {}
Then, this code will fire after it is called in the method of your user control. In order for that code to fire, you'll have to assign the events to button events on your user control.
edit: myUserControl is the id of your user control. Also, some would argue that event handlers should be in your page's init method.
edit:
Is your user control properly referenced in the page? i.e. Are you registering the user control in web.config or using the reference directive in the page?
Also, did you try cleaning the solution and rebuilding? If your user control is dynamically created/loaded, you'll have to wire up the events in the same scope as the instantiated control. In order to dynamically load the user control, you'll have to have a placeholder in your page and do the following:
UserControl control = Page.LoadControl("~/ControlPath/ControlName.ascx");
((MyUserControlClass)control).Cancel += += new EventHandler(myUserControl_Cancel); // etc...