Dropdown inside Repeater return only default value - c#

I have Repeater with Dropdown within
<ItemTemplate>
<asp:DropDownList runat="server" ID="ddl_PostponeTime" >
</asp:DropDownList>
</ItemTemplate>
When I fire onCommand Event with Linkbutton and trying to get and locate the DropDown list by repeater item number
ddl.selecteditem.value
I can't get the value I've selected, only the first (and default) Value comes.
Maybe it have something to do with UpdatePanel?
Any Other Ideas?

Wherever you bind your DropDownList, make sure that you're checking for Page.IsPostBack == false. Otherwise, you're always going to get the initial SelectedValue because ASP.NET is helpfully re-loading the whole DropDownList.

Yes, you need to use UpdatePanel. You need something like that:
<asp:UpdatePanel ID="TheUpdatePanel" runat="server"
UpdateMode="Conditional">
<ContentTemplate>
//your controls...
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
On the dropdowns set Autopostback=true.

Related

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 the selectedindexchanged event firing only once in my code?

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.

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

A control with ID could not be found for the trigger in UpdatePanel

I have an update panel that has UpdateMode of Conditional and ChildrenAsTriggers set to false. I only want a few controls to cause an asynchronous postback:
<asp:UpdatePanel ID="updPnlMain" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
// ...
<asp:Repeater ID="rptListData" runat="server">
<ItemTemplate>
<asp:Button ID="btnAddSomething" runat="server" OnClick="btnAddSomething_Click" />
</ItemTemplate>
</asp:Repeater>
// ...
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnAddSomething" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
I am getting the following error when I try and load this page:
A control with ID 'btnAddSomething' could not be found for the trigger in UpdatePanel 'updPnlMain'.
Since my btnAddSomething control is in a repeater and might not be there right away it acts like it is nonexistent. How can I get around this?
Because your control is in the repeater control and it is out of scope to the Trigger collection. By the way you don't need to add trigger because your button control is already in the UpdatePanel, it will update when you click the button.
Edit: There is a solution if you really want to update your updPnlMain updatepanel. You can put in another updatepanel and put your button in that panel. e.g.
<asp:UpdatePanel ID="updButton" runat="server" UpdateMode="Conditional">
<asp:Button ID="btnAddSomething" runat="server" OnClick="btnAddSomething_Click" />
</ContentTemplate>
and then simply call the updPnlMain.Update(); method in btnAddSomething_Click event.
It will actually do what you are looking for :)

how to assign a value to textbox which is outside of the updatePanel

i am having a Page.in that page button inside a UpdatePanel.when a user click on the button.
i need to assign a value to textbox which is outside of the updatepanel.
how to achieve this ? any suggestion it will be there ?
You can also place the TextBox in a Update panel, leave the button out of the update panel and set a trigger that will cause the button to do a Async postback like this:
<asp:Button ID="btnSubmit" runat="server />
<asp:UpdatePanel ID="upTextBox" runat="server">
<ContentTemplate>
<asp:TextBox ID="tbTitle" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSubmit" />
</Triggers>
</asp:UpdatePanel>
And then add a button event that will change the text of the text box.
Or if you don't want to add the text box in a Update Panel you can register a startup script to set the text of the textBox something like this:
ScriptManager.RegisterStartupScript(this, GetType(), "setTextBoxText", "<script type='text/javascript'>$('#"+tbTitle.ClientId+"').val('submit button has been clicked');</script>", false);
Place the TextBox in a updatepanel with a trigger to the button here's a example:
<asp:UpdatePanel ID="upd1" runat="server">
<ContentTemplate>
<asp:Button ID="Btn1" runat="server />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="upd2" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtBox1" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Btn1" />
</Triggers>
</asp:UpdatePanel>
And on the Button Click, you can change the value of txtBox1 and call upd2.Update()
IIRC You would need to call .Update() on the additional controls (perhaps placing them in a second UpdatePanel and calling .Update() on that). See MSDN for an example.

Categories

Resources