473,837 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to set & show default value in dropdownlist from databound items (not static)

10 New Member
Hi I am using these: ASP.Net 2.0, VB.Net, Visual Studio 2005, SQL Server 2005, Formview controls

In a ASP.Net form I have 20 textboxes and 20 dropdownlists(d dl). All ddl(s) are databound and get the data from a single objectdatasourc e. All textboxes and ddl(s) support null values. Textboxes are for entering numbers and ddl(s) are for selecting the unit (mm, cm, in, m3, oz, qt like these.).

I am using the below code to achieve two things.(For each textbox and ddl have their own corresponding codes like below.)

1. Allowing the selection of ddl item also if it is not selected too, a default unit item will be inserted in the database.
2. Only if textbox has value then the default unit item will be inserted in the database.

It is working fine.

Part of form below:

Expand|Select|Wrap|Line Numbers
  1. <asp:FormView ID="FormView1" runat="server" DataKeyNames="ImperialMeasureID" DataSourceID="ImperialMeasureDataSource" DefaultMode="Insert">
  2. <InsertItemTemplate>
  3. <asp:TextBox ID="tbProductSizeValue" runat="server" Text='<%# Bind("ProductSizeValue") %>' Columns="6" MaxLength="6"></asp:TextBox>
  4. <asp:DropDownList ID="ddlProductSizeUnit" runat="server" AppendDataBoundItems="True" DataTextField="UnitValue" DataValueField="UnitValue">
  5.     <asp:ListItem Text="---" Value="" />
  6. </asp:DropDownList>
Part of code behind:

Expand|Select|Wrap|Line Numbers
  1. Dim ddl1 As DropDownList = CType(FormView1.FindControl("ddlProductSizeUnit"), DropDownList)
  2. Dim tb1 As TextBox = CType(FormView1.FindControl("tbProductSizeValue"), TextBox)
  3. If ddl1.SelectedValue = "" AndAlso tb1.Text.Length &gt; 0 Then
  4.    e.Values("ProductSizeUnit") = "oz"
  5. Else
  6.    e.Values("ProductSizeUnit") = ddl1.SelectedValue
  7. End If
But what I want to change,

* Visually the form should show the preferred ddl default item. So that the user can see and if they need to change.
* I don't want to hardcode the preferred unit item (like oz) in the code behind. Instead the ddl item itself should be refered.

How to achieve the above 2 things? (Attached image will show what I am expecting)

Please help me.
Attached Images
File Type: jpg dropdownlist.jpg (10.9 KB, 1206 views)
Apr 13 '09 #1
5 26643
10 New Member
OK, I tried this way and worked for 2 instances. But 3rd ddl onwards are not working and giving the error "Object reference not set to an instance of an object".
Expand|Select|Wrap|Line Numbers
  1. Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
  2.         Dim ddl1 As DropDownList = CType(FormView1.FindControl("ddlProductSizeUnit"), DropDownList)
  3.         ddl1.SelectedItem.Text = ddl1.Items.FindByText("oz").ToString
  4.         Dim ddl2 As DropDownList = CType(FormView1.FindControl("ddlMaxCapacityUnit"), DropDownList)
  5.         ddl2.SelectedItem.Text = ddl2.Items.FindByText("oz").ToString
  6.         Dim ddl3 As DropDownList = CType(FormView1.FindControl("ddlItemDiameterUnit"), DropDownList)
  7.         ddl3.SelectedItem.Text = ddl3.Items.FindByText("in").ToString
  8. rest of 17 ddl code goes here.
What mistake I am doing? Please help me.
Apr 13 '09 #2
115 New Member
as for my knowledge is concern you have to add the following code into your page load event as this
Expand|Select|Wrap|Line Numbers
  1. If Not IsPostBack Then
  2. dropdown1.databind()
  3. dropdown1.text = "oz"
  4. end if
in the above case it will bind your ddl with the datasourse and then call the value from that data like "oz" repeat this for your remaining ddl(s)

Apr 13 '09 #3
10 New Member
Thanks. But still not working.
Apr 13 '09 #4
115 New Member
make sure that you placed all the code in side the code
Expand|Select|Wrap|Line Numbers
  1. if not ispostback then
  2. 'your code
  3. end if
otherwise it will not work properly.

Apr 13 '09 #5
1 New Member
Exception: How to Set the default value of a drop down list.

Here is the solution...



Murugan Andezuthu Dharmaratnam

Oct 28 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Nir | last post by:
Hello all , I have a very strange problem with DropDownList . I am using this Code for select items in Drop Down : Dim Item As New ListItem() Item = cmb.Items.FindByValue(Data) cmb.SelectedIndex = cmb.Items.IndexOf(Item) Item = Nothing
by: huzz | last post by:
How do i make a dropdownlist selected value based on the value i retrive from the database. Basically i have an edit page and like to display the default value in a dropdown list from the database. for example: if the ddl_value is 2 from the database i want the second list item to be selected by default.
by: Harold | last post by:
I have read the following article http://www.4guysfromrolla.com/webtech/073101-1.shtml. I added the empty selection as the first option. When I click submit without choosing anything it does not return my empty string but the 2nd value in listbox. When the page reloads it selects the 2nd option by default even thou my Empty field is in the list. What do I have to do to get the empty field and databound data to act as one?
by: John_H | last post by:
Re: ASP.NET 2.0 I would like suggestions or code examples on how to collect a variable length list of input data (item# & item quantity specifically). I thought that I could accomplish this using a GridView that has ViewState enabled, an ObjectDataSource to process the submitted list, textboxes for getting new item data and an add button. Does this approach sound feasible or are there better alternatives? My problem is that I don't...
by: IMB | last post by:
I'm using VS2005 and am working with a ASP.NET 2.0 web site and I'm coding in VB.NET. I understand that I can add an item to a databound dropdownlist and make it the default selection using the following code: MyDropDownList.Items.Insert(0, "My default item") MyDropDownList.SelectedIndex = 0 The problem that I am experiencing is *where* to put the above code! If I
by: BigAl.NZ | last post by:
Hi Guys, I am trying to write/copy some code that uses events with a GPS. Everytime the GPS position updates the event fires. The GPS code is from a SDK Library that I got called GPS Tools from www.franson.com For some reason my code below doesnt work - the GpsFixEvent never seems to "fire" as it were.
by: KerryL | last post by:
How do I update the value of a databound column within my datagrid from within the SelectedIndexChanged event of a dropdownlist, also found within my datagrid? I believe I need to determine the row I'm on but I don't know how to get that.
by: ssims | last post by:
For some reason my databound DropDownList will not populate. I've tried a variety of things...here's the latest. - I pulled a DropDownList on to the page. - Then I pulled a SqlDataAdapter and set the select query to "SELECT * FROM Items". - I chose Generate DataSet... from the SqlDataAdapter's properties. - I set the DataSource, DataMember, DataTextField, and DataValueField properties in the Properties window. - Then I put...
by: dizzy77 | last post by:
I have a dropdownlist databound to table column with string values "n" i "y". <asp:DropDownList ID="ddlPrivate" runat="server" SelectedValue='<%# Bind("cPrivate") %>'> <asp:ListItem Value="n">No</asp:ListItem> <asp:ListItem Value="y">Yes</asp:ListItem> </asp:DropDownList> How to handle situation when value is not "n" or "y" to avoid exception. Is it possible to make dropdownlist select some default value...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.