dropdown value not refresh in gridview - asp.net

I am using a DropDownList in a GridView like so:
<asp:DropDownList ID="drpstatus" Style="outline: 0" runat="server" SelectedValue='<%#Eval("status_value")%>'>
<asp:ListItem Text="Inactive" Value="0"></asp:ListItem>
<asp:ListItem Text="Active" Value="1"></asp:ListItem>
<asp:ListItem Text="Rejected" Value="2"></asp:ListItem>
</asp:DropDownList>
When I bind the first time it shows the correct selected value,
but when I change the value through another webform
and refresh this page the value still remains same.
I checked the database; it has changed but
it is not changing in the dropdown.

after you change the value, again bind the grid and check.
hope this may help you...and if not then provide more details

Related

ASP.NET Set RadioButton to Select by a String Value

I have a RadioButton control with 3 buttons.
I am retrieving a value from SharePoint list, and to select the RR control according to the value retrieved.
Which of the following is the correct method to use?
ResignationTypeRBList.SelectedValue = _request.ResignationType;
ResignationTypeRBList.SelectedItem = _request.ResignationType;
ResignationTypeRBList.SelectedIndex = _request.ResignationType;
<asp:RadioButtonList ID="ResignationTypeRBList" RepeatDirection="Horizontal" runat="server" CausesValidation="false">
<asp:ListItem Text="Resignation" Value="Resignation" Selected="True"></asp:ListItem>
<asp:ListItem Text="NPL" Value="NPL"></asp:ListItem>
<asp:ListItem Text="Others" Value="Others"></asp:ListItem>
</asp:RadioButtonList>
Thank you
If you are getting Value from Sharepoint list, then assign that to SelectedValue
ResignationTypeRBList.SelectedValue = "NPL";

Select multiple value in DropDownList using ASP.NET and C#

Select multiple value in DropDownList using ASP.NET and C#. I tried it to select single value from drop down but unable to find multiple selection.
In that case you should use ListBox control instead of dropdown and Set the SelectionMode property to Multiple
<asp:ListBox runat="server" SelectionMode="Multiple" >
<asp:ListItem Text="test1"></asp:ListItem>
<asp:ListItem Text="test2"></asp:ListItem>
<asp:ListItem Text="test3"></asp:ListItem>
</asp:ListBox>
Take a look at the ListBox control to allow multi-select.
<asp:ListBox runat="server" ID="lblMultiSelect" SelectionMode="multiple">
<asp:ListItem Text="opt1" Value="opt1" />
<asp:ListItem Text="opt2" Value="opt2" />
<asp:ListItem Text="opt3" Value="opt3" />
</asp:ListBox>
in the code behind
foreach(ListItem listItem in lblMultiSelect.Items)
{
if (listItem.Selected)
{
var val = listItem.Value;
var txt = listItem.Text;
}
}
Dropdown list wont allows multiple item select in dropdown.
If you need , you can use listbox control..
ASP.NET List Box
For multiple selection dropdown list,cannot accomplish it directly using dropdown..Can be done in similar ways..
Either you have to use checkbox list or listbox (ajax inclusive)
http://www.codeproject.com/Articles/55184/MultiSelect-Dropdown-in-ASP-NET
http://social.msdn.microsoft.com/Forums/vstudio/en-US/54374df7-5a54-42bc-83b8-ad5994cb634d/multi-select-dropdownlist
http://www.dotnetfunda.com/articles/article1591-multiselect-dropdownlist-in-aspnet-using-csharp-40-.aspx

Stop autopostback on keyboard input for dropdownlist

Is there any way do delay the autopostback on a dropdownlist until the list actually loses focus? I want to accept input from the keyboard without the page posting back after every keystroke. For example, take this code:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
<asp:ListItem >Test1</asp:ListItem>
<asp:ListItem >Tst1</asp:ListItem>
<asp:ListItem >twotest</asp:ListItem>
</asp:DropDownList>
Say I want to select twotest. I tab to the ddl and input 'tw' into my keyboard. However, as soon as the 't' is entered, the page automatically posts back, while the 'w' input is lost. Resetting focus doesn't really help any as then the ddl thinks that 'w' is the first input. Any help would be appreciated.
It seems impossible. You must remove AutoPostBack="true" and try another way to perform postback. Consider below solution:
when dropdown ddlCountry changed, the function ddlCountry_changed is called that trigger btnLoad click to perform postback. The button btnLoad is put inside an update panel so the page is not reload and you still have focus on the dropdown.
<script type="text/javascript">
function ddlCountry_changed() {
document.getElementById('<%= btnLoad.ClientID %>').click();
}
</script>
Country:
<asp:DropDownList ID="ddlCountry" runat="server" onchange="ddlCountry_changed();">
<asp:ListItem Text="Vietnam" Value="1"></asp:ListItem>
<asp:ListItem Text="United State" Value="2"></asp:ListItem>
</asp:DropDownList>
<asp:UpdatePanel ID="updatePanel" runat="server">
<ContentTemplate>
<asp:Button ID="btnLoad" style="display: none" runat="server" OnClick="btnLoad_OnClick" />
</ContentTemplate>
</asp:UpdatePanel>
It's a little more complicated than just handling the blur. You need to remove the onchange attribute (AutoPostBack="false") so keyboard navigation is possible and attach event handlers that submit the form when the value has changed via clicking, blur, or the enter key is pressed.
I ran into this same issue and created a plugin for handling this. Check out https://github.com/bstruthers/AutoPostBack-Fix.

