Checkboxlist loop is not working - c#

I have a dropdownlist control and a button in asp.net page. The dropdownlist is populated from a method. If I select any item other than the first item, after clicking the button, I lose the selected item in the DDL and it selects the first item and also I am getting the value of the first item only in the button click event. How can I fix the problem?
<asp:DropDownList ID="userDropDown" runat="server" DataTextField="CustomerName" DataValueField="CustomerId">
</asp:DropDownList>
protected void Button1_Click(object sender, EventArgs e)
{
if(!page.isPostBack)
{
userDropDown.DataSource = CC.GetCustomers();
userDropDown.DataBind();
}
}

i think you must have bind userDropDown in Page_Load event without condition
if (!IsPostBack)
Please put dropdown binding part inside if (!IsPostBack) condition then it should work

Please bind dropdownlist values inside the if(!ispostback){} or
after submitting button please bind updated field to dropdownlistname.text

It sounds like you are binding your DropdownList to your datasource at ever request. Instead bind it only if Page.IsPostBack is false like below; (You may not need ObjectDataSource)
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//bind your datasource here (something like below)
userDropDown.DataSource = GetCustomers();
userDropDown.DataBind();
}
}
As soon as DataBind() method is called it will lose posted data of that object and the FirstItem will be selected by default.

Related

DataGrid Web Forms EditCommand/UpdateCommand. How to get changed value in TextBox

I've got a problem with my DataGrid's Edit command.
Here's my DataGrid:
http://imgur.com/0nmDJX0
and now i would like to edit my Title:
http://imgur.com/VU20GNa
How can i get the "abc" value?
((TextBox) e.Item.Cells[0].Controls[0]).Text still gives me the old value "sdfsd".
How can i get that "abc" value from textbox? I store my records to XML so all i need is to get these edited values.
Thanks.
Make sure you are not binding your data on every page load. If you do that, your new values are reloaded with old values before your event handler runs.
In my case, I just needed to put the BindData() inside the if (!Page.IsPostBack) block
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}

How do I Grab Selected Index on two Dropdownlist's OnSelectedIndexChanged?

I am trying to use two DropDownLists to filter data. I set both of the OnSelectedIndexChanged Equal to the method below. The problem is it is only grabbing the SelectedIndex of the DDL that is changed. Example: if i choose a option in DDL1 it grabs that value and doesnt grab the value of DDL2. They both have the same OnSelectedIndexChanged i figured it would grab the current value of both. Is there a way to make it look at both DDL Controls?
protected void BrandsList_SelectedIndexChanged(object sender, EventArgs e)
{
int DDLcatId = CategoriesList.SelectedIndex;
int DDLBraId = BrandsList.SelectedIndex;
IQueryable<Product> DDLprodResult = GetProductsDDL(DDLcatId, DDLBraId);
if(DDLprodResult == null)
{
}
else
{
CatLab.Text = DDLprodResult.ToList().Count().ToString();
productList.DataSource = DDLprodResult.ToList();
productList.DataBind();
}
}
Your code should work. Of course only one can be changed if you have set AutoPostBack="true"(default is false) on both. But you should get the correct SelectedIndex anyway in the handler.
So i will guess: you are databinding the DropDownLists on every postback. Do this only if(!IsPostBack), otherwise you always overwrite changes with the original values.
So for example in Page_Load:
protected void Page_Load(Object sender, EvengtArgs e)
{
if(!IsPostBack)
{
// DataBind your DropDownLists
}
}

Setting SelectedValue of a data bound DropDownList

