Why is the selectedindexchanged event firing only once in my code? - c#

I have two drop down lists,the first one is ddlSubjectArea and below is it's source code:
<asp:DropDownList ID="ddlSubjectArea" runat="server" AutoPostBack="True" Height="20px"
OnSelectedIndexChanged="ddlSubjectArea_SelectedIndexChanged" Width="160px"
meta:resourcekey="ddlSubjectAreaResource1" class="notranslate">
</asp:DropDownList>
Here is the code for the second one:
<asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="ddlCompetency1" runat="server" Width="150px">
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlSubjectArea" runat="server" />
</Triggers>
</asp:UpdatePanel>
And this is the code for the selectedindexchanged event of the first dropdownlist.
protected void ddlSubjectArea_SelectedIndexChanged(object sender, EventArgs e)
{
tblSubjectMasterDLL.BindSubjectOnSubjectArea(ddlSubjectName, Convert.ToInt32(ddlSubjectArea.SelectedItem.Value));
bIsMultipleCompetency = tblSubjectAreaMasterDLL.IsMultipleCompetency(Convert.ToInt32(ddlSubjectArea.SelectedValue));
MultipleCompetencySetting();
}
The visibility of the second dropdownlist depends on the first dropdownlist's selected index.The selectedindexchanged event fires once but after that it does not fire.I have tried selecting different values every time but had no success.Where am I going wrong?
Another piece of information that I would like to provide is that even the page load event is getting called only once.Also if I remove the update panels,everything works normally.
I would like to add one more piece of information:If I set EnableEventValidation=false for the page,things start working as expected.I have also heard that setting EnableEventValidation to false is not recommended.

Related

Add Items To ListBox In UpdatePanel

I have a Listbox that exists in an UpdatePanel on an ASP.NET webform. Also inside the UpdatePanel, exists a Button that adds a bunch of ListItem's to the Listbox, this PostBackTrigger as shown below:
<asp:UpdatePanel ID="updSection6" runat="server">
<ContentTemplate>
<asp:LinkButton Text="Run Scan" ID="btnEditSectionStory6" runat="server" OnClick="btnRunScan_Click" />
<br />
<asp:ListBox ID="lbLog" runat="server" Height="263px" Width="747px"></asp:ListBox>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnEditSectionStory6" />
</Triggers>
</asp:UpdatePanel>
I add items in the click event:
protected void btnRunScan_Click(object sender, EventArgs e)
{
lbLog.Items.Add("Scan Beginning...");
.....hundreds of other items
}
The items are added to the ListBox, however it is after the entire btnRunScan_Click method is run, rather than adding them as each event occurs (So the user can receive messages as the actions occur). Am I missing an attribute or something on the UpdatePanel?
TIA
To Add Items without refreshing page or prevent from calling the cyclic events use following code and eliminate postBackTrigger inside the Trigger.Use following code that is tested working for me.Also use the scriptManager to prevent from exception over to use the updatePanel
.aspx design file
<asp:ScriptManager runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="updSection6" runat="server">
<Triggers>
</Triggers>
<ContentTemplate>
<asp:LinkButton Text="Run Scan" ID="btnEditSectionStory6" runat="server" OnClick="btnRunScan_Click" />
<br />
<asp:ListBox ID="lbLog" runat="server" Height="263px" Width="747px"></asp:ListBox>
</ContentTemplate>
</asp:UpdatePanel>
.cs file
protected void btnRunScan_Click(object sender, EventArgs e)
{
lbLog.Items.Add("Scan Beginning...");
}
As other also mention you can also use jquery with ajax to code over the client side to do this one.

OnSelectedIndexChanged not working within the asp:updatePanel

I have a dropdown in which list of countries will be listed.During the select OnSelectedIndexChanged event country code such as +91 will be shown in a textbox. The dropdown should be in <asp:Update panel> tag and the update panel just working fine. OnSelectedIndexChanged event also working in the code behind. but the problem is the country code value not displayig in the textbox.
Here is my code..
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:DropDownList
runat="server"
ID="ddl_country"
AutoPostBack="true"
OnSelectedIndexChanged="ddl_country_SelectedIndexChanged">
</asp:DropDownList>
<asp:RequiredFieldValidator
ControlToValidate="ddl_country"
ID="reqCountry"
ValidationGroup="req"
class="validation-msg"
ErrorMessage="Please select a country"
InitialValue="0"
runat="server"
Display="Dynamic">
</asp:RequiredFieldValidator>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger
ControlID="ddl_country"
EventName="ddl_country_SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
<asp:TextBox runat="server" ID="text_countrycode"/>
text_countrycode is outside any update panel in the code above. So it's in the form and the entire form needs to post back for this to change it's text.
You're probably using Updatepanels because you don't want the entire form to flash and post back.
Changing the dropdown list doesn't refresh the entire page because it's in an Update Panel. If you want the Textbox to be updated without an whole page post back, put it in it's own updatepanel and trigger it from the first one.
Or even easier, just put them together in the same one.
Add Textbox Control Inside the <ContentTemplate>.
Try this code :
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:DropDownList runat="server" ID="ddl_country" AutoPostBack="true"
OnSelectedIndexChanged="ddl_country_SelectedIndexChanged"></asp:DropDownList>
<asp:RequiredFieldValidator ControlToValidate="ddl_country" ID="reqCountry"
ValidationGroup="req" class="validation-msg" ErrorMessage="Please select a country"
InitialValue="0" runat="server" Display="Dynamic">
</asp:RequiredFieldValidator>
<asp:TextBox runat="server" ID="text_countrycode"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddl_country" EventName="ddl_country_SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>

