Initial SelectedValue in DropDownList not posting back when re-selected - asp.net

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
}

Related

How to display a message below the dropdownlist not an alert box when specific dropdownlist item is chosen?

To provide consistency, I cannot use an alert box to display a message in my code when someone chooses a specific item in a dropdownlist. It needs to display a red message below the dropdown field only if someone has chosen a specific item.
So in this case if someone chooses Alabama from a state dropdownlist for example, a red message needs to display below the dropdownlist box immediately after the choice is made. The old version of this form works accurately, but the new version of the form with many changes in it is not displaying this message.
In the code behind file (the aspx.vb file) I have these code snippets:
Protected Sub StateDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles StateDropDownList.SelectedIndexChanged
If StateDropDownList.Text = "AK" Then
AlabamaPanel.Visible = True
Session("Alabama") = "Yes"
Else
AlabamaPanel.Visible = False
Session("Alabama") = "No"
End If
StateDropDownList.Focus()
End Sub
I'm using previous code with certain edits and another part of the problem is that I don't understand the Session("Alabama") part - I don't find anywhere else in the code where Session("Alabama") is listed or rather to understand what Session() is and how it even would know what an Alabama Session is... So if you know something about Session() that might be helpful - I see this (https://msdn.microsoft.com/en-us/library/ms178581%28v=vs.140%29.aspx) but that's not helping me understand. The only place in the code that Alabama by itself exists is in the items of the dropdownlist. And maybe that is part of the problem since in the old code the Alabama was specifically noted in a long list like (in the aspx file, not the aspx.vb file) such:
<asp:DropDownList class="DropDowns" ID="StateDropDownList" runat="server" AutoPostBack="True">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="AL">Alabama</asp:ListItem>
<asp:ListItem Value="AK">Alaska</asp:ListItem>
<asp:ListItem Value="AZ">Arizona</asp:ListItem>
<asp:ListItem Value="AR">Arkansas</asp:ListItem>
<asp:ListItem Value="CA">California</asp:ListItem>
<asp:ListItem Value="CO">Colorado</asp:ListItem>
<asp:ListItem Value="CT">Connecticut</asp:ListItem>
<asp:ListItem Value="DE">Delaware</asp:ListItem>
<asp:ListItem Value="DC">District of Columbia</asp:ListItem>
<asp:ListItem Value="FL">Florida</asp:ListItem>
<asp:ListItem Value="GA">Georgia</asp:ListItem>
<asp:ListItem Value="HI">Hawai'i</asp:ListItem>
<asp:ListItem Value="ID">Idaho</asp:ListItem>
<asp:ListItem Value="IL">Illinois</asp:ListItem>
<asp:ListItem Value="IN">Indiana</asp:ListItem>
<asp:ListItem Value="IA">Iowa</asp:ListItem>
<asp:ListItem Value="KS">Kansas</asp:ListItem>
<asp:ListItem Value="KY">Kentucky</asp:ListItem>
<asp:ListItem Value="LA">Louisiana</asp:ListItem>
<asp:ListItem Value="ME">Maine</asp:ListItem>
<asp:ListItem Value="MD">Maryland</asp:ListItem>
<asp:ListItem Value="MA">Massachusetts</asp:ListItem>
<asp:ListItem Value="MI">Michigan</asp:ListItem>
<asp:ListItem Value="MN">Minnesota</asp:ListItem>
<asp:ListItem Value="MS">Mississippi</asp:ListItem>
<asp:ListItem Value="MO">Missouri</asp:ListItem>
<asp:ListItem Value="MT">Montana</asp:ListItem>
<asp:ListItem Value="NE">Nebraska</asp:ListItem>
<asp:ListItem Value="NV">Nevada</asp:ListItem>
<asp:ListItem Value="NH">New Hampshire</asp:ListItem>
<asp:ListItem Value="NJ">New Jersey</asp:ListItem>
<asp:ListItem Value="NM">New Mexico</asp:ListItem>
<asp:ListItem Value="NY">New York</asp:ListItem>
<asp:ListItem Value="NC">North Carolina</asp:ListItem>
<asp:ListItem Value="ND">North Dakota</asp:ListItem>
<asp:ListItem Value="OH">Ohio</asp:ListItem>
<asp:ListItem Value="OK">Oklahoma</asp:ListItem>
<asp:ListItem Value="OR">Oregon</asp:ListItem>
<asp:ListItem Value="PA">Pennsylvania</asp:ListItem>
<asp:ListItem Value="RI">Rhode Island</asp:ListItem>
<asp:ListItem Value="SC">South Carolina</asp:ListItem>
<asp:ListItem Value="SD">South Dakota</asp:ListItem>
<asp:ListItem Value="TN">Tennessee</asp:ListItem>
<asp:ListItem Value="TX">Texas</asp:ListItem>
<asp:ListItem Value="UT">Utah</asp:ListItem>
<asp:ListItem Value="VT">Vermont</asp:ListItem>
<asp:ListItem Value="VA">Virgina</asp:ListItem>
<asp:ListItem Value="WA">Washington</asp:ListItem>
<asp:ListItem Value="WV">West Virgina</asp:ListItem>
<asp:ListItem Value="WI">Wisconsin</asp:ListItem>
<asp:ListItem Value="WY">Wyoming</asp:ListItem>
</asp:DropDownList>
But now we are calling to another VB file to grab the state dropdown list and the full set as shown above is no longer in the code.
Since there is nowhere besides this list that no longer exists or this list that does exist in the separate vb file we are calling to which is successfully populating the dropdownlist, then that could perhaps be blocking the Session() from working?
In the new code (in the aspx file, not the aspx.vb file), all of the above is replaced with:
<label for="StateDropDownList">State <span class="Required">*</span></label>
<asp:DropDownList class="form-control" ID="StateDropDownList" runat="server">
</asp:DropDownList>
I'm trying to use (in the aspx file) :
<asp:Panel ID="AlabamaPanel" runat="server" Visible="false">
<tr>
<td align="center" colspan="3">
<br />
<asp:Label ID="AlabamaLabel" runat="server" visibile="false" Text="Alabama Text that I want to appear right below the statedropdownlist field if they choose alabama."
Font-Bold="True" ForeColor="#e00022" />
</td>
</tr>
</asp:Panel>
to get the text to show up on the page below the dropdown field if the user chooses Alabama and it's simply not displaying. I have also tried putting the long list of states back in instead of calling out to the separate dropdowns vb file.
I'm also trying to use Update Panel, wrapping the dropdown list with an
<asp:UpdatePanel ID="UpdatePanel1"
UpdateMode="Conditional"
runat="server">
<ContentTemplate>
</ContentTemplate>
</asp:UpdatePanel>
And that has benefited me because before that I wasn't able to include the autopostback="true" without it taking me to a blank screen whenever someone chose Alabama from the dropdown list. Now at least the entire screen isn't blanking out... But still I'm not getting the message to show on the screen.
Also I've tried this, but still no message appearing:
<asp:DropDownList class="form-control" ID="StateDropDownList" runat="server" AutoPostBack="True" CausesValidation="False" OnSelectedIndexChanged="StateDropDownList_SelectedIndexChanged">
Any ideas or is anything glaringly obvious as to why my message isn't showing up when someone chooses Alabama?
Try this:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="updtPnl">
<ContentTemplate>
<asp:DropDownList class="form-control" ID="StateDropDownList" runat="server" AutoPostBack="True" OnSelectedIndexChanged="StateDropDownList_SelectedIndexChanged">
<asp:ListItem Value="Al">Alabama</asp:ListItem>
<asp:ListItem>Other value</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Label ID="AlabamaLabel" runat="server" visibile="false" Text=""
Font-Bold="True" ForeColor="#e00022" />
</ContentTemplate>
</asp:UpdatePanel>
And change the code-behind as:
Protected Sub StateDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles StateDropDownList.SelectedIndexChanged
If StateDropDownList.SelectedValue = "Al" Then
AlabamaLabel.Text = StateDropDownList.SelectedItem.Text + " is selected!"
AlabamaLabel.Visible = True
Else
AlabamaLabel.Visible = False
End If
End Sub
In the original code that you provided you have the following issues:
1) you are looking at the text of the Dropdown for an abbreviation, which is not the text in the dropdown that is the value so you would need to use something like StateDropDownList.SelectedValue = "AK"
2) The AlabamaPanel gets set to visible however; you do not set the visibility of the label to true, so the panel is visible, but has no controls in it that are visible.

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.

