Set value of Asp DropDown List is not Working - c#

I have an asp.net DropDownList that I use to filter a RadGrid. After the filter has been applied I can click on a row to edit the record on a separate page. I have a requirement to provide the ability, if the wrong row was selected, to return to the previous search page and display the same records with the same filter. I have taken care of the return and showing the same filtered records.
I also need to show the same value in the dropdown list that was chosen to create the filter. I am trying to do this using a session variable. The session variable gets created on the search click and I am trying to select the same item from the drop down using this code when the user returns to the search page.
string value = (Session["ComplaintType"] != null) ? Session["ComplaintType"].ToString() : String.Empty;
ddlComplaint.Items.FindByValue(value).Selected = true;
It is not working ant I get this error message: Object reference not set to an instance of an object.
Not sure why I am getting that error the string value is equal to the text value of the item selected from the Drop down??

I was able to solve this problem by setting the dropdownlist selected value in the BindDropDown method where I bind the database to the database. Once it was bound I could select a value from the database as the default value.

Related

ASP MVC3 DropDownListFor with blank default option

I'm populating a small set of drop down list items in my model list so:
SecSellerCodes = (from s in db.DropDownValues
where s.Field.Equals("SecSellerCd") &&
s.DisplayPage.Equals("Agent Create")
select s).Select(x => new SelectListItem
{
Text = x.DisplayValue,
Value = x.AllowedValue
}).Distinct().ToList();
I would like the user to be able to select from the list of items pulled from the query above, plus include a blank option at the top of the list in case they do not want this particular field populated.
Using the following helper, this works fine once the page loads/user saves to the database. However, if they need to reload the page and make adjustments, the blank string.Empty item is always selected (no matter the value in the table).
#Html.DropDownListFor(model => model.SecSellerCd, Model.SecSellerCodes, string.Empty)
Any idea which helper/technique to use to accomplish this?
In your model code you need to assign the selected value to model.SecSellerCd. So if you want them to make changes from previous selection then you need to save the selected value in the database as well.

Not getting value from a ASPxRadioButtonList inside a user control

I have a scenario in which i have a user control having a ASPxRadioButtonList, i have a data source bind to this list.
Now when i select some option in the ASPxRadioButtonList and on update button when i try to get the selected item, it returns null.
i used following code:
int id = ((ASPxRadioButtonList)ucPlannedRelease.FindControl("lstRdoTicketDetail")).Value.ToString();
Even i tried to create a function in the control and to get the select item but it always returns null.
I am unable to find where i am doing wrong

how to enable the RadioButtonList while retriving from database

I am using a RadioButtonList,Active and InActive are list items.
I didt specify any values to the list items and i am using this code for inserting the value to the database, every time its taking the true value only..
staffMaster.IsActive = ui_rblIsActive.Enabled == true ? true : false;
staffMaster is a VO object which i created in the WCF layer..
while retriving from database i ve tried this code specified below, for enabling radio button, but its not showing as enabled.. table has only true values..
ui_rblIsActive.Enabled =Convert.ToBoolean(ui_gridView.SelectedRow.Cells[13].Text);
What mistake i have done here???? what i ve to do, to insert & retrive data properly..?
ui_rblIsActive.Enabled is just telling you whether the control is enabled, not what if anything is selected. You want to use ui_rblIsActive.SelectedValue instead. If you did not privde any value you will get the text of the radio button label.
Can you give more information, with the info you provided, I guess you should use rbl's SelectedItem.(Text|Value) property
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.radiobuttonlist.aspx

Dropdown list selected item always set to default value

I wish to pass values of the selected items into a database and however noticed that the selected item is not what is sent into the database you can see in the snap shot below.
during run time the following value is recorded.
Where did it all go wrong with the dropdown lists selected item?
Counting on your intelligence...thanks.
Put your call to load the dropdownlist in
if (!Page.IsPostBack)
{
//LoadDropdownListHere();
}
Try DBNull.Value instead of null.
Edit:
I think you need to specify the type using this overload: http://msdn.microsoft.com/en-us/library/cc491445.aspx
CSMDataSource.InsertParameters.Add("CaseID", DBType.Int32, CaseIDDropDownList.SelectedItem.Text);
I found the problem for the dropdownlist always selecting the first value. It appears the dropdown list is always rebinded anytime i click a button that requires a post pack as such the data tha was binded at page load re binds a gain before the selected item gets picked as such the default first value gets picked all the time. to solve my problem i first disabled enable auto postback on the dropdownlist and in the code behind that is my .cs file during the page load, where i first binded the data to the dropdown list, i used a condition like if(!ispostback) to bind my data. what this does is when the page loads first time the dropdownlist is binded and if i should select an item from the drop down list, the "auto post back" that i disabled earlier on keeps the selected item selected and when i click and button that requires a post to the server, the (!ispostback) prevents the dropdownlist to be binded again before selection. so in effect, the selection is done first and afterwards if the page should load anew, the drop down list is binded again.
i was in a bit of a rush while typing so please bare with my typo errors. do call in anytime for more clarification...peace

Set selected value in a SharePoint Dropdown list

We are using SharePoint 2010 Foundation.
We have an item in a list that is a dropdown with values from another list.
When we access the list as a SharePoint list it works fine, we can select a value, save the list, the next time we access the list the correct value is selected.
We have programmed a form that will updated the list. When we pull up the form, select a value and save it, we can see by accessing the list directly that the value has been saved.
However, when we pull up the form again it is the first item in the list that is selected. Have tried storing the selected value is a temp variable before binding the list but have not been able to get it to work. Anyone know how to fix this?
We found a solution.
The trick was to get the number that is the first part of the ToString of the SPListItem, before binding the list.
Then use that number to set the selected value after the list is bound
Parameters:
SPListItem currentItem, string fieldName
Code:
string selectedValue = currentItem[fieldName].ToString().Substring(0,1);
//... Bind list
ddlLookup.SelectedValue = selectedValue;

Categories

Resources