Forms Data Controls :: Setting The Selected Index / Value In A DropDownList?
Feb 8, 2011
I have a problem in setting the selected index /value in a drop down list. The list is bound by a Linq query and I want to add an item at the top of the list and set it as the selected item.
THis is the code i am using
ListItem li = new ListItem("Select", "", true);
list.DataSource = (from ap in edc.Approvers
where ! ap.approverEmail.Equals("")
orderby ap.approverEmail
select new {ap.approverEmail}).Distinct();
list.DataTextField = "approverEmail";
list.DataValueField = "approverEmail";
list.DataBind();
list.Items.Add(li);
list.Items.FindByText("Select").Selected = true;
The listItem li is the item i want to be first in the list and also selected. Adding this before the databind just loses the item.
View 2 Replies
Similar Messages:
Mar 31, 2010
I have a datalist that holds imagebuttons which all link to images stored in a database. When I click on any of the image buttons the datalist enlarges the chosen picture and manages to dothis as the datalist is binded by the uniqueID of the image in the database. So, when I am looking at the enlarged picture, it would be nice to be able to click 'Next' & 'Prev' link buttons to move onto the next or previous picture without having to return to the datalist in between looking at a different photo.
I have managed to set up methods for clicking on 'next' and 'prev' linkbuttons that will successfully browse back and forth between photos in the datalist but the selected index always starts at 0, i.e the first photo in the datalist. So if I'm viewing the 5th image in the list and press 'next', the first image in the list shows up. I thought just by clicking on an image button would automatically set the selected index but obviously this is not the case. I have read various threads and tutorials on the net and a lot of them point to dlIcons.SelectedIndex = e.Item.ItemIndex; I have tried to put this in the itemDataBound method for my datalist but it changes nothing. And if i try to put that line of code in other messages in the class i get error messages (I'm using Visual Web Developer C#).
its part of a final year project due in 3 weeks and by now I should be writing the report! The code I'm using is pretty huge and spread out between classes but I will include what I think might help illustrate how it all works:
<asp:DataList ID="dlIcons" runat="server" DataKeyField="AttachmentID"
OnItemDataBound="dlIcons_ItemDataBound" ItemStyle-BorderWidth="1px"
RepeatColumns="5" RepeatDirection="Horizontal" CssClass="Icons"
OnSelectedIndexChanged="dlIcons_SelectedIndexChanged">
<AlternatingItemStyle CssClass="AlternatingRowStyle" />
<ItemStyle CssClass="RowStyle" BorderWidth="1px" />
<HeaderStyle CssClass="HeadeBrStyle" />
<FooterStyle CssClass="FooterStyle" />
<SelectedItemStyle CssClass="SelectedRowStyle" />
<ItemTemplate>
<asp:Label ID="lblFilename" runat="server" Font-Size="Small"
Text='<%# Eval("Filename") %>' ></asp:Label>
<br />
<asp:LinkButton runat="server" id="btnDelete" CommandArgument="<%# Bind('AttachmentID') %>"
CommandName="DELETE" Text="Delete" OnClick="btnDelete_Click"
Font-Size="small" CssClass="btnDelete" />
<br />
<asp:ImageButton runat="server" ID="imgBtnFileType" width="120" Height="120"
ImageAlign="right" ImageUrl='<%#"http://localhost:49279/PDFViewer/GetAttachment.ashx?AttachmentID=" + Eval("AttachmentId") %>'
OnClick="imgBtnFileType_Click" BorderWidth="1PX" CssClass="imgBtnFileType"
CommandName="VIEW" CommandArgument="<%# Bind('AttachmentID') %>"/>
<br /><br />
</ItemTemplate>
</asp:DataList>
The CS file:
protected void dlIcons_ItemDataBound(object sender, DataListItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
DataRowView drv = e.Item.DataItem as DataRowView;
dlIcons.SelectedIndex = e.Item.ItemIndex;
int attachmentID = (int)drv["AttachmentID"];
string fileType = (string)drv["Type"];
ImageButton imgBtnFileType = e.Item.FindControl("imgBtnFileType") as ImageButton;
string fileName = (string)drv["FileName"];
Label lblFilename = e.Item.FindControl("lblFilename") as Label;
lblFilename.Text = fileName.Length > 12 ? fileName.Substring(0, 10) + ".." : fileName;
}
}
The next and prev link buttons onclick leads to these two methods:
public int nextPic()
{
dlIcons.Items.Equals(dlIcons.SelectedIndex++);
int nextAttach = (int)dlIcons.SelectedValue;
return nextAttach;
}
public int prevPic()
{
dlIcons.Items.Equals(dlIcons.SelectedIndex--);
int prevAttach = (int)dlIcons.SelectedValue;
return prevAttach;
}
these were void methods but i've been stuck on this for hours and have resorted to fiddling with just about everything.
View 6 Replies
Sep 13, 2010
I have a dropdownlist1 control in footer template, but when i go to edit command for editing an existing row. at that time. the footer dropdownlist box showing exact index which is selected in edit template dropdownlist box.
dropdownlist1 is in footer template and dropdownlist2 is in edit item template.
same datagrtid i am using for adding new rows via footer template and editing existing rows via edititem template.
how to make dropdownlist1 which is in footer template to selectedindex "0", when i go to editcommand. tried using itemdatabound but not working.
View 4 Replies
Sep 15, 2010
I have a gridview that receives data based on selection from a primary dropdownlist object. Works.
Within the gridview, there is a dropdownlist whose selected index is based on the query from the primary dropdownlist. Works.
Problem.
How do I force the used to change whatever value that the dropdownlist contains after they make their initial selection from the primary dropdownlist; except the default value of "make a selection".
Gridview, contains a checkbox which drives whether a selected row is inserted into the data base. The databind in done using an ObjectDataSource on the presentation page, not in the code behind.
View 11 Replies
Apr 28, 2014
<br />
<table align="center">
<tr>
<td>
<fieldset>
<legend>
[code]...
For Edit I want to select the OfficeName before Selecting a Department or any another way to do this
View 1 Replies
May 7, 2015
In ASP.NET, I can get the selected index of dropdownlist using:
var dropdown1 = document.getElementById('dropdown1');But I want the selected index of that dropdownlist, which is present INSIDE a GridView.How to get that using JavaScript?
View 1 Replies
Jan 5, 2014
I want to create a drop down list, where user can view the code name and the name the code when they click on the drop down list.
But after they choose, only the code is appeared. This is my current coding as for now.
<asp:DropDownList ID="ddlcodetype" runat="server">
<asp:ListItem Selected="True">-</asp:ListItem>
<asp:ListItem>DK Deck</asp:ListItem>
<asp:ListItem>EG Engine</asp:ListItem>
<asp:ListItem>CT Catering</asp:ListItem>
<asp:ListItem>OT Others</asp:ListItem>
</asp:DropDownList>
With this, both my name and the code name are appeared.
View 1 Replies
Jul 17, 2015
How do i change the name column text the current row when the selected index of drop down is changed? ex:first row is mudassar khan and belgium, when i change belgium i want mudassar to be replaced to a name which i fetch from database....
View 1 Replies
May 7, 2015
i need to Filter and display Google Map Markers from database based on DropDownList selection. for that I need to pass the selected value of DropDownList to the query.What should i do in the following code to do the above task?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
[code]...
View 1 Replies
Apr 27, 2016
how can i fetch the data from different tables stored in database on the value selected of dropdownlist..for ex.
DropDownlist1.selectedValue=="HEG01"
then the data from that table will be fatched from the databse and entries are displayed in gridview.
DropDownlist1.selectedValue=="RES01"
then the data from that table will be fatched from the databse and entries are displayed in gridview.
View 1 Replies
Jul 2, 2012
I want to filter grid view, drop down list should select the column and text box will filter the column. like ID NAME Phone Designation fields in gridview.i will select Designation in dropdown and type programmer in text box .so it should filter programmers from the gridview...
View 1 Replies
Feb 28, 2011
How to get the selected item from the listbox in the selected index changed event. I tried: Label1.Text = ListBox1.SelectedItem.Text; It is giving me object set to null reference.
foreach (ListItem item in ListBox1.Items)
{
if (item.Selected)
{
//lblResults.Text += item.Text + "
";
Label1.Text = item.Text;
}
}
No use, no value coming in to label.
View 5 Replies
May 5, 2010
I have a CascadingDropDown on an ASP.NET page. Now, the prompt text is "Select State". (list of states). However, on a different version of this page (ie querystring), i might want to set the selected index to "California" for example. How can i do this? The web service used by the ajax control (ie GetStates) gets invoked at the same time the jquery document.ready function is triggered (ie asynchronously).
So when i try and set the selected index in jquery, the items are not yet bound. Is there a way to attach a handler to the ajax dropdown so that i can set the selected index once the webservice call has completed, and the items are bound?
View 1 Replies
Dec 21, 2010
I have looked high and low and have not found what I need or found something that could work but it was old and did not work. I have a datalist (in an update panel) that contains 2 dropdownlist (Categories and Sub Categories) being populated by CascadingDropDown. What I want to do is set the selected index of the First one to the value I get from the Database after populating it in the web service. Then based on that I would like to adjust the 2nd dropdown for the new selected index. Here is the ui code
[Code]....
I saw a post about CascadingDropDownProperties using Atlas but I cannot seem to find anything about the AtlasControlKit anymore (is it part of AjaxControlKit?)
View 1 Replies
Jan 25, 2011
I have two control page in my aspx page. first one left side "tree view",second one right side " form design".Form design will change based on tree view selected index changed.i have 4 level child node(site, master , slave, space). I have seperate forms to each level of node.
cannot update tree node when update the forms. so i reload tree view.
now i need how to auto selected index change to tree node.
ex.
1 parent node
1.1 child node
1.2 child node
i have update "1.2 child node" rename to "1.3 child node"
and reload treeview so it will chage...
how set tree node.selected index = 1.3 child node....
View 3 Replies
Jan 15, 2010
My problem is I need to set selected value of a drop down list to a value I retreive from a database. The example only has a know value.
Below is the HTML and Code.
<asp:DropDownList
id="ddlCatogory1st1"
DataSourceID="srcCatogory1st1"
DataTextField="CategoryName"
DataValueField="CategoryID"
AutoPostBack="true"
Runat="server" />
<asp:sqldatasource id="srcCatogory1st1"
ConnectionString="server=(local);database=db;Trusted_Connection=true"
SelectCommand="SELECT CategoryID, CategoryName FROM Categories WHERE ParentCategoryID is null ORDER BY CategoryID"
Runat="server" ></asp:sqldatasource>
Sub Page_Load()
DISPLAY_CATEGORY()
End Sub
Sub DISPLAY_CATEGORY()
ddlCatogory1st1.DataBind()
ddlCatogory1st1.Items.FindByValue("A").Selected = True
End Sub
When I run this page I get
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 222:
Line 223: Sub DISPLAY_CATEGORY()
Line 224: ddlCatogory1st1.DataBind()
Line 225: ddlCatogory1st1.Items.FindByValue("A10").Selected = True
Line 226: End Sub
Even if I take out the databind I get the same error.
View 30 Replies
Dec 4, 2010
I have a user control that has some drop down lists. These DDLs are used by the user to filter their product search on my search page. A button is used to post the form and get the results of the product search. Those results are displayed on the results page, which also has the search filter user control on it. I'd like the results page to have the values of the DDLs be preserved from the search page. Example: user selected color of red and size of large on the search page. When the results page is displayed, I'd like the selected color to be red for the color DDL and the size to be large for the size DDL. How can I do this?
View 9 Replies
Aug 30, 2012
How to select next row of grid view on selected index change event on dropdownlist in gridview...
View 1 Replies
Feb 23, 2013
there is 3 drop down list control in in first dropdown load Districts when page load when we select or index changed in drop down1 items changed or loaded in drop down list2 according to the disrict.According to the item selected in drop downlist2 items changed or loaded in drop downlist3.
View 1 Replies
Mar 20, 2011
I have a simple query based on the index changed event of a dropdownlist in ASP.NET. The scenario is i have a dropdown list bearing a collection of items (say ONE, TWO, THREE, FOUR, FIVE) with the default value selected as 'ONE'. Now, when the client changes the index (say the client selects 'TWO'), a confirmation box is prompted asking 'Are you sure want to change the index?' with the buttons YES & NO. Now, the problem is, if YES button is clicked then its working fine as the index is changed. But when NO button is clicked in the confirmation box, I do not want the index to be changed to 'TWO' and it should be holding the previously selected value which was 'ONE'. How can I do this? For which event should I write the code, as I cant think of writing the code for the IndexChanged event because here the index change has already occured and since there is no 'IndexChanging' event in ASP.NET, where shall i write the code
View 7 Replies
Nov 26, 2010
I am binding months to a list 0-13 items default selection is 0 months duration, and years 0-8 items and 0 years is the default selection.As I know there are always going to be 12 months in a year, I could use selectedvalue or selectedindex without any perceived problem. Is there a reason why I should not use either/or of the two properties to select a list item?
View 2 Replies
Oct 12, 2010
i have 2 drop down list..first for city and second for state i want to fill the state list when the city is selected...m using the code
protected void Ddl_SelectedIndexChanged(object sender, EventArgs e)
{
String sqlQuery="select SM.StateId,StateName From StateMast SM,CityMast CM where CM.StateId=SM.StateId AND CM.CItyId='"+ Convert.ToInt16(DdlCity.SelectedValue.ToString())+"'";
DdlState.DataSource = cls.Select(sqlQuery);
DdlState.DataTextField = "StateName";
DdlState.DataValueField = "StateId";
}
but nothing is happing on selecting city..i have set the autopostback of city=true..select is a function which is returning data tablepublic DataTable Select(String sqlQuery)
{
con.Open();
SqlDataAdapter adapter = new SqlDataAdapter(sqlQuery,con);
DataTable table = new DataTable();
adapter.Fill(table);
con.Close();
return table;
}
View 2 Replies
Sep 13, 2010
Why I am unable to setfocus to my list box if the index > 820
My list box contains 2000 items and the index is not able to set the focus if the index > 820
View 6 Replies
Jul 12, 2012
I am using SelectedIndexChanged in the DropDowList and it works fine and populates a GridView based upon a site selection.
Is there there a way I can reset the SelectedIndexChanged so if user wants to refresh the GridView they would open the drop down list and click on the same selection again?
View 5 Replies
Jan 26, 2012
I am populating a DropDownList from a text file (class requirement).
The file is read in the Page_Load method and the DropDownList is populated then.
When I press the Submit button, throughout the process of the code it is supposed to get the SelectedIndex and use it to grab a value out of a List(Of ).
The thing is, nothing is ever passed through. I even setup an alert box and it passes right over the alert box and never even triggers it, but gives me an Index out of range error when it moves to the line right after the Alert.
Code:
Redacted to keep people from stealing classwork.
View 6 Replies