AsyncPostBackTrigger works only the first time

I have an update panel that includes a gridview.
This grid has a drop down list column.
Beta aspx code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" EnablePartialRendering="false" UpdateMode="Conditional">
...
<asp:GridView ID="Gv_Queue" runat="server">
<Columns>
<asp:TemplateField HeaderText="H">
<ItemTemplate>
<asp:DropDownList ID="ddl_proprietà" runat="server" OnSelectedIndexChanged="ddl_proprietà_SelectedIndexChanged" AutoPostBack="true"/>
</ItemTemplate>
</Columns>
</asp:GridView
</asp:UpdatePanel>
I add the triggers of the DDL in the UpdatePanel by code:
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = dl.UniqueID; //dl is the Drop Down control
UpdatePanel1.Triggers.Add(trigger);
It works good at the fist selectedIndexChanged event... but the second time that the event is fired the trigger does not work correctly because a post back operation runs.
I already tried:
to change the AsyncPostBackTrigger to a PostBackTrigger but a missing
component exception is thrown.
change the updateMode in theUpdatePanel attribute to 'Always'.
Put another UpdatePanel in the ItemTemplate Column only for the
DropDown.
You have to re-create the trigger on every postback. You could add this code to the Load event of the DropDownList:
aspx:
<asp:TemplateField HeaderText="H">
<ItemTemplate>
<asp:DropDownList ID="ddl_proprietà" OnLoad="ddl_proprietà_OnLoad" runat="server" OnSelectedIndexChanged="ddl_proprietà_SelectedIndexChanged" AutoPostBack="true"/>
</ItemTemplate>
</asp:TemplateField>
codebehind:
protected void ddl_proprietà_OnLoad(object sender, EventArgs e)
{
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = ((Control)sender).UniqueID; // sender is the DropDown control
UpdatePanel1.Triggers.Add(trigger);
}
I found a solution to my own question.
You were all rights: I naively forgot that the trigger did not work after the first event just because it must be recreate.
Strangely even the recreation of the trigger in the on_Load even did not resolve the problem.
I did the trick with another update panel like this:
<asp:TemplateField HeaderText="H">
<ItemTemplate>
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="ddl_proprietà" runat="server" OnSelectedIndexChanged="ddl_proprietà_SelectedIndexChanged"
AutoPostBack="true"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddl_proprietà" />
</Triggers>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
In my case the issue was because I had 2 controls on the page with the same Id. You may want to check this out if the previous responses did not resolve the issue.

Why is my DropDownList in asp.net not refreshing?

I have a simple DropDownList on my asp.net webpage:
It looks like this:
<asp:UpdatePanel ID="UpdatePanel4" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="FriendList" runat="server"
EnableViewState="False" Height="30px" Width="10%"
OnTextChanged="FriendListSwitch" AutoPostBack="True">
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSend" />
</Triggers>
<--!The button is a stub, I actually need to load it from the codebehind-->
</asp:UpdatePanel>
Then I have the codebehind like this:
void OnRosterItem(object sender, agsXMPP.protocol.iq.roster.RosterItem item)
{
FriendList.Items.Add(new ListItem(String.Format("{0}", item.Jid.User), ""));
FriendList.DataBind();
}
While debugging I can see that 'FriendList' is being filled, and also that 'btnSend' is being clicked.
Yet my DropDownList is not refilling itself, it just stays empty, why is it not refreshing?
Edit: I'd rather remove the button and replace it with
void OnRosterEnd(object sender)
{
UpdatePanel5.Update();
}
But then I keep getting
The Update method can only be called on UpdatePanel with ID 'X' before Render.
Solved, the DataBind() only worked inside a Page_Load for me.
The button 'btnSend' should be inside the update panel
try using OnSelectedIndexChanged event instead of OnTextChanged
If you need the button to update the dropdown then it needs to be in the update panel
If you need the dropdown list to be the trigger then asyncpostback trigger needs to have the control id of the dropdown list

partial page postback using javascript/jquery

I have two asp.net updatepanels on my page. One of them has a checkbox and other one has some labels. I want to update the contents of second updatepanel when checkbox is checked/unchecked. I am using following code:
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm._doPostBack('UpdatePanel2', '');
but it is doing full page post back.
Please suggest a solution.
You can set checkbox's AutoPostback to True and make it a trigger of the second UpdatePanel:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="CheckBox1" EventName="CheckedChanged" />
</Triggers>
</asp:UpdatePanel>
And then you can add label changing code in checkbox's "CheckedChanged" event handler, e.g.
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
Label2.Text = DateAndTime.Now;
}
It seems you are looking for update panel triggers, you don't need to trigger it by javascript...

Categories

Resources