DropDownList - First Item in the List is not posting back properly but all the others work

I have this DropDownList:
<asp:DropDownList id="ddlSort" runat="server" EnableViewState="true" OnSelectedIndexChanged="ApplyFilters" AutoPostBack="True">
<asp:ListItem Value="0">Sort By Relevancy</asp:listitem>
<asp:ListItem Value="1">Sort By Product Code</asp:listitem>
<asp:ListItem Value="2">Sort By Product Description</asp:listitem>
<asp:ListItem Value="3">Sort By Price - Lowest</asp:listitem>
<asp:ListItem Value="4">Sort By Price - Highest</asp:listitem>
</asp:dropdownlist>
I am setting the option to be initially selected in the Page_Load in if (!Page.IsPostBack) which defaults to "0". If I select Sort By Product Code (or any other option) it will call ApplyFilters without a problem. If I then change the Drop Down to the first option, the AutoPostBack is fired but it doesn't go into the ApplyFilters code.
If I change the markup to this:
<asp:DropDownList id="ddlSort" runat="server" EnableViewState="true" OnSelectedIndexChanged="ApplyFilters" AutoPostBack="True">
<asp:ListItem Value="-1">First Option</asp:listitem>
<asp:ListItem Value="0">Sort By Relevancy</asp:listitem>
<asp:ListItem Value="1">Sort By Product Code</asp:listitem>
<asp:ListItem Value="2">Sort By Product Description</asp:listitem>
<asp:ListItem Value="3">Sort By Price - Lowest</asp:listitem>
<asp:ListItem Value="4">Sort By Price - Highest</asp:listitem>
</asp:dropdownlist>
then selecting Sort By Relevancy will work properly after selecting any other option BUT selecting First Option won't (as in the first example).
I am using ASP.NET 2.0.
I have checked the control hierarchy and the view state is enabled for all the controls.
I have tried explicitly setting EnableViewState="true" without any success.
Does anyone have any ideas as all the information I have seen on this points to the view state being false and in an update panel - Neither of which are true in my case.
EDIT: I have put a dummy entry in for now as the first entry but this is not ideal.
This is the expected behavior.
Your event is OnSelectedIndexChanged, if open the dropdown and select the already selected option, asp.net will not detect that as a change.
Only when you select a different value, will the OnSelectedIndexChanged be fired.
I suggest having a default value in place, like in your second example, just more readable:
<asp:DropDownList id="ddlSort" runat="server" EnableViewState="true" OnSelectedIndexChanged="ApplyFilters" AutoPostBack="True">
<asp:ListItem Value="-1">Select Sort order</asp:listitem>
<asp:ListItem Value="0">Sort By Relevancy</asp:listitem>
<asp:ListItem Value="1">Sort By Product Code</asp:listitem>
<asp:ListItem Value="2">Sort By Product Description</asp:listitem>
<asp:ListItem Value="3">Sort By Price - Lowest</asp:listitem>
<asp:ListItem Value="4">Sort By Price - Highest</asp:listitem>
</asp:dropdownlist>
If you have a default sort order, you can set it on page load
if (!Page.IsPostBack)
{
ddlSort.Text = 0;
}

How do I get an ASP RadioButtonList where the buttons don't have any text next to them?

I have a list of radio buttons on my ASP.NET page displayed thus:-
<asp:RadioButtonList id="my_radio_box"
runat="server"
AutoPostBack="True"
RepeatDirection="Horizontal"
onselectedindexchanged="my_function">
<asp:ListItem Value="0"></asp:ListItem>
<asp:ListItem Value="1"></asp:ListItem>
<asp:ListItem Value="2"></asp:ListItem>
</asp:RadioButtonList>
but I would like the buttons to be displayed without any accompanying text (there'll be other things on the page to make clear what is required). I've tried leaving no space in between the >< in the list item, and also a space and a non-breaking space, but the value text is displayed whenever I do this.
How do I have a radiobutton with no text?
(I've tried setting the foreground colour the same as the background colour; this works, but doesn't seem right to me.)
MSDN says "If the Text property contains Nothing, the get accessor returns the value of the Value property. If the Value property, in turn, contains Nothing, String.Empty is returned."
So I imagine you could use a whitespace character in the Text property to 'fool' it?
Have you tried adding a whitespace to the Text property of the ListItem?:
<asp:ListItem Value="0" Text=" " />
EDIT
I created a test case using the above, and it works:
<asp:RadioButtonList ID="rd" runat="server">
<asp:ListItem Value="0" Text="" />
<asp:ListItem Value="1" Text="" />
</asp:RadioButtonList>

Resources