i want the code to disable default selection in dropdownlist in asp.net and also on selection of a particular data field the values are displayed in the txtbox below.the dropdown should be filled with system related data like eg c:drive, d ... etc dynamically at run time.
then you can manually append ""--select--"" in the first place in the dropdownlist by edit items. and set appenddatabounditems to true.. thus you will get select in the first place rather than the first drive by default......
ok then use this function and cal this function on the databound event of dropdownlist.
void RemoveDuplicateItems(DropDownList ddl)
{
for (int i = 0; i < ddl.Items.Count; i++)
{
ddl.SelectedIndex = i;
string str = ddl.SelectedItem.ToString();
for (int counter = i + 1; counter < ddl.Items.Count; counter++)
{
ddl.SelectedIndex = counter;
string compareStr = ddl.SelectedItem.ToString();
if (str == compareStr)
{
ddl.Items.RemoveAt(counter);
counter = counter - 1;
}
}
}
}
and call this function in dropdownlist_databound() event.
then you can manually append ""--select--"" in the first place in the dropdownlist by edit items. and set appenddatabounditems to true.. thus you will get select in the first place rather than the first drive by default......
Related
I'm trying to create previous and next buttons on a page to navigate through items in a drop down list. Clicking next should select the next item in the DDL and clicking previous should go to the previous item.
Here's something I've attempted for the next button but it just takes me to the last row.
protected void btnNext_Click(object sender, EventArgs e)
{
int currentSelection = DDL.SelectedIndex;
for (int i = currentSelection; i < DDL.Items.Count; i++)
{
string nextSelection = (DDL.Items[i].ToString());
DDL.SelectedValue = nextSelection;
}
}
You're looping through all the items in the list until you get to the last one, and selecting each item individually until the loop exits. Drop a breakpoint inside that loop and debug to see what I mean.
You don't need any looping here at all. What you'd want instead would be simply:
int nextIndex = DDL.SelectedIndex + 1;
if (nextIndex + 1 >= DDL.Items.Count)
return; // We're on the last item, do nothing (or whatever you like)
DDL.SelectedValue = DDL.Items[nextIndex].ToString();
You easily can change selected value by its index so you don't need change it by its value.
int i = ddl.SelectedIndex;
if (i+1!=ddl.Items.Count)
{
ddl.SelectedIndex = i + 1;
}
else
{
ddl.SelectedIndex = i;
}
as you said in comment it wont change index if the current index is the last one.
I have to programmatically change a value in a GridView cell, depending on whether a compatible value exists or not in a populated List. If the value does exist, then take the value from a second List and modify the GridView cell value.
The logic would be as follows:
Check if the value of the list exists in the cell:
Get the corresponding value from the second list and set it as the Gridview cell value:
The code I was trying to implement is as follows:
if ((originalCombo.Count > 0) && (destinationCombos.Count > 0))
{
for (int k = 0; k < GridView1.Rows.Count; k++)
{
for (int i = 0; i < originalCombo.Count; i++)
{
for (int j = 0; j < destinationCombos.Count; j++)
{
if (GridView1.Rows[k].Cells[6].Text == originalCombo[i].ToString())
{
GridView1.Rows[k].Cells[6].Text = destinationCombos[j].ToString();
}
}
}
}
}
I think I have my entire logic wrong. Maybe I should use another method to iterate between the elements?
EDIT 1: So, probably I should be more clear about what I have and what I want to achieve. The originalCombo and destinationCombos are two lists, which are populated by a certain query. The GridView is already populated at the time I need to check for the value. So, if originalCombo and destinationCombos are not empty, I have to check the last cell on each row of the GridView, and if it's value is equal to one of the items of originalCombo,then I have to replace it with the respective value of destinationCombos
I'm using asp.net web forms application to view data of certain table in grid view & select a row in this grid view based on ID of this data row (Key Value) retrieved from query string
I tried using this code in Code behind
gridview1.SelectedValue= Request.QueryString["RowToSelectID"];
but it said that selected value is a read only property and can't be assigned
Is there another way to do that ?
Try the following and see here for more information.
var keyValue = 1; // Replace with your Convert.ToInt32(Request.QueryString["RowToSelectID"])
for (int i = 0; i <= this.gridview1.DataKeys.Count - 1; i++)
{
if ((int)gridview1.DataKeys[i].Value == keyValue )
{
this.gridview1.SelectedIndex = i;
}
}
I used the SelectedIndex. The record in the GridView having a key value of 1, will be selected.
I'm using a ListView in Details mode to display a list. I want to change the current index in two ways: firstly, by a mouse click (which works now), and secondly with + and - buttons. The problem is that when I click the button, the list loses focus and the row highlight disappears. How do I keep the highlight?
EDIT: Okay, I found the HideSelection property. But how do I change the selected index from the outside?
You can do something simple like this
this.listView1.Items[0].Selected = true;
Or you can iterate throught the list of items and find the one that you want to select.
private void PlusButtonClick()
{
int newIndex = 0;
for (int x = 0; x < listView1.Items.Count; x++)
{
if(listItem.Selected);
{
listItem.Selected = false;
newIndex = x++;
break;
}
}
this.listView1.Items[newIndex].Selected = true;
}
I have 14 Dropdownlist accroding to 7 Days.
Like For a day First dropdown list is Named as From-Time and Second Dropdown list is To-Time.
List Values are Set by 30 minutes Time Difference.
To-Time dropdownlist Should save only those list items which fall after From-time Dropdownlist.like if i select 1 Pm by first then second dropdown list should carry list-items after 1 Pm.
removing is done like this..
protected void ddlMonst_SelectedIndexChanged(object sender, EventArgs e)
{
RemoveListItem(sender as DropDownList,checkboxes);
}
private void RemoveListItem(DropDownList DDl,DropDownList[] checkboxes)
{
int CurrrentSelectedIndex = DDl.SelectedIndex;
String StartDDlName = DDl.ID.Substring(3, 3).ToString() + "ed";
String TargetedDDlName = string.Empty;
for (int i = 0; i < checkboxes.Length; i++)
{
TargetedDDlName = checkboxes[i].ID.Substring(3, 5).ToString() ;
if (StartDDlName.Equals(TargetedDDlName))
{
for(int j=0 ;j<CurrrentSelectedIndex;j++)
checkboxes[i].Items.RemoveAt(0);
}
}
}
but this logic fails if i selected again and again from First Dropdown list.It reomoves all from second one.
How to Handle this situation
Assuming your code is successfully removing the Items, then it makes sense that the To-Time DropDowns could end up being empty after multiple successive selects from the From-Time DropDown(s).
Instead of using .RemoveAt(), you could .Clear() your To-Time DropDowns and then .Add() the Items from the From-Time DropDown from its SelectedIndex and onwards.
// ...
// get reference to To/From DropDown(s) here
// ...
dd_time_to.Items.Clear();
for (int i = dd_time_from.SelectedIndex; i < dd_time_from.Items.Count; i++)
{
dd_time_to.Items.Add(dd_time_from.Items[i]);
}
You'll have to modify this to work on your DropDownList[]