|
-
March 16th, 2009, 10:39 AM
#1
Postback processed, but event not hit
Hi
In page ( or in Control to be accurate ) i have DropDownList with
AutoPostBack set to true to postback when it is changed. I have
problem, that postback is really executed. Breakpoint in OnLoad
is hit, but event code OnSelectedIndexChanged is never hit ! I wonder
why.
Code:
<asp:DropDownList ID="cbAdults" runat="server" AutoPostBack="true" CausesValidation="false"
OnSelectedIndexChanged="InputChanged" Width="40px">
</asp:DropDownList>
Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillComboNumbers();
}
//if (Request["__EVENTTARGET"] == cbAdults.UniqueID)
// InputChanged(null, EventArgs.Empty);
}
protected void InputChanged(object sender, EventArgs e)
{
// THIS IS NEVER HIT, WHY ???
// do something
}
This happend to me several times in past and to solve it quicky,
I always had to write somethink like :
Code:
if (Page.Request["__EVENTTARGET"]==dropDown.UniqueId)
OnSelectionChanged ( dropDown, EventArgs.Empty );
This time however, I would like to know, what is real problem behind this.
I should write, that this code is inside control, but it shouldn't be the cause.
Thank you, I really have to solve this.
Martin
-
March 16th, 2009, 03:52 PM
#2
Re: Postback processed, but event not hit
Sounds like the event handler isn't setup right.
Did you verify on the properties window that the function is still set as the event handler? Also, make sure it's set in attributes for the drop down in the markup. Visual Studio has a habit of applying property changes to the design view but never updating the markup. Which results in the change being lost.
If it is set in both places, I'd suggest removing it. Do a rebuild. Then put it back.
Scott
-
March 16th, 2009, 10:54 PM
#3
Re: Postback processed, but event not hit
Maybe something else is happening in your code which might not be evident at the moment. I give below the code for the Default pages of a small Default application. You can open a sample web site in VS 2005 and substitute the code given below for the Default page and run it.
Here is the html code for Default.aspx
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="cbAdults" runat="server" AutoPostBack="true" CausesValidation="false"
OnSelectedIndexChanged="InputChanged" Width="40px">
<asp:ListItem Text="Item1" Value="This is Item1"></asp:ListItem>
<asp:ListItem Text="Item2" Value="This is Item2"></asp:ListItem>
</asp:DropDownList>
<asp:Label ID="mylabel" runat="server">I have nothing in me</asp:Label>
</div>
</form>
</body>
</html>
And here is the code for Default.aspx.cs
Code:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void InputChanged(object sender, EventArgs e)
{
mylabel.Text = cbAdults.SelectedItem.Value;
}
}
After substituting the code in the Default page of a new web site if you run it you will see a drop down and a label with text "I have nothing in me". Try changing the item in the drop down the label will tell give you the value of the selected item.
Try comparing the difference in the html code especially in the page directive etc.
Hope this helps.
Warm Regards,
-
March 17th, 2009, 06:59 AM
#4
Re: Postback processed, but event not hit
Hi
On the "blank" page it always work. Event when I put complete control on it. It works. But on another page it doesn't :-( I have been trying to find out what's wrong since morning. So far I have found several bugs that I repaired :-), but not this.
-
March 17th, 2009, 08:49 AM
#5
Re: Postback processed, but event not hit
Oh, this is custom control. Is it a user control or a server control? If it's a user control then the design is the same as a web page. Make sure the attribute for the event is set correctly and it should just work.
If it's a server control things can get complicately. You need to be sure you are rendering everything correctly. If it's a composite control the control needs to catch the events for the contained controls and fire off it's own events. If you render your own controls your need to correctly implement IPostBackDataHandler. It may be useful to implement INamingContainer. Because of name mangling you need to make sure you are using the correct id for things. I believe ClientId will give you the full id.
It may be helpful it you posted some of the code for your custom control.
-
March 17th, 2009, 09:38 AM
#6
Re: Postback processed, but event not hit
I have found that "root of all evil" in my code and I am going to fix it. But still don't know, how it is posible. Let me explain:
on Page there is UserControl, in user control there was server control,
particularly derived control of one third party control ( WebPanel from Infragistics ). In this infrg. panel I had to render custom header to have two titles ( or links ) on right and left side.
Code:
public class WebResPanel : Infragistics.WebUI.Misc.WebPanel
{
#region members
private string title1; // first title
private string title2; // second title
private string link1; // links
private string link2;
...
#endregion
#region properties
public string Title1 { get { return title1; } set { title1 = value; EvaluateHeaderText(); } }
public string Title2 { get { return title2; } set { title2 = value; EvaluateHeaderText(); } }
public string Link1 { get { return link1; } set { link1 = value; EvaluateHeaderText(); } }
public string Link2 { get { return link2; } set { link2 = value; EvaluateHeaderText(); } }
...
in EvaluateHeaderText function I build HTML text that is then rendered to client.
in this function, error was on line, where I tried to read ClientID of this control.
Code:
private void EvaluateHeaderText()
{
// simplified
string getthatstring = this.UniqueID;
....
}
This caused somehow rendering HTML IDs without full path ( Page is in MasterPage and it was rendered as if it was normal page )
This I have found after long time removing everything else from my Page, then only this control left and when I delete attribute "Title1" from ASPX page, it started to work.
Martin
-
March 17th, 2009, 09:54 AM
#7
Re: Postback processed, but event not hit
Wow, that's wierd. In the last custom control I did I had to have a dummy control because ASP.NET would add an ID attribute to the first control I rendered even if I already set the id of the control. Yes, that means the html would show 2 id attributes. The id would be the name of my custom control. Also, the would prevent events from working with this control. I've been meaning to look into this but since I fixed it by having this dummy control suck up this id attribute ASP.NET likes to injet it hasn't been a high priority.
Anyway, my thought is maybe your problem is related to ASP.NET injeting an id attribute.
Scott
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|