How to Save Data without Page Reloading on Dropdown SelectedIndexChanged Event

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>

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>

Required Field Validator gets run on post back trigger

I have a dropdown list on my aspx page on which I have a RequiredFieldValidator applied over it. DropDown code is:
<asp:DropDownList ID="ddlglCategoryId" runat="server" CssClass="textEntry2"
AutoPostBack="true" ValidationGroup="Save" DataSourceID="dtsglCategoryId" DataTextField="LookupItem"
DataValueField="Id" AppendDataBoundItems="true">
<asp:ListItem Text="All" Selected="True" Value="0"></asp:ListItem>
</asp:DropDownList>
RequiredFieldValidator code is:
<asp:RequiredFieldValidator ID="rfvddlglCategoryId" InitialValue="0" runat="server"
ErrorMessage="Please select category" CssClass="Validations" ControlToValidate="ddlglCategoryId"
ValidationGroup="Save" Display="Dynamic" SetFocusOnError="true">
</asp:RequiredFieldValidator>
I have a post back trigger as well on my aspx page and when I change the category from the dropdown, page posts back and a grid on my page gets updated. But due to post back, the validator message appears and then disappears. I want this to appear only when "All" is selected from dropdown and user clicks save button.
Any guidelines?
Trigger:
</ContentTemplate>
<Triggers>
<%--<asp:AsyncPostBackTrigger ControlID="lbFileName" />--%>
<asp:PostBackTrigger ControlID="btnFileUploadSave" />
</Triggers>
</asp:UpdatePanel>
Button:
<asp:ImageButton ID="btnFileUploadSave" runat="server" ValidationGroup="Save"
ImageUrl="~/App_Themes/Default/images/update.png" ToolTip="Save"
Height="18px" onclick="btnFileUploadSave_Click"/>
You may need to set the AutoPostBack property of the dropdownlist to false. It sounds like a postback is getting fired with the OnSelectedIndexChanged event handler.
Set the ValidationGroup Property Of all Validaters same which you want to validate on button click and then set buttons ValidationGroup property same as Your Validators
For Example:
<asp:RequiredFieldValidator ID="rfvDoc" runat="server" ControlToValidate="FileUpload1"
ErrorMessage="Please Select A Document" ValidationGroup="OnSave">
</asp:RequiredFieldValidator>

Resources