By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,369 Members | 1,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,369 IT Pros & Developers. It's quick & easy.

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

P: 10
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, 1094 views)
Apr 13 '09 #1
Share this Question
Share on Google+
5 Replies


P: 10
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.
  9.  
  10.  
What mistake I am doing? Please help me.
Apr 13 '09 #2

maliksleo
100+
P: 115
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
  5.  
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)

maliksleo
Apr 13 '09 #3

P: 10
Thanks. But still not working.
Apr 13 '09 #4

maliksleo
100+
P: 115
@nzkks
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
  4.  
otherwise it will not work properly.

maliksleo
Apr 13 '09 #5

P: 1
Exception: How to Set the default value of a drop down list.

Here is the solution...

http://muruganad.com/ASP.NET/ASP-.NE...ult-Value.html



Thanks!

Murugan Andezuthu Dharmaratnam

http://muruganad.com
Oct 28 '09 #6

Post your reply

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