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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nzkks
    New Member
    • Mar 2009
    • 10

    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(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:

    Code:
    <asp:FormView ID="FormView1" runat="server" DataKeyNames="ImperialMeasureID" DataSourceID="ImperialMeasureDataSource" DefaultMode="Insert">
    <InsertItemTemplate>
    <asp:TextBox ID="tbProductSizeValue" runat="server" Text='<%# Bind("ProductSizeValue") %>' Columns="6" MaxLength="6"></asp:TextBox>
    <asp:DropDownList ID="ddlProductSizeUnit" runat="server" AppendDataBoundItems="True" DataTextField="UnitValue" DataValueField="UnitValue">
        <asp:ListItem Text="---" Value="" />
    </asp:DropDownList>
    Part of code behind:

    Code:
    Dim ddl1 As DropDownList = CType(FormView1.FindControl("ddlProductSizeUnit"), DropDownList)
    Dim tb1 As TextBox = CType(FormView1.FindControl("tbProductSizeValue"), TextBox)
    If ddl1.SelectedValue = "" AndAlso tb1.Text.Length &gt; 0 Then
       e.Values("ProductSizeUnit") = "oz"
    Else
       e.Values("ProductSizeUnit") = ddl1.SelectedValue
    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 Files
  • nzkks
    New Member
    • Mar 2009
    • 10

    #2
    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".
    Code:
    Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
            Dim ddl1 As DropDownList = CType(FormView1.FindControl("ddlProductSizeUnit"), DropDownList)
            ddl1.SelectedItem.Text = ddl1.Items.FindByText("oz").ToString
            Dim ddl2 As DropDownList = CType(FormView1.FindControl("ddlMaxCapacityUnit"), DropDownList)
            ddl2.SelectedItem.Text = ddl2.Items.FindByText("oz").ToString
            Dim ddl3 As DropDownList = CType(FormView1.FindControl("ddlItemDiameterUnit"), DropDownList)
            ddl3.SelectedItem.Text = ddl3.Items.FindByText("in").ToString
    rest of 17 ddl code goes here.
    What mistake I am doing? Please help me.

    Comment

    • maliksleo
      New Member
      • Feb 2009
      • 115

      #3
      as for my knowledge is concern you have to add the following code into your page load event as this
      Code:
      If Not IsPostBack Then
      dropdown1.databind()
      dropdown1.text = "oz"
      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)

      maliksleo

      Comment

      • nzkks
        New Member
        • Mar 2009
        • 10

        #4
        Thanks. But still not working.

        Comment

        • maliksleo
          New Member
          • Feb 2009
          • 115

          #5
          Originally posted by nzkks
          Thanks. But still not working.
          make sure that you placed all the code in side the code
          Code:
          if not ispostback then
          'your code
          end if
          otherwise it will not work properly.

          maliksleo

          Comment

          • muruganad
            New Member
            • Oct 2009
            • 1

            #6
            Exception: How to Set the default value of a drop down list.

            Here is the solution...





            Thanks!

            Murugan Andezuthu Dharmaratnam

            Comment

            Working...