468,766 Members | 1,379 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,766 developers. It's quick & easy.

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

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(ddl). All ddl(s) are databound and get the data from a single objectdatasource. 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, 1150 views)
Apr 13 '09 #1
5 26309
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 100+
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
Thanks. But still not working.
Apr 13 '09 #4
115 100+
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
Exception: How to Set the default value of a drop down list.

Here is the solution...



Murugan Andezuthu Dharmaratnam

Oct 28 '09 #6

Post your reply

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

Similar topics

reply views Thread by Nir | last post: by
2 posts views Thread by huzz | last post: by
reply views Thread by BigAl.NZ | last post: by
3 posts views Thread by ssims | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.