How to Save Data without Page Reloading on Dropdown SelectedIndexChanged Event - asp.net

I am trying to update a DataTable on selection Index Changed event of DropdownList.
What I want is when I select/change dropDownList Item, the Selected Value should be saved into the database without Page refresh(without AutoPostback).

You will need to set AutoPostback to true.
But you can use an update panel to do a partial PostBack and not refresh the complete page.
See here
Use it like this:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always" ChildrenAsTriggers="true" >
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>item 1</asp:ListItem>
<asp:ListItem>item 2</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>

Related

Full postback triggered by CheckBox inside GridView inside UpdatePanel

I have a GridView inside of a UpdatePanel. In a template field is a CheckBox I use for marking items. Functionally, this works fine, but the CheckBox always triggers a full page postback instead of a partial postback. How do I get the CheckBox to trigger a partial postback?
<asp:GridView ID="gv_test" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cb_View_CheckAll" runat="server" AutoPostBack="true" OnCheckedChanged="cb_View_CheckAll_CheckedChanged"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
use trigger and scriptmanager
<asp:ScriptManager ID="script" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel" runat="server">
<ContentTemplate>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="cb_View_CheckAll" />
</Triggers>
</asp:UpdatePanel>
In your ScriptManager add EnablePartialRendering="true"
<asp:ScriptManager ID="ScriptManager1" runat="server" EnableViewState="False" EnablePartialRendering="true" EnableScriptGlobalization="true" > </asp:ScriptManager>
Or in Code Behind try to add AsyncPostbackTrigger
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(gv_test);

change control outside UpdatePanel