I have an asp.net dropDownList which is automatically bound to a sqlDataSource to values of client type on page load. On page load I am also creating a Client object, one of it's properties is ClientType. I am trying to set the SelectedValue of the ddl according to the value of the ClientType property of the Client object unsuccessfully. I recieve the following error message "System.ArgumentOutOfRangeException: 'ddlClientType' has a SelectedValue which is invalid because it does not exist in the list of items". I understand that this is because the list has not yet been populated when I'm trying to set the selected value. Is there a way of overcoming this problem? Thank you!
You have to use the DataBound Event, it will be fired, once databinding is complete
protected void DropDownList1_DataBound(object sender, EventArgs e)
{
// You need to set the Selected value here...
}
If you really want to see the value in the Page load event, then call the DataBind() method before setting the value...
protected void Page_Load(object sender, EventArgs e)
{
DropdownList1.DataBind();
DropdownList1.SelectedValue = "Value";
}
Before setting a selected value check whether item is in list and than select it by index
<asp:DropDownList id="dropDownList"
AutoPostBack="True"
OnDataBound="OnListDataBound"
runat="server />
protected void OnListDataBound(object sender, EventArgs e)
{
int itemIndex = dropDownList.Items.IndexOf(itemToSelect);
if (itemIndex >= 0)
{
dropDownList.SelectedItemIndex = itemIndex;
}
}
EDIT: Added...
If you are doing binding stuff in Page Load, try to follow this way:
Move all binding related code in overriden DataBind() method
In Page_Load of Page add: (in case of control do not call DataBind directrly, this is a responsibility of a parent page)
if (!IsPostBack)
{
Page.DataBind(); // only for pages
}

selected index of DDL is always equal to Zero in asp .net

I have a Program Class, with properties as Id, ProgramName,ShortName and Code, im my app
I have a ASP DDL like
<asp:DropDownList ID="DDLProgram" runat="server"
OnSelectedIndexChanged ="OnDDLProgramChanged" AutoPostBack = "true">
</asp:DropDownList>
My OnDDLProgramChanged method is defined as
protected void OnDDLProgramChanged(object sender, EventArgs e)
{
List<CcProgramEntity> programEntities = GetAllPrograms();
DDLProgram.DataSource = programEntities;
DDLProgram.DataTextField = "Shortname";
DDLProgram.DataValueField = "Id";
//My Problem goes here
string programCode = programEntities[DDLProgram.SelectedIndex].Code;
}
My list is getting all the records correctly, I have checked it. But whenever I am changing an item in the DDL, the selected index in not changing. The selected index is remaining zero.Therefore, I am not being able to get the code of other items but the 0 index's item.
Can anyone help me in this case?
You are binding the data again in your selectedIndex Change event and it will reset your current SelectedIndex after rebinding. You don't need to rebind the data to your dropdown in SelectedIndex Change Event
It should be like..
protected void OnDDLProgramChanged(object sender, EventArgs e)
{
string programCode = programEntities[DDLProgram.SelectedIndex].Code;
}
You have to bind data to the DropDownList on page load method
if (!IsPostBack)
{
DDLProgram.DataSource = programEntities;
DDLProgram.DataTextField = "Shortname";
DDLProgram.DataValueField = "Id";
DDLProgram.DataBind();
}
Otherwise it will bind data everytime and hence clear the selection
Why are you assigning the DataSource in OnDDLProgramChanged, this would be resetting the selection you make.

Change a selected item in a listview based on the selection in another listview

I have two list views. In the Item command event of the first Listview i am showing the second list view in modal popup using ajaxtoolkit.
protected void lvSelection_ItemCommand(object sender, ListViewCommandEventArgs e)
{
this.lvPopup.Visible = true;
this.lvPopup.DataSource = linqdataSource;
this.lvPopup.DataBind();
this.mdlPopup.Show();
}
Now in the itemcommand event of the second list view I need to change the content of the selected item in the first listview.
Is it possible to do that?
protected void lvPopup_ItemCommand(object sender, ListViewCommandEventArgs e)
{
// Set the text of the first list view item to the selected item
// of the second list view.
lstView1.Items[lstView1.SelectedIndex].Text =
lstView2.Items[lstView2.SelectedIndex].Text
}
I'd think that if you were to set the CommandName of the selector button in the first ListView to "Select" - from the second list view's ItemCommand event, you should be able to alter either the SelectedItemTemplate or the current item for the selected item in the first list.
protected void lvPopup_ItemCommand(object sender, ListViewCommandEventArgs e)
{
lvSelection.SelectedItemTemplate = "<div>woohoo!</div>";
// OR...
lvSelection.Items[lvSelection.SelectedIndex].SkinID = "SomeNewSkinForExample";
mdlPopup.Hide();
}
Have you already tried to dynamically generate the items of the List?
On the event code of the 1st list, clear the Items from the 2nd list and populate it with whatever logic suits you.

Categories

Resources