Pls Help: Inheritance Control declared in aspx: Button Event firing! Inheritance control created programmatically: buttons NOT Firering

Hello all,

Facing a problem again hope u can help me out:

Control:

  

 [Serializable]
public class HeaderField
{
public string HeaderText { get; set; }
public bool NotSortable { get; set; }
public string SortExpression { get; set; }

public HeaderField(string text, string sortExp)
{
HeaderText = text;
SortExpression = sortExp;
}
}

public class EviSorterCommandEventArgs : CommandEventArgs
{
public string Message { get; set; }
public EviSorterCommandEventArgs(string message, CommandEventArgs originalArgs)
: base(originalArgs)
{
this.Message = message;
}
}

[Themeable(true)]
public class SortableTableHeaderRow : System.Web.UI.WebControls.TableHeaderRow, INamingContainer
{

#region Members

public IList HeaderFields
{
get
{
return (ViewState["HeaderFields"] != null ? (IList)ViewState["HeaderFields"] : null);
}
set
{
ViewState["HeaderFields"] = value;
EnsureChildControls();
}

}


public string SortExpression
{
get
{
return (ViewState["SortExpression"] != null ? (string)ViewState["SortExpression"] : "1");
}
set
{
ViewState["SortExpression"] = value;
}
}

public string SortDirection
{
get
{
return (ViewState["SortDirection"] != null ? (string)ViewState["SortDirection"] : "ASC");
}
set
{
ViewState["SortDirection"] = value;
}
}

public string ImageUrl
{
get
{
return (ViewState["ImageUrl"] != null ? (string)ViewState["ImageUrl"] : "");
}
set
{
ViewState["ImageUrl"] = value;
}
}


#endregion

public override
ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}


protected override void CreateChildControls()
{
Controls.Clear();

int i = 1;
if (HeaderFields != null && HeaderFields.Count != 0)
{
foreach (HeaderField headerFild in HeaderFields)
{

TableHeaderCell thc = new TableHeaderCell();
thc.CssClass = "silver";
Table tblSortHeader = new Table();
thc.Controls.Add(tblSortHeader);
tblSortHeader.Rows.Add(new TableRow());
TableCell tcHeaderText = new TableCell();
tblSortHeader.Rows[0].Cells.Add(tcHeaderText);

Literal litHeaderText = new Literal();
litHeaderText.Text = headerFild.HeaderText;
tcHeaderText.Controls.Add(litHeaderText);


if (headerFild.NotSortable == false)
{
ImageButton ibtnSortAsc = new ImageButton();
ibtnSortAsc.ID = "ibtnSortAsc" + i.ToString();
ibtnSortAsc.CommandName = "sortAsc";
ibtnSortAsc.CommandArgument = headerFild.SortExpression;

if (ibtnSortAsc.CommandArgument == SortExpression && SortDirection == "ASC")
{
ibtnSortAsc.ImageUrl = ImageUrl + "/Images/sorting/small_arrow_up_highlight.gif";
}
else
{
ibtnSortAsc.ImageUrl = ImageUrl + "/Images/sorting/small_arrow_up.gif";
}

ibtnSortAsc.Command += new CommandEventHandler(this.SortButtonClicked);

ImageButton ibtnSortDesc = new ImageButton();
ibtnSortDesc.ID = "ibtnSortDesc" + i.ToString();
ibtnSortDesc.CommandName = "sortDesc";
ibtnSortDesc.CommandArgument = headerFild.SortExpression;

if (ibtnSortDesc.CommandArgument == SortExpression && SortDirection == "DESC")
{
ibtnSortDesc.ImageUrl = ImageUrl + "/Images/sorting/small_arrow_down_highlight.gif";
}
else
{
ibtnSortDesc.ImageUrl = ImageUrl + "/Images/sorting/small_arrow_down.gif";
}

ibtnSortDesc.Command += new CommandEventHandler(this.SortButtonClicked);

tcHeaderText.RowSpan = 2;

TableCell tcBtnSortAsc = new TableCell();
tcBtnSortAsc.Controls.Add(ibtnSortAsc);
tblSortHeader.Rows[0].Cells.Add(tcBtnSortAsc);
tblSortHeader.Rows.Add(new TableRow());
TableCell tcBtnSortDesc = new TableCell();
tcBtnSortDesc.Controls.Add(ibtnSortDesc);
tblSortHeader.Rows[1].Cells.Add(tcBtnSortDesc);
}
i++;
this.Controls.Add(thc);
}
}
else
{
TableHeaderCell thc = new TableHeaderCell();
Literal litHeaderText = new Literal();
litHeaderText.Text = "No HeaderFields through the member provided!";
thc.Controls.Add(litHeaderText);
this.Controls.Add(thc);
}
}

protected void SortButtonClicked(Object sender, CommandEventArgs e)
{
SortExpression = e.CommandArgument.ToString();
switch (e.CommandName)
{
case "sortAsc":
SortDirection = "ASC";
break;
case "sortDesc":
SortDirection = "DESC";
break;
}
ChildControlsCreated = false;
RaiseEvent(sender, e);
}

private static readonly object EviSorterCommand = new object();

public event EventHandler SorterCommand
{
add
{
this.Events.AddHandler(EviSorterCommand, value);
}
remove
{
this.Events.RemoveHandler(EviSorterCommand, value);
}
}

protected void OnSorterCommand(EviSorterCommandEventArgs e)
{
EventHandler handler = (EventHandler)base.Events[EviSorterCommand];
if (handler != null)
handler(this, e);
}

protected void RaiseEvent(Object sender, CommandEventArgs e)
{
OnSorterCommand(new EviSorterCommandEventArgs("Raise Event Message!", e));
}
}
 

When I declare it in the aspx site the buttons are firering:

<table class="silver">
    <thead>
        <esc:SortableTableHeaderRow ID="sthr" runat="server" />
    </thead>
    <tbody> 

When I use a placeholder:

<table class="silver">
    <thead>
        <asp:PlaceHolder ID="phSorter" runat="server" />
    </thead>
    <tbody>

and declare the control programmatically:

SortableTableHeaderRow sthr = new SortableTableHeaderRow();
sthr.ID = "sthr";
sthr.HeaderFields = hf;
phSorter.Controls.Add(sthr);

big thx!

0
wuz
8/28/2008 9:13:11 PM
asp.net.web-forms 93655 articles. 6 followers. Follow

2 Replies
955 Views

Similar Articles

[PageSpeed] 8
Get it on Google Play
Get it on Apple App Store

wuz:

An imagebutton appers in the control hierachy like ctl00$content$componentsHeader$sthr$ibtnSortAsc3

but the contentplaceholder is not mentioned here don't know if this can ahve something to do with the problem.

Hi wuz,

Your question is not clear.

Could you description the question more details?


Hong-Gang Chen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Hong
9/2/2008 7:57:41 AM

 Hello,

 Sorry the contentplaceholder and the NamingContainer of the control hierachy were missleading.

So long story short:

Problem:

ButtonEvents within a dynamically created control are not firing

Solution:

When the control is created dynamically the buttons within are created and added to the controls selection atfer the creation of the PostBack Events. So that's why they are not firing. 

The only solution for me was to create the Buttons before the PostbackEvents and then recreate them with ChildControlsCreated = false:

 

public IList HeaderFields
        {
            get
            {
                return (ViewState["HeaderFields"] != null ? (IList)ViewState["HeaderFields"] : null);
            }
            set
            {
                ViewState["HeaderFields"] = value;
                ChildControlsCreated = false;
            }

        }

 

I tried to avoid the call CreateChildControls unnecessaryly because it doesn't make much sense to call it without the HeaderFields but it seems that this is impossible without disabling the events, but for sure i am open for suggestions :).

The resource which helped me quite a lot was http://www.peterkirchner.de/blogs/pk/archive/2007/09/29/asp-net-life-cycle.aspx (unfortunately in German)

Thx and kind regards,

Thomas

 

0
wuz
9/2/2008 9:14:33 AM
Reply:

Similar Artilces:

How to programmatically access "Create User" button in CreateUserWizard control and "Log In" button in Login control?
Hello, How can I programmatically access "Create User" button in CreateUserWizard control? Also how can I programmatically access "Log In" button in Login control?  Improving lifestyles for luxury pets: TheFabulousPet Hi, here is the way in this example I change only the buttons text on page load (you didn't specify what property exactly want to access):Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Login1.LoginButtonText = "test" CreateUserWizard1.CreateUserButtonText = "test2" End ...

Web User Control - Button Click
I have a web user control which is has one Button control on it. It has a event for handling the click... if I put this user control ( button ) at the top  of the page, the click event does not get fired just page is refreshed...but where as if I move to the bottom of the page, ButtonClick is fired ???? I am completely clueless why is this change of behavior with respect to the position of the control?? Thanks. could you show us the code that you have problem with ...   Here is the code ( i have taken out the unwanted part of the page ) Please note that the bSave does ...

ISSUE WITH Control's behaviour inisde UPDATE PANEL
 hii evry1..,             i have an strange issue...here..plzz chk it out..                      aspx page:            < asp:UpdatePanel ID="OUpdatePanel" runat="server" >                <ContentTemplate>             &nbs...

Button control event not firing on a wizard control?
I have an app that has a wizard control. On one of the wizardsteps, I have a button control and the server side click event will not fire!  Why? I know this one! 42!(Please post code ) Please Mark As Answer posts that helped you."If we learn from our mistakes, I should be brilliant by now." check that the button's onClick event in the event properties window actually has the name of the method you are using as the click event handlerMicky BIntranet Developer...

how to create button controls dynamically .. then i need that click event of buttons
how to create button controls  dynamically .. then i need that click event of buttons  urgent ???????Vino  Add dynamic controls with eventsWebsite Design Darlington - http://mdssolutions.co.ukhttp://lessthandot.com - Experts, Information, Ideas & Knowledgehttp://aspnetlibrary.com - An online resource for professional ASP.NET developersPlease remember to click "Mark as Answer" on this post if it helped you refer below snippet..public partial class _Default : System.Web.UI.Page {    protected void Page_Load(object sender, EventArgs e)  ...

Button control not firing from within a dynamically-created user control
Ok, I've been searching for the answer for a couple of days now, so thought I'd ask here.My basic issue is that I can't get a button to fire properly that is inside of a dynamically-created user control. Here's my setup:I've got a repeater on an aspx page that is made up of a bunch of dynamically-created user controls as follows:protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e){    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)    {        ...

Urgent!!! Form inside Windows form control (that is embedded to Web page i.e aspx), not displayed on button click
 Hi all I have a Windows Form Control(i.e Windows Control Library), that consists of a Button "button1" and  a panel "panel1". Also I have a form "form1.cs". On form "form1.cs" I have a label whose text value i have set to  "Hi". On click of button "button1" on windows form control i am opening the form "form1.cs"  inside a panel and it displays "Hi". The code of windows form control is as below :  public partial class UserControl1 : UserControl    {    ...

Custom User Controls and dynamically created web controls events not firing
I have a peculiar problem and since I am just starting to use .net I am confident I am doing something wrong, but I can't see it and I've wasted a lot of time so far trying to figure it out. Hopefully one of you can figure it out. I have a custom User Control and I am dynamically creating a RadioButtonList. If I remove the custom user control from the page the RadioButtonList works as expected and correctly. However, once I add the custom control, it does not fire the SelectedIndexChanged event. Below is some code. If all I do is remove: "<UC:TOPMENU id="TopMenu1"...

button click handler on .aspx page for user web control button
Hi guys,  I have a web user control which contains my search function(some textfields and a button named btnSearch).  I create a web part manager and a web part zone to put the web user control in it to allow the user to minimise/show the search function   In my web page(.aspx), I have a nested datagrid and the web part manger and the webpart zone. Is there a way get the event handle for the btnSearch.click in the aspx page? This is to allow me to filter the datagrid based on the search critial given in the search function...

Ajax Control toolkit ----Tab control ---Button click event is not firing
 Hi frdsi'm using ajax control toolkit controls in my web applicationi have done an application which uses tab controllogin tab and register tabfor both the bustton event is not firinghere is the code in html  <%@ Page Language="VB" MasterPageFile="~/DefaultMaster.master" AutoEventWireup="false" CodeFile="Tab1.aspx.vb" Inherits="Tabs_Tab1" title="Untitled Page" %> <asp:Content ID="Content1" ContentPlaceHolderID="SampleContent" Runat="Server"> <ajaxToolkit:ToolkitScriptM...

Registering an event handler for a button control which is dynamically created and added to a PlaceHolder control
Task: Create a form to add computer to database. Problem:  I have a button (Submit) which is dynamically/programmatically created in an event handler method and added to a PlaceHolder.  The page is render correctly but when I click the submit buttom, the event handler method is not being called.  Code:  The 'table' is then added to the PlaceHolder. protected void AddComputer_Click(object sender, EventArgs e){// ... More code ...#region Add Submit button rowTableRow tbRowSubmitBtn = new TableRow();TableCell tbCellEmptySubmit = new TableCell();tbCellEmptySubmit.I...

Please help me about dynamically created button control event
 My problem is I created a button control dynamically at runtime using placeholder in asp.net. when i click the button it doesnt call the event which i have given in the program. tell me how can i invoke an event for the button control.  :zzz: My coding isBtnSave = new Button();                <b>BtnSave</b>.ID = "ControlID_" + NumberOfControls.ToString();                BtnSave.Text = "Update"; &n...

web user Control and aspx button event Prob
Hi all,            I am using  web user control in aspx,I have a buttons in Usercontrol and related labels in aspx page.depends on the  button events, I have to show labels in aspx page. but the thing is first the ASPX page will  load then the ascx page will load.    I need It has to load first Button event in ASCX page and reflect back in aspx page. hwo to achive this i'e  First Button in ASCX page events  then ASPX page events...  Thanks & Regards,Murthy. Hi, there is no way...

Multiple Button controls on form not firing Click event
There is probably a better way to do this, but here is what is going on. Imagine two "div" sections that contain images that have been tiled. One div contains all of the images that belong to a product, the other div contains the library of images you may want to assign to the product. Under each image in the library section is a Button control "Add". Under each image in the "Assigned to product" div there is a "Remove" Button control to remove it from the product. I'm trying to provide functionality where the user can simply click "Add&q...

Web resources about - Pls Help: Inheritance Control declared in aspx: Button Event firing! Inheritance control created programmatically: buttons NOT Firering - asp.net.web-forms

Resources last updated: 11/24/2015 12:40:27 AM