In short: I want to enable a button i.e. outside UpdatePanel when I click on a button from inside UpdatePanel.
Problem: The button enables but tags i and asp:Localize are not showing in the rendered code after button click from UpdatePanel updPnlOfferings
Detail:
I have a DataList inside UpdatePanel which contains delete button for every item in the list. delete button is firing an event on server which deletes the record and disables a button (i.e. outside of the UpdatePanel.
As someone suggested I kept my button(which need to be disabled) in UpdatePanel below
<asp:UpdatePanel ID="updPnlCourse" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:LinkButton ID="lnkDeleteCourseDetails" runat="server" OnClick="lnkDeleteCourseDetails_Click">
<i class="icon-remove-sign"></i>
<asp:Localize ID="Localize6" runat="server" Text="<%$ Resources:CBLabels, LBL_DeleteDetails %>" />
</asp:LinkButton>
<asp:ConfirmButtonExtender ID="lnkDeleteCourseDetails_ConfirmButtonExtender" runat="server"
ConfirmText='Are you sure you want to delete this course?' Enabled="True" TargetControlID="lnkDeleteCourseDetails">
</asp:ConfirmButtonExtender>
</ContentTemplate>
</asp:UpdatePanel>
The Main UpdatePanel which is firing the event is below
<asp:UpdatePanel ID="updPnlOfferings" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="pnlDSOfferings" runat="server">
<asp:DataList ID="dtlstOfferings" runat="server" OnItemCommand="dtlstOfferings_ItemCommand" EnableViewState="true">
</asp:DataList></asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
on dtlstOfferings_ItemCommand event I coded this
lnkDeleteCourseDetails.Enabled = true;
lnkDeleteCourseDetails.ToolTip = "";
lnkDeleteCourseDetails_ConfirmButtonExtender.Enabled = true;
updPnlCourse.Update();
This is a known problem as specified in this link.
The solution is to put all your content that are there inside the LinkButton, within a place holder like below, or make all the child controls as server controls.
<asp:LinkButton ID="lnkDeleteCourseDetails" runat="server" OnClick="lnkDeleteCourseDetails_Click">
<asp:PlaceHolder runat="server">
<i class="icon-remove-sign"></i>
<asp:Localize ID="Localize6" runat="server" Text="<%$ Resources:CBLabels, LBL_DeleteDetails %>" />
</asp:PlaceHolder>
</asp:LinkButton>

Update Panel is not working over there "OnSelectedIndexChanged" Event which is inside a repeater

I have put an update panel in a page and its working properly.In that page i was
loading an repeater and its also working properly.But inside that repeater i am firing an event "OnSelectedIndexChanged" in a dropdownList .while using it the page is getting refreshed. seems update panel is not working over there.
<asp:UpdatePanel ID="update_invest" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:Repeater ID="rptinvest" runat="server" OnItemDataBound="rptactions_ItemDataBound">
<ItemTemplate>
<td>
<asp:DropDownList ID="ddlemployee" runat="server" OnSelectedIndexChanged="ddlEmployee_SelectedIndexChanged"
AppendDataBoundItems="true" AutoPostBack="True">
</asp:DropDownList>
</td>
</ItemTemplate>
</asp:Repeater>
the above is the code....!!
Thanks Arshad..!
I think you need to register the postback triggering controls inside update panel. In your code snippet, it is ddlemployee. If it was in the mark up, you can do so like:
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlemployee" EventName="OnSelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
However, drop down control is nested inside repeater that you have to register it from code behind like:
For Each item As RepeaterItem In rptinvest.Items
Dim ddlemployee As DropDownList = DirectCast(item.FindControl("ddlemployee"), DropDownList)
ScriptManager1.RegisterAsyncPostBackControl(ddlemployee)
Next
Hope this help you. Visit here for more information about update panel and triggers.

Initial SelectedValue in DropDownList not posting back when re-selected

I have a DropDownList with the following markup:
<asp:UpdatePanel id="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Always">
<ContentTemplate>
<asp:DropDownList id="ddlCampaignModule" runat="server" OnSelectedIndexChanged="ddlDynamicType_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Value="-1">None</asp:ListItem>
<asp:ListItem Value="10">Category Menu</asp:ListItem>
<asp:ListItem Value="11">Best Sellers</asp:ListItem>
<asp:ListItem Value="12">Best Reviews</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
In the code behind I select the second option Category Menu. If I run my code and select any other option it posts back to ddlDynamicType_SelectedIndexChanged however if I re-select the second option (after selecting one of the other ones) my postback isn't triggered.
Am I missing something simple here?
Sounds like you are always selecting the 2nd option in the code behind, regardless of postback... ensure you only do that if it isn't!
if (!IsPostBack)
{
//select 2nd Item
}

Page refresh on clicking checboxlist in repeater

I am annoyed with a problem I have repeater control in updatepanel like this.
<asp:UpdatePanel ID="UpdPnlConstituentRepeater" ChildrenAsTriggers="true" runat="server">
<ContentTemplate>
<asp:Repeater ID="repConstituentInformation" runat="server" OnItemDataBound="repConstituentInformation_ItemDataBound">
<ItemTemplate>
<asp:DropDownList ID="dropRegistrantDownCostType" runat="server" AppendDataBoundItems="true"
AutoPostBack="true" OnSelectedIndexChanged="dropRegistrantDownCostType_SelectedIndexChanged"
EnableViewState="true">
<asp:ListItem Text="Select Type" Value="0" Selected="True" />
</asp:DropDownList>
<asp:CheckBoxList ID="chkBoxListRegistrantBenefits" AutoPostBack="true" runat="server"
OnSelectedIndexChanged="chkBoxListRegistrantBenefits_SelectedIndexChanged">
</asp:CheckBoxList>
</itemTemplate>
</ContentTemplate>
</asp:UpdatePanel>
The problem I face that whenever I select any value from dropdown all the page gets refreshed After spending hours on the google I found a solution i.e, on itemdatabound event of repeater we just need to add the following code after finding the dropdown,
Dim sm As ScriptManager = ScriptManager.GetCurrent(Page)
sm.RegisterAsyncPostBackControl(objDropdownlist)
It worked very well for the dropdown but same is not working for checkboxlist like using the scripmanager instance if I write sm.RegisterAsyncPostBackControl(chkBoxListRegistrantBenefits), it is not working :(
Add ClientIDMode="AutoID" to the Repeater control.
You should not have to add any triggers and you should not even have to call RegisterAsyncPostBackControl. The ChildrenAsTriggers should take care of that.
on Repeater ItemDataBound event
use screiptmanager method RegisterAsyncPostBackControl
this.ScriptManager1.RegisterAsyncPostBackControl(
e.Item.FindControl(" put here your checkbox ID "));
Add this Before start of <ContentTemplate>:
<Triggers>
<asp:AsyncPostBackTrigger ControlID="objDropdownlist" EventName="OnSelectedIndexChanged" />
</Triggers>

Resources