In the following code everytime its taking only one item from dropdownlist. When I select any other item from dropdownlist its same as first item.
Please give solution
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DataSet _subcat = new DataSet();
_subcat = serviceus.Get_SERVICEUS_SUB_CATEGORYLIST(DropDownList1.SelectedValue.ToString());
lbsubcategory.DataSource = _subcat.Tables[0].DefaultView;
lbsubcategory.DataTextField = Convert.ToString(_subcat.Tables[0].Columns["CATEGORY_SUB1_NAME"].ColumnName);
lbsubcategory.DataBind();
Label5.Visible = true;
}
Check how you bind your dropdownlist. I think you're binding it everytime your page posts back to server. Try to use IsPostBack property of the page :
if (!IsPostBack){
DropDownList1.DataSource = datasource;
DropDownList1.DataBind();
}
Do you have:
if(!IsPostBack) {
DataBind();
}
around your initial databind (eg in OnLoad)
EnableViewState="False" on the lbsubcategory.
Assuming AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
Related
My question is very simple, i found lots of example but they are not concise and to the point.
below is the markup in my aspx file
<asp:DropDownList ID="ddlCountry" DataTextField="CountryName" DataValueField="CountryCode" runat="server">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
and following is the my code behind
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadCountry();
this.ddlCountry.SelectedValue = "USA";
}
}
private void LoadCountry()
{
DataTable dtResult = null;
dtResult = new DataTable("Countiers");
dtResult.Columns.Add("CountryCode");
dtResult.Columns.Add("CountryName");
DataRow dtRow1 = dtResult.NewRow();
dtRow1["CountryCode"] = "PAKISTAN";
dtRow1["CountryName"] = "PAKISTAN";
dtResult.Rows.Add(dtRow1);
DataRow dtRow2 = dtResult.NewRow();
dtRow2["CountryCode"] = "INDIA";
dtRow2["CountryName"] = "INDIA";
dtResult.Rows.Add(dtRow2);
DataView dv = new DataView(dtResult);
dv.Sort = "CountryName";
this.ddlCountry.DataSource = dv;
this.ddlCountry.DataBind();
this.ddlCountry.Items.Insert(0, new ListItem("--Please Select--", "-1"));
}
protected void Button1_Click(object sender, EventArgs e)
{
this.ddlCountry.SelectedValue = "USA";
}
}
i am simply loading this drop-down with countries, during initial page load event after calling LoadCountry method i am setting selectedValue to "USA" which gives no exception or error. But after postback by clicking Button1 i am again setting selectedValue to "USA" but this time exception occurred (has a SelectedValue which is invalid because it does not exist in the list of items). I understand the value "USA" is not in the list thats why it throws exception but why it didnt throws that exception initially?
It's a documented behaviour, but I actually don't know the reason for this:
When the selected value is not in the list of available values and a postback is performed, an ArgumentOutOfRangeException exception is thrown.
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.selectedvalue%28v=vs.110%29.aspx
I succeeded adding List-items to my DropDownList from my database, but after I ran my application and selected a value from the DropDownList, I checked my break point at code and I saw that the selected value is wrong. It always selects the first value.
My code is:
UserBLL uBLL = new UserBLL();
List<Item> list = uBLL.GetAllItemsCategory();
foreach (Item item in list)
{
int var = 1;
ListItem lItem = new ListItem(item.name, var.ToString());
modelsList.Add(lItem);
var++;
}
DropDownList2.DataSource = modelsList;
DropDownList2.DataBind();
How can I fix it so that when the following is executed I'll get the right selection and not just the first item?
order.nameItem = DropDownList2.SelectedValue;
You need to check for !IsPostBack in the Page_Load event, like this:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
// Put logic here to bind
DropDownList2.DataSource = modelsList;
DropDownList2.DataBind();
}
}
The following code is not executing!!
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow r = GridView1.SelectedRow;
TxtName.Text = r.Cells[0].Text;
TextBox2.Text = r.Cells[1].Text;
TextBox3.Text = r.Cells[2].Text;
TextBox4.Text = r.Cells[3].Text;
}
You must use EnableViewState="true" and bind just one time with ! IsPostBack in order to not erase your event foreach post.
because when your re build your grid you erase all events
In your page load :
if(! IsPostback)
{
//Bind your Gridview
}
Persist your datas with EnableViewState="true"
<asp:gridview id="YourGridView" EnableViewState="true" ...../>
we need to make the select button enable then only the GridView1_SelectedIndexChanged is fired i.e AutoGenerateSelectButton="true"
I got a weird problem. I use a DropDownList item in a web application. after i check for the first time one of the options all the items are added to end of the list.
initialized like this
protected void Page_Load(object sender, EventArgs e)
{
string myXMLfile = Server.MapPath("~/VMlist.xml");
DataSet dsMachines = new DataSet();
try
{
dsMachines.ReadXml(myXMLfile);
DropDownList1.DataSource = dsMachines;
DropDownList1.DataValueField = "machineID";
DropDownList1.DataTextField = "machineName";
DropDownList1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
and the SelectedIndexChanged:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
machineOS.Text = OSdata.OSDataRetrieve.getOSInfo(DropDownList1.SelectedItem.Text);
}
i use XML initializing. I'm also adding an image of 'before' and 'after'
You need to add the following to your Page_Load:
if (!Page.IsPostBack)
{
//code to bind dropdown
}
Your list is being bound each time the page posts back, which is why you're getting duplicates. Add the above line, and you should be all set.
Because you had set the AppendDataBoundItems="true"
You have to set it to AppendDataBoundItems="false"
Every time the dropdown selected Index changed event is fired, your dropdownlist again binded and added datasource items again.
In page_load, you should probably check for IsPostback.
Currently, you are rebinding the list to the listbox on each load. It's not just adding 3 new items - there is a scrollbar and I suspect it is re-adding ALL of the items.
My problem was that I manually called databind() but found that databind was already being called during because it was a nested databound control. Try commenting out your databind call and see if that fixes it.
I have one asp.net application, in which i have one dropdown which is binded to dataset. But after selecting one item, the drop down gets cleared all the value, How we can resolve this issue?
This is my dropdown list in design page:
<asp:DropDownList ID="ddlProduct" runat="server" CssClass="textEntry" Width="300px"
AutoPostBack="True" OnSelectedIndexChanged="ddlProduct_SelectedIndexChanged">
</asp:DropDownList>
and binding code is shown below.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindProductDdl();
}
private void BindProductDdl()
{
Products objProducts = new Products();
dsProducts dsProduct = new dsProducts();
ListItem olst = default(ListItem);
olst = new ListItem(" Select", "0");
dsProduct = objProducts.GetDataset("");
ddlProduct.DataSource = dsProduct;
ddlProduct.DataTextField = "Product";
ddlProduct.DataValueField = "Id";
ddlProduct.DataBind();
ddlProduct.Items.Insert(0, olst);
}
protected void ddlProduct_SelectedIndexChanged(object sender, EventArgs e)
{
Products objProducts = new Products();
dsProducts dsProduct = new dsProducts();
string criteria = "";
if (ddlProduct.SelectedItem.Text != " Select")
{
string id = ddlProduct.SelectedItem.Value;
criteria = "Id='" + id + "'";
dsProduct = objProducts.GetDataset(criteria);
productValue = Convert.ToDecimal(dsProduct.tblProducts.Rows[0]["Value"].ToString());
}
}
Thanks in advance..
From your question if I understand correctly you dont want the dropdown list to rebind if it is populated. Also please check your viewstate, this should not be happening, unless you have disabled viewstate
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack && ddlProduct.Items.count <=0 )
BindProductDdl();
}
Set the AppendDataBoundItems property of the dropdown to true and this will allow you to have a mix of databound items and non databound items (otherwise that insert statement is clearing your list)
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.appenddatabounditems.aspx
Do you have viewstate disabled on the page? Since you are only loading the items into the dropdownlist on the first load of the page, if viewstate is not enabled there will be nothing in the list after the postback.
Not positive, but I've seen in other languages and false interpretation...
You have your product Value as convert of ToDecimal which implies 99.999 for example.
If your ID that you are binding to is based on a whole number (ie: Integer basis), the bound value won't match... even if Value = 1 vs Value = 1.00 it won't match and will not be considered a valid "value" that matches your list. Convert your answer to a whole/integer number and it might do what you expect.
Without seeing the full source for the page I am simply speculating, but have you disabled ViewState on the page? If so, the DropDownList cannot retain its values between postbacks and the lists will have to be reloaded each time.