Have run this by my entire dev group to no avail. Seems simple enough, here's the question.
I have a UserControl with a single, non-databound dropdownlist. The UserControl is then dropped onto the masterpage and then also loaded into an inheriting webform that has the control manually loaded onto the page.
No AJAX, straight post-back.
The issue I am running into is that the SelectedIndexChanged event is not firing for the second instance of the DropDownList. The first one fires just fine.
TIA
Here is the code for the ascx:
<asp:dropdownlist id="SelectLanguage" autopostback="true" runat="server" enableviewstate="true">
<asp:listitem>- Select Language -</asp:listitem>
<asp:listitem value="xxx">Netherlands</asp:listitem>
<asp:listitem value="xxx">United Kingdom</asp:listitem>
<asp:listitem value="xxx">United States</asp:listitem>
</asp:dropdownlist>
Here is the CB for the user control:
protected override void OnInit(EventArgs e)
{
SelectLanguage.SelectedIndexChanged += new EventHandler(SelectLanguage_SelectedIndexChanged);
base.OnInit(e);
}
protected void SelectLanguage_SelectedIndexChanged(object sender, EventArgs e)
{
// do something
}
Does the postback actually happen for the second dropdown, or does the page do nothing?
If the postback isn't happening, you have javascript errors on your page.
Not sure exactly how this is, but the FancyBox lightbox jQuery library we're using to do the lightbox effect around the 2nd drop down is somehow messing with the bindings on the back end. No real time to figure out why, just let it be known it does in case anyone else runs into this issue.
Related
Before I begin, I have already seen this question about a very similar topic (as well as this one and this one), none of which answer my question completely. I already understand the concepts presented in these questions/answers, but I have more questions.
A) What happens if you have multiple controls with AutoPostBack="false" and you change a number of them before a postback? Take the following brief example (assume that everything else needed for the page is written correctly and trivially; e.g., Page_Load):
Default.aspx:
<asp:DropDownList ID="ddlFoo" runat="server"
OnSelectedIndexChanged="ddlFoo_Changed" AutoPostBack="false" >
<asp:ListItem Text="a" />
<asp:ListItem Text="b" />
<asp:ListItem Text="c" />
</asp:DropDownList>
<asp:DropDownList ID="ddlBar" runat="server"
OnSelectedIndexChanged="ddlBar_Changed" AutoPostBack="false" >
<asp:ListItem Text="1" />
<asp:ListItem Text="2" />
<asp:ListItem Text="3" />
</asp:DropDownList>
<asp:Button ID="btnQux" runat="sever" Text="Click for PostBack" OnClick="btnQux_Click"
Default.aspx.cs:
protected void ddlFoo_Changed(object sender, EventArgs e)
{
Response.Write("ddlFoo changed to " + ddlFoo.Text + ". ");
}
protected void ddlBar_Changed(object sender, EventArgs e)
{
Response.Write("ddlBar changed to " + ddlBar.Text + ". ");
}
protected void btnQux_Changed(object sender, EventArgs e) { }
Now, say you change ddlFoo to 3 and then ddlBar to b. Then, you click btnQux. You get the following output from Response.Write after clicking:
ddlBar changed to b. ddlFoo changed to 3.
Why does this happen? Do the OnSelectedIndexChanged methods get put into a stack to be called once a postback happens?
B) Why does my webpage load much more quickly when I use this approach and set AutoPostBack="false" for most of my controls? To be specific, I did this for a
CheckBox, a DropDownList, and a TextBox in a GridView, which retrieved ~1200 rows and 27 columns of data and took 10s in VS2008 debug mode versus 310s before. Why would the load/refresh time be so much faster?
EDIT: I released the code earlier this afternoon, and there was no significant difference between the load time of the old (AutoPostBack="true") and new (AutoPostBack="false") versions. I think that perhaps the debugger was doing something extra, which caused the large jump in load time. A better way to rephrase question B) might be this then: What could the debugger have been doing to cause this large jump in load time?
Warning: I'm no ASP.NET expert... If this turns out to be garbage, I'll delete it :)
A) I believe you will see the new values of all the controls, whenever the postback ends up happening, including all the change events, just as you described. The values have changed, after all - the AutoPostBack just affects the timing (and whether the postback occurs at all, of course).
B) There's more Javascript in the HTML delivered with AutoPostBack = True on all the controls, but not enough to make that enormous difference. As noted in your edit, it looks like that was a transient issue anyway - we can't really explain transient issues without more diagnostics.
You can use Fiddler to see what data is moving between client and server.
A. With fiddler you can easily see what data is sent to the server.
For example:
If you have DropDownList ddlFoo, when you click on button, you actually post this information:
POST http:// [server]:[port]/[resource.aspx] HTTP/1.1 Host: [server]:[port]
[Headers...]
_VIEWSTATE[viewstate data stored in html as hidden field value]&_EVENTVALIDATION=[event validaion
data]&ddlFoo=selecteItem&button1=ButtonText
When ASP.NET receives request, it compares ddlFoo's value and invokes it's event.
B. When you set AutoPostBack to true, then this javascript function will be generated:
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
And onchange attribute will be added to ddlFoo. So whenever you change DropdownList item, onchange event will be fired and __doPostBack function will be called, which will auto post back to the server.
Both answers you have gotten so far are correct. The simplified version of it is this:
A) When the form is finally POST'ed to the server, the server compares the form's current state with the ViewState and responds accordingly.
B) Enabling AutoPostBack causes javascript to be generated, and this javascript submits the form (which then triggers the postback).
Why does this happen? Do the OnSelectedIndexChanged methods get put
into a stack to be called once a postback happens?
Events that do not immediately post back (in your case ddlFoo_Changed and ddlBar_Changed) are cached.
Then those cached/pending events are raised along with btnQux's click event, when a page is posted back (by btnQux's click event).
You can read more here - ASP.NET Server Control Event Model
Im trying to have my drop down list make four controls visible on the selectedindexchanged event for the control.
Basically when the user chooses "Server" from the drop down list the event should fire and the user should see two extra options.
Tried a tonne of approaches so far including text_changed event but nothing.
Here is what i got so far
//adds new fields to the form when the user selects server as the asset type
protected void AddNewFields(object sender, EventArgs e)
{
//If the asset is a server then make the extra controls available
if (TypeDDL.Text.Equals("Server"))
{
DNLabel.Visible.Equals(true);
//DNLabel.Visible = true;
DomainNameTB.Visible = true;
RoleLabel.Visible = true;
RoleDDL.Visible = true;
}
}
<asp:DropDownList ID="TypeDDL" runat="server" DataSourceID="AssetTypeDS"
DataTextField="AssetTypeDescription" DataValueField="AssetTypeID" OnTextChanged="AddNewFields">
</asp:DropDownList>
Add AutoPostback="True" to your DropDownList and the above code should trigger
As for an explanation: The drop down list doesn't automatically post back to the server. Changing a selection happens on the client side. Once you add the above, it'll repost the page. If you don't want the whole page to flicker each time someone changes a selection, you can either use some client side Javascript or Jquery, or use an asp:UpdatePanel
Please set AutoPostBack="True" property of a DropdownList...
add AutoPostBack="true" in dropdown
<asp:DropDownList ID="TypeDDL" runat="server" DataSourceID="AssetTypeDS" AutoPostBack="true"
DataTextField="AssetTypeDescription" DataValueField="AssetTypeID" OnTextChanged="AddNewFields">
</asp:DropDownList>
I’m having trouble with my DropDownList. The events don’t fire! I've tested it on a separate project, with a DropDownList and a literal. Every time the selected value would change, I would add a little star “*” to the literal. No problems what so ever. But every time I try it on my webpage in the project, it fails.
Here is an image.
protected void ddlConsole_SelectedIndexChanged(object sender, EventArgs e)
{
ltlTesting.Text += "*";
}
UPDATE:
I've tried some things but still with no succes. I hope someone can tell me what i'm doing wrong. I'm wiring the events in the code behind now, but i've added a linkbutton next to the dropdownlist to see if it works.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
ddlConsole.SelectedIndexChanged += new EventHandler(ddlConsole_SelectedIndexChanged);
lnkGet.Click += new EventHandler(ddlConsole_SelectedIndexChanged);
}
Here is an image to see what's going on. The stripe in the literal at the beginning is added in pageload with the same code the star is added. Just to be sure it doesn't load twice. The "GET" linkbutton works fine. The dropdownlist doesn't...
Have you Set
AutoPostBack="true"
in control properties??
EDIT:
Remove
OnSelectedIndexChanged="ddlConsole_SelectedIndexChanged"
from the markup in the ASPX page and try again only with AutoPostback true and the
event defined in codebehind. The aspx page should look like this:
<asp:DropDownList runat="server" ID="ddlConsole" AutoPostBack="True"></asp:DropDownList>
Is the AutoPostBack of the dropdownlist true ?
Check AutopostBack property of Dropdownlist set it to true :
If picture is right and the AutoPostBack="True", is there any code that sets the value of ltlTesting when page loads?
Add the AutoPostback="True" and OnSelectedIndexChanged="ddlConsole_SelectedIndexChanged" to the ddlConsole attributes. You can delete the OnInit method, since you bound the SeletedIndexChanged event at design time.
I have a dropdownlist in my page, as Shown below:
<asp:DropDownList ID="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
<asp:ListItem Text="3" Value="3"></asp:ListItem>
</asp:DropDownList>
Code Behind:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//Logic Goes Here
}
In Page Load, From Session Variable I will get DropDownList id as "DropDownList1". Based on this ID string I can get the Control, but not able to get the Event Name associated with it.
So How to find the Event Name of Control by knowing Control??
Thnaks in advance
You may need to get it by Attributes.
string selected = EventDropDownList1.Attributes["onselectedindexchanged"].ToString();
Not sure what your asking here but the event can be wired up in two ways,
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)Handles DropDownList1.SelectedIndexChanged
Or in the page load part:
AddHandler DropdownList1.SelectedIndexChanged, AddressOf DropDownList1_SelectedIndexChanged
I know your question is asking to find the name of the event from the control name but you wire the event to the control. If you need to test which control fired the event i.e. you have the same code wired up to multiple drop down lists and you want to control the code flow based on the DDL ID then cast the sender to access it's values:
Dim id As String = CType(sender,DropDownList).ID
Hope that helps, if you can give more info I can perhaps give a more definitive answer.
Sorry answer in VB.Net.
You can use blow code:
Request.Form("__EVENTTARGET")
If you are using AutoPostBack = true you don't need to specify events. Auto postback will run the correct event based on what happened in the postback.
So you don't need to know the name of the event, just create the event in the back end and autpostback will fire the appropriate code.
I have placed a list box and a text box with Selected Index Changed and Text Changed event respectively in an aspx page. Now If I write something in text box and then with out clicking elsewhere select a value in list box, then first Text Changed event of text box is called then selected Index Change event of list box is called. After that again Text Change event of text box is called. Can any body give some insight why this happening??
Below is the markup:
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ListBox_IndexChanged">
<asp:ListItem Text="abc" />
<asp:ListItem Text="def" />
</asp:ListBox>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" OnTextChanged="Text_Changed" />
Code behind:
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void ListBox_IndexChanged(object sender, EventArgs e)
{
}
protected void Text_Changed(object sender, EventArgs e)
{
}
}
}
The problem/issue is that AutoPostBack works by attaching JavaScript events to your controls. Each browser handles JavaScript a little bit differently, so there's no real guarantee to the ordering.
When I try your code in Google Chrome, for example, the following sequence of events occurs:
Request sent to server
ListBox_IndexChanged event gets called
Text_Changed event gets called
Response goes back to client
However, in Internet Explorer 8, I noticed the following sequence:
Request sent to server
Text_Changed event gets called
Response goes back to client
Another request sent to server
ListBox_IndexChanged event gets called
Text_Changed event gets called
2nd Response goes back to client
This isn't a fault of ASP.NET, but just of varying JavaScript implementations across browsers, I suppose.
If you need to rely on a specific sequence of events, AutoPostBack isn't going to cut it. Depending on your situation, I might look at implementing my own JavaScript events using a cross-browser compliant library like jQuery. You could programmatically call back to the server by using the __doPostBack() function.
When a new item is selected the text in the text part of the listbox is replaced with the text of the item. That's why you get the second TextChanged event.