why my radcombobox is empty after postback? - c#

I have a radcombobox,I want get checked items and save it in database but when i click save button,page is load again and my radcombobox become empty and then all of my checked items disappear.please help me,how can keep ckeckeditems?

As D Stanley mentioned in the comments, you're probably not checking for a postback when populating your dropdown.
This is the general approach you need to use in your code...
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PopulateTheDropdown();
}
}
private void PopulateTheDropdown()
{
// Populate / databind your dropdown here
}
This will ensure your dropdown is not rebound when a postback occurs so you don't lose the selected value(s).

If you have autopostback activated you must save the selected value separately. Try to explicitly disable and check the value when the event fires:
<telerik:RadComboBox ID="RadComboBoxControl" AutoPostBack="false" OnSelectedIndexChanged="RadComboBoxControl_SelectedIndexChanged" runat="server" EmptyMessage="Select something"></telerik:RadComboBox>
protected void RadComboBoxControl_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
// Only test
var seleccionado = RadComboBoxControl.Items.FindItemByText(e.Text);
}
Check if you have assigned DataSource of the control in other parts of the code
RadComboBoxControl.DataSource = ...
RadComboBoxControl.DataBind();
This will also lose the selected element

Related

How to get asp.net dropdownlist to allow selections

I have a dropdownlist on a webform that I fill from a sql query, I then want to be able to select individual items in the dropdown and have corresponding fields from a datatable fill textboxes on the form
Problem is rowSel is returning 0 and the dropdown won't let me select any other item it always snaps back to the first itenm in the list.
Thought this might have something to do with autopostback being set to true, but if I set it to false that causes otheer problems, Not sure what else to try Im a winforms person and very new to asp.net
protected void ddClients_SelectedIndexChanged(object sender, EventArgs e)
{
int rowSel = ddClients.SelectedIndex;
txtClient.Text = dsShow.Rows[rowSel["ClientsTableFieldA"].ToString();
}
It should allow me to select a value from the drop down then populate some textboxes with fields from the datatable.
You could try:
protected void ddClients_SelectedIndexChanged(object sender, EventArgs e)
{
txtClient.Text = ddClients.SelectedItem.Value.ToString();
}
protected void ddClients_SelectedIndexChanged(object sender, EventArgs e)
{
txtClient.Text = ddClients.SelectedItem.Text;
}
As suggested by B. Seberle
DDL items have fields value and text so it depends how you bound the ddl to SQL datasource.
place break point on txtClient.Text = ddClients.SelectedItem.Text see if item list is empty.
it should not be necessary but you can force a ddClient.databind() in page_load if(!Page.IsPostback).
however ddClients_SelectedIndexChanged will only trigger on postback.

Dropdown List not returning the correct value

i am trying to retrieve the value on a previously databinded DropDownList like this:
<asp:DropDownList ID="DropDownListReception" runat="server" CssClass="span3 drop-down-reception"
OnPreRender="DropDownListReception_PreRender" OnSelectedIndexChanged="DropDownListReception_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
protected void Page_Load(object sender, EventArgs e)
{
var receptions = BLLFactory.ReceptionBLL.GetListAll();
DropDownListReception.DataSource = receptions;
DropDownListReception.DataBind();
}
On the DropDown PreRender i am personalizing this DropDown like this:
protected void DropDownListReception_PreRender(object sender, EventArgs e)
{
if (DropDownListReception.DataSource != null)
{
DropDownListReception.Items.Clear();
DropDownListReception.Items.Add(new ListItem("-- Select --", "NA"));
foreach (Reception item in (DropDownListReception.DataSource as IEnumerable))
{
DropDownListReception.Items.Add(new ListItem(item.Name + " " + item.Number, item.Id.ToString()));
}
}
}
this is working perfectly, my DropDown loads as it should, my problem is when i try to retrieve the SelectedValue in the SelectedIndexChanged event, it wont return the value as a string but as a type, what i am doing is:
protected void DropDownListReception_SelectedIndexChanged(object sender, EventArgs e)
{
//CurrentReception is a string i want to save in ViewState
//I also tried (sender as DropDownList).SelectedValue
//Tried DropDownListReception.SelectedValue
CurrentReception = DropDownListReception.SelectedItem.Value;
}
but this "DropDownListReception.SelectedItem.Value" will always return "Reception" which is the type of the item, not the id i assigned as the item value in the PreRender event. This also happens if i do this: "DropDownListReception.SelectedItem.Text", this also return "Reception". How can i return the string Value i assigned to the DropDown item?
var CurrentReception = DropDownListReception.SelectedItem as Reception;
string val = CurrentReception.PropertyYouNeed;
DropDownListReception.SelectedItem.Text and DropDownListReception.SelectedItem.Value will return the value of the selection, which is the second term in the ListItem used when you add it to the list. In other words, the problem is with item.Id.ToString(). It returns the type of the object instead of the ID. I'm not sure what your item object actually consists of so I'm not sure what you actually need, but are you sure it's not just item.Id? ToString() is generally the string representation of the object, if item.Id is an int, then ToString should give you the string equivalent of that int... but the fact that it doesn't work suggests item.Id is not actually an int.
I think you need to cast the list item to the type that you stored in it (Reception), then access the property from the Reception object that you want (from your description it sounds like you want the id). Like this:
protected void DropDownListReception_SelectedIndexChanged(object sender, EventArgs e)
{
//CurrentReception is a string i want to save in ViewState
CurentReception = ((Reception)DropDownListReception.SelectedItem).Id.ToString();
}
I figured it out, i was DataBinding the DropDownList on the PageLoad, which fires before the SelectedIndexChanged event. Since the DropDown does a PostBack when its value changes, the PageLoad was "Recreating" the DropDown and i was losing the changes before getting to the SelectedIndexChanged code.
Thank you all for your answers.:)

