work on C# asp.net vs05 .I have a ddl,ddl contain five element
ddl values are
1
2
3
4
5
From the database i get 1,Need to show selected value is 1 .How to show ddl Selected value is 1.
Try to set ddl.SelectedValue to the value of the item you want to select. If that doesn't work try this (where you replace 0 with the index of the element you need to select):
ddl.Items[0].Selected = true;
What about .SelectedValue property?
MSDN, DropDownList class members
Related
I have a listbox in VB.NET that allows users to select multiple categories. I need to put these categories into a database and need the index value from the listbox of the categories as the database works off IDs. SelectedItems (with an s) does not work in the net application.
I have tried the following code:
For Each category As ListItem In CategoryListBox.Items
If category.Selected Then
Dim courseCategory As New CourseCategory
courseCategory.CourseID = course.ID
courseCategory.CategoryID = CategoryListBox.SelectedIndex
Using courseCategoryEntities As New Eng_NWDTrainingWebsiteEntities
courseCategoryEntities.CourseCategories.Add(courseCategory)
courseCategoryEntities.SaveChanges()
End Using
End If
Next
When iterating through the loop the code that is:
courseCategory.CategoryID = CategoryListBox.SelectedIndex
works correctly the first time around.
On the second iteration of the loop, it goes to the next selected item however returns the index for the first selected value. How do I return the values of the other indexes selected?
It depends on what ID you need to pass to your database. If it is truly the index of the ListItem in the ListBox, then you would use:
courseCategory.CategoryID = CategoryListBox.Items.IndexOf(category);
That may not be the best approach however. Maybe the order of the ListItems changes, messing up your indexes. You probably want to store the actual CategoryID on each ListItem. The Value property works well for that. You set the column you want as the DataValueField like so:
<asp:ListBox ID="CategoryListBox" runat="server"
DataValueField="CategoryID "></asp:ListBox>
So as you are looping through each ListItem in the ListBox and checking if it is selected, just use it's Value property.
For Each category As ListItem In CategoryListBox.Items
If category.Selected Then
Dim courseCategory As New CourseCategory
courseCategory.CourseID = course.ID
courseCategory.CategoryID = category.Value;
Using courseCategoryEntities As New Eng_NWDTrainingWebsiteEntities
courseCategoryEntities.CourseCategories.Add(courseCategory)
courseCategoryEntities.SaveChanges()
End Using
End If
Next
Fixed the code by deselecting the item in the listbox after it was successfully saved.
End Using
category.Selected = False
End If
This solved my problem.
I have a DropDownList that pulls values from a table in sql server. I want the dropdownlist to populate the list of values based on a selection by the user but also display the contents of the rest of my table in case they need to change something. For ex:
ddl1 has values of: 1, 2, 3, 4, 5 user selects 5, so the value displayed in ddl2 is five, but if you exand the ddl2 you will also see the values of one, two, three, four...
This is the code in my load event...
If Not IsPostBack Then
result = dal.dbConnect(DAL.dbType.SqlServer, ConfigurationManager.AppSetting("SQLServerConnection"))
If result = "Successful" Then
Try
dt = dal.ExecuteSelectStoredProc(DAL.dbType.SqlServer, "StoredProc1", "#obj1", DropDownList1.Text)
DropDownList2.DataSource = dt
DropDownList2.DataTextField = dt.Columns.Item(1).ToString
DropDownList2.DataBind()
Catch ex As Exception
End Try
End If
End If
Stored Procedure...
USE [DB1]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[StoredProc1]
#obj1 nvarchar(4)
AS
BEGIN
SET NOCOUNT ON;
SELECT [Obj1], [obj2]
FROM [DB1].[dbo].[Code]
WHERE [obj1] = #obj1
END
You need to set the AppendDataBoundItems property to true to preserve the existing values in the DDL:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.appenddatabounditems.aspx
If you're looking to implement a cascading effect with the DDLs, take a look at this:
http://www.codersbarn.com/post/2007/07/21/Code-Snippet-Cascading-DropDownLists.aspx
In your SelectedIndex Change event method, write the following:
ddl2.SelectedValue = ddl1.SelectedValue;
I'm not clear why you want to have two drop-down lists, or what is in each.
If you simply want a dropdown list with all the values from a table, but with the value showing being a value previously selected, just say "ddl.SetSelectedValue(somevalue)".
I have been asked question on this scenario.There are 2 dropdownlist populating same data from same table one at top and other at bottom.when Item in first dropdownlist get selected,same item is shown in second dropdownlist and second item get selected,same item is selected in first.That means it is continuous loop.How to handle this situation. Don't want to use query
select name from table where id = ddl1.selecteditem.value and execute it.
On selectedindex function of dropdownlist1,he wrote like this
ddl2.selectedItem.value = ddl.selectedItem.value
and second list
ddl.selectedItem.value = ddl2.selectedItem.value
Does it possible to select the value from these 2 statement?I done like this,but not working.
In order to get the same value in both dropdownlists you need to do this in your dropdownlist1_selectedindexchanged:
DropDownList2.Items.FindByValue(DropDownList1.SelectedValue).Selected = true;
The same in dropdownlist2_selectedindexchanged:
DropDownList1.Items.FindByValue(DropDownList2.SelectedValue).Selected = true;
This should keep the same value in both dropdownlists whenever they have been modified.
I have an ASP.NET site (either .NET 2.0 or 3.5), and I want to get the selected item of two drop down boxes (call them a and b). B is updated with appropriate values depending on what is selected for A.
However, the following code:
string val = dd1.SelectedItem.Text;
Returns a value of "not selected". Why is this?
Thanks
The only way this could happen is if you have "not selected" set as either the Text property of your selected item, or if Text is null then the Value property will be returned.
From MSDN's documentation on ListItem (which is what SelectedItem inherits from):
If the Text property contains null,
the get accessor returns the value of
the Value property. If the Value
property, in turn, contains null,
String.Empty is returned.
I have a GridView and I want to refresh a row (not the whole grid) when it gets selected. I tried to change Text property for each of GridView.SelectedRow.Cells when Grid selected index changes. It seems to work for DataField, but not for TemplateField. For TemplateField I got strange results - the value for selected row changes properly, but when I select another row, the value of TemplateField for previously selected row becomes blank. Brief illustration :
1. Nothing selected
--------------------------
id template_field
--------------------------
1 value_1
2 value_2
2. First record selected
--------------------------
id template_field
--------------------------
1 updated_value_1
2 value_2
3. Second record selected
--------------------------
id template_field
--------------------------
1 [blank!]
2 updated_value_2
Eventually, I end up with blank template_field for each record except selected. What is the proper way to change the text of TemplateField ?
Try adding a TextBox control to your template, and change it instead of the cell contents. You should be able to get at it via something like this:
TextBox tb = GridView.SelectedRow.Cells[0].Controls[0] as TextBox;
if(tb != null)
tb.Text = newValue;