Why my ListBox Control in my Web Control is not Raising the SelectedIndexChanged Event ?

  • Thread starter Thread starter msnews.microsoft.com
  • Start date Start date
M

msnews.microsoft.com

Hi All,

I am creating a control with ASP "ListBox" control.

Also I have added necessary code to handle the ListBox.SelectedItemChanged
Event.

But surprisingly the control is not comng to this code at all.

Why my ListBox Control in my Web Control is not Raising the
SelectedIndexChanged Event ?

Any suggestions please?

Thanks
Anand Ganesh
 
did you set the AutoPostBack property to true?

<asp:listbox ... AutoPostBack="True" />

Karl
 
Yes I did. Here is the code.

protected override void CreateChildControls()

{

foreach(string st in this.GlobalArrayList)

{

TheListBox.Items.Add(st) ;

}

TheListBox.Attributes.Add("AutoPostBack","True") ;

TheListBox.Attributes.Add("id","ListBoxCheck") ;

TheListBox.Attributes.Add("runat","server") ;

TheListBox.Attributes.Add("OnSelectedIndexChanged","TheListBox_SelectedIndexChanged")
;

this.Controls.Add(TheListBox) ;

Button TheButton = new Button() ;

TheButton.Click +=new EventHandler(TheButton_Click);

this.Controls.Add(TheButton) ;

//base.CreateChildControls ();

}


But still the TheListBox_SelectedIndexChanged function is not being called.

I am using the CreateChildControls() function to create my controls and I am
not overiding and using the render function.

Will this cause an issue?

Thank
Anand
 
TheListBox.Attributes.Add("AutoPostBack","true") ;
Will add an "AutoPostBack" Attrubute to the rendered oupput of your control,
but will not set this property of the control.

try:

TheListBox.AutoPostBack=true

TheListBox.Attributes.Add("id","ListBoxCheck") ;
TheListBox.OnSelectedIndexChanged +=new
EventHandlerTheListBox_SelectedIndexChanged);


Hope this helps.
Brian
 
TheListBox.Attributes.Add("AutoPostBack","True") ;

TheListBox.Attributes.Add("id","ListBoxCheck") ;

TheListBox.Attributes.Add("runat","server") ;

TheListBox.Attributes.Add("OnSelectedIndexChanged","TheListBox_SelectedIndexChanged")
;

Hi,

The 'attributes' collection you are adding to, is for client-side
attributes only. Instead, use the appropriate property of the ListBox
control like this:

TheListBox.AutoPostBack = true;
TheListBox.ID = "ListBoxCheck";
TheListBox.SelectedIndexChanged += new
EventHandler(TheListBox_SelectedIndexChanged);

Roger
 
Thanks a lot Roger it Worked.

Roger Helliwell said:
Hi,

The 'attributes' collection you are adding to, is for client-side
attributes only. Instead, use the appropriate property of the ListBox
control like this:

TheListBox.AutoPostBack = true;
TheListBox.ID = "ListBoxCheck";
TheListBox.SelectedIndexChanged += new
EventHandler(TheListBox_SelectedIndexChanged);

Roger
 
Back
Top