using P tag in C# codes?

I'm having a problem with this: in my source i gave an id and runat to my <p> tag as this :
<p style="border-style:solid" id="p1" runat="server" > Hello</p>
But when i try to run this code :
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
p1.Style.Add(HtmlTextWriterStyle.BorderColor,"" + DropDownList2.SelectedItem.Text + "")
}
and also this doesn't work:
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
p1.Style.Add(HtmlTextWriterStyle.BorderColor,"Blue");
}
Please help?
Make sure your DropDownList has AutoPostback = true
To add a little: buttons such as LinkButton and Button by default will cause postbacks, but other input items like CheckBox and DropDownList do not be default. Input items that don't postback by default will have an AutoPostBack property that can be set to true to force a postback when they are changed in some way by the user.
Without setting the AutoPostBack property, ASP.NET won't be notified that the user did something, and your events (in your code-behind) will never fire.

Changing the FormView record while in insert mode

I have 2 ObjectDataSource's - 1 for the GridView and 1 for the FormView.
I enabled selection on the GridView and all is well; the page loads, you select a record, and the FormView populates with the record.
However, once the FormView is in Insert mode, when I select a record on the GridView, it is not changing back to display the record I selected.
Where could I handle this in the code, as I don't see how to access the "Enable Selection" on the GridView in code behind?
What I have tried
protected void grdSearchGroup_SelectedIndexChanged(object sender, EventArgs e)
{
formGroupInput.DefaultMode = FormViewMode.ReadOnly;
formGroupInput.DataBind();
}
protected void grdSearchGroup_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
formGroupInput.DefaultMode = FormViewMode.ReadOnly;
formGroupInput.DataBind();
}
I think the simplest way to solve your problem is to handle the GridView's SelectedIndexChanging method, and set the FormView back to read-only mode (that way you can see the records details in the FormView like you want).
protected void yourGridView_SelectedIndexChanging(Object sender, EventArgs e)
{
yourFormView.ChangeMode(FormViewMode.ReadOnly);
}
Note that you must call the FormView's ChangeMode method in order to update the mode. Just setting the DefaultMode property is not enough.

DropDown List resets to the first item and doesn't return a selectedvalue

So I have a country dropdownlist and a state dropdownlist which is being populated dynamically based off of the country chosen. When I click the country the state dropdown gets populated just fine but the problem arises when I click a value (state) from the other dropdown, the list instead of retaining the selected item will go back to the first item of the list and no selectedvalue are displayed.
<td><asp:DropDownList ID="ddlState" runat="server"
DataSourceId="dsStateList"
DataTextField="state_nm"
DataValueField="state_cd"
OnSelectedIndexChanged="ddlState_SelectedIndexChanged"
AutoPostBack="true"
AppendDataBoundItems="true"
Width="160px" OnDataBound="ddlState_OnDataBound">
</asp:DropDownList>
</td>
<asp:DropDownList ID="ddlCountry" runat="server"
DataSourceId="dsCountryList"
DataTextField="COUNTRY_NAME"
DataValueField="COUNTRY_CIA_ID"
OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged"
OnDataBound="ddlCountry_OnDataBound"
AutoPostBack="true"
AppendDataBoundItems="true"
Width="160px">
</asp:DropDownList>
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
string comboStateCODE = ddlState.SelectedValue;
dsCompanyListParam.Text = comboStateCODE;
ddlCountry.DataBind();
ddlState.DataBind();
}
protected void ddlState_OnDataBound(object sender, EventArgs e)
{
ddlState.Items.Insert(0, "Please Select a State");
}
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
ddlState.Items.Clear();
dsStateList.SelectParameters["iCountryID"].DefaultValue = ddlCountry.SelectedValue;
dsCompanyListParam.Text = ddlCountry.SelectedValue;
Trace.Warn("ddlCountry_SelectedIndexChanged");
ddlCountry.DataBind();
ddlState.DataBind();
}
protected void ddlCountry_OnDataBound(object sender, EventArgs e)
{
ddlCountry.Items.Insert(0, "Please Select a Country");
}
I presume that somewhere in your Page_Load() you are making a call to a method that populates the dropdown... you need to encapsulate this into an IF !PostBack block:
// somewhere in PageLoad()...
If(!IsPostBack)
{
PopulateDropdown();
}
Using the convention above, the dropdown will only be populated on the first ever page load. What I suspect is happening is that when you make a selection from the other dropdown, the AutoPostBack is executing the Page_Load() method (as it should) and repopulating the dropdowns again.
Using the convention above should help avoid this.
Your state drop down is set to Autopostback - is it possible that your code to populate the country drop down is executing again on postback, thus rendering the selected state invalid because the country dropdown was repopulated
I would remove the ddlCountry.DataBind(); from the ddlState_SelectedIndexChanged event. I don't see why you need to do another DataBind there.
Solved it!
Ok, just so anyone who's stuck with a similar problem and can't find any other areas to look at here's how I fixed the stupid problem.
First of all, I was using a stored procedure and the stored procedure is concatenating the values from two fields. I set the parameters the Integer(4) which I didn't noticed there're a couple of countries with codes of more than 4. So basically, it's returning a NULL selectedvalue which in turn will not generate any value since my onselectedindexchanged method is based off of the selectedvalue and for some reason a NULL is not being processed.
So yeah, check your stored procs and parameter data! :D
Thanks for your time!

Categories