Combo box in form

Discussion in 'ASP .Net' started by Jason, Jun 11, 2010.

  1. Jason

    Jason Guest

    Hi,
    How do I rerefence my combobox in my VB code behind. It's sitting in an ASP
    formview.

    When it is outside the formview my code behind works like this:
    Private Sub cboCurrency_DataBound(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles cboCurrency.DataBound
    cboCurrency.SelectedIndex =
    cboCurrency.Items.IndexOf(cboCurrency.Items.FindByText("GBP"))
    End Sub

    When I put the combo inside the formview it says "cboCUrrency is not declared"

    Thanks for your help
    Regards,
    fidl
    Jason, Jun 11, 2010
    #1
    1. Advertising

  2. Hi fidl,

    When the server control is placed in some DataControl's template, the
    code-behind server event handler of the control should be bound with every
    instance of each DataControl's row instead of single one. In this function,
    we need to find the current instance of the ComboBox by the sender
    parameter and convert it to ComboBox type. In the same time, we have to
    bind every ComboBox's OnDataBound event explicitly in the HTML tag.

    For example, below code is to describe how to bind a DropDownList's
    DataBound event with a function and the DropDownList is placed in a
    GridView's template.

    .aspx file
    <asp:GridView ID="GridView1" runat="server"
    AutoGenerateColumns="False" DataKeyNames="ID"
    DataSourceID="SqlDataSource1">
    <Columns>
    <asp:BoundField DataField="ID" HeaderText="ID"
    InsertVisible="False" ReadOnly="True"
    SortExpression="ID" />
    <asp:TemplateField HeaderText="Field1"
    SortExpression="Field1">
    <EditItemTemplate>
    <asp:TextBox ID="TextBox1" runat="server" Text='<%#
    Bind("Field1") %>'></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%#
    Bind("Field1") %>'></asp:Label>
    <asp:DropDownList ID="DropDownList1" runat="server"
    DataSourceID="SqlDataSource1"
    DataTextField="ID" DataValueField="Field1"
    OnDataBound="DropDownList1_DataBound">
    </asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
    .aspx.vb file
    Protected Sub DropDownList1_DataBound(ByVal sender As Object, ByVal e
    As EventArgs) 'Handles DropDownList1.DataBound
    Dim ddl As DropDownList = CType(sender, DropDownList)
    ddl.SelectedIndex = 2
    End Sub

    --
    Sincerely,
    Zhi-Qiang Ni
    Microsoft Online Support
    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

    MSDN Managed Newsgroup support offering is for non-urgent issues where an
    initial response from the community or a Microsoft Support Engineer within
    2 business day is acceptable. Please note that each follow up response may
    take approximately 2 business days as the support professional working with
    you may need further investigation to reach the most efficient resolution.
    The offering is not appropriate for situations that require urgent,
    real-time or phone-based interactions. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
    ==================================================
    Zhi-Qiang Ni[MSFT], Jun 15, 2010
    #2
    1. Advertising

  3. Jason

    Jason Guest

    Thanks for your excellent reply Zhi-Quiang

    I'm still having troubles however. My Combo box is supposed to be a control
    parameter for the form's datasource.
    I think maybe this isn't possible because it tries to build the datasource
    before the control exists:

    <asp:UpdatePanel ID="UpdatePanel5" runat="server">
    <ContentTemplate>
    <asp:FormView ID="frmDashboard"
    runat="server" CssClass="cssForm" DataSourceID="SqlDataSourceDash"
    Style="height: 57px; width: 288px"
    DefaultMode="Edit" CellPadding="10">
    <EditItemTemplate>
    <div style="text-align: Center;
    font-weight: bold">
    Dashboard</div>
    <table style="text-align: right">
    <tr>
    <td class="styleForm">
    Currency:
    </td>
    <td class="styleForm">
    <asp:DropDownList
    ID="cboCurrency" runat="server" DataSourceID="SqlDataSourceCurrency"
    AutoPostBack="True"
    DataTextField="Currency" DataValueField="CurrencyID">
    </asp:DropDownList>
    </td>
    </tr>

    etc.....then.....


    <asp:SqlDataSource ID="SqlDataSourceCurrency"
    runat="server" ConnectionString="<%$ ConnectionStrings:pADSConnectionString
    %>"
    SelectCommand="SELECT [CurrencyID], [Currency] FROM
    [tlkpCurrency] ORDER BY [Currency]">
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSourceDash" runat="server"
    ConnectionString="<%$ ConnectionStrings:pADSConnectionString %>"

    SelectCommand="spDashboardWhereReviewUserIDCurrencyID"
    SelectCommandType="StoredProcedure">
    <SelectParameters>
    <asp:SessionParameter Name="UserID"
    SessionField="strUserID" Type="String" />
    <asp:ControlParameter
    ControlID="frmDashboard$cboCurrency" Name="CurrencyID"
    PropertyName="SelectedValue"
    Type="Int32" />
    </SelectParameters>
    </asp:SqlDataSource>

    The error I get says that it can't find Control frmDashboard$cboCurrency for
    the control source.



    "Zhi-Qiang Ni[MSFT]" wrote:

    > Hi fidl,
    >
    > When the server control is placed in some DataControl's template, the
    > code-behind server event handler of the control should be bound with every
    > instance of each DataControl's row instead of single one. In this function,
    > we need to find the current instance of the ComboBox by the sender
    > parameter and convert it to ComboBox type. In the same time, we have to
    > bind every ComboBox's OnDataBound event explicitly in the HTML tag.
    >
    > For example, below code is to describe how to bind a DropDownList's
    > DataBound event with a function and the DropDownList is placed in a
    > GridView's template.
    >
    > .aspx file
    > <asp:GridView ID="GridView1" runat="server"
    > AutoGenerateColumns="False" DataKeyNames="ID"
    > DataSourceID="SqlDataSource1">
    > <Columns>
    > <asp:BoundField DataField="ID" HeaderText="ID"
    > InsertVisible="False" ReadOnly="True"
    > SortExpression="ID" />
    > <asp:TemplateField HeaderText="Field1"
    > SortExpression="Field1">
    > <EditItemTemplate>
    > <asp:TextBox ID="TextBox1" runat="server" Text='<%#
    > Bind("Field1") %>'></asp:TextBox>
    > </EditItemTemplate>
    > <ItemTemplate>
    > <asp:Label ID="Label1" runat="server" Text='<%#
    > Bind("Field1") %>'></asp:Label>
    > <asp:DropDownList ID="DropDownList1" runat="server"
    > DataSourceID="SqlDataSource1"
    > DataTextField="ID" DataValueField="Field1"
    > OnDataBound="DropDownList1_DataBound">
    > </asp:DropDownList>
    > </ItemTemplate>
    > </asp:TemplateField>
    > </Columns>
    > </asp:GridView>
    > .aspx.vb file
    > Protected Sub DropDownList1_DataBound(ByVal sender As Object, ByVal e
    > As EventArgs) 'Handles DropDownList1.DataBound
    > Dim ddl As DropDownList = CType(sender, DropDownList)
    > ddl.SelectedIndex = 2
    > End Sub
    >
    > --
    > Sincerely,
    > Zhi-Qiang Ni
    > Microsoft Online Support
    > ==================================================
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
    >
    > MSDN Managed Newsgroup support offering is for non-urgent issues where an
    > initial response from the community or a Microsoft Support Engineer within
    > 2 business day is acceptable. Please note that each follow up response may
    > take approximately 2 business days as the support professional working with
    > you may need further investigation to reach the most efficient resolution.
    > The offering is not appropriate for situations that require urgent,
    > real-time or phone-based interactions. Issues of this nature are best
    > handled working with a dedicated Microsoft Support Engineer by contacting
    > Microsoft Customer Support Services (CSS) at
    > http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
    > ==================================================
    >
    > .
    >
    Jason, Jun 22, 2010
    #3
  4. Hi fidl,

    The ControlParameter's ControlID property should be the DropDownList's ID
    instead of its UniqueID. Please refer to my sample in this ASP.NET forum
    post to describe how to previewing the Main Data in DetailsView and editing
    the Detail Data in GridView by popuping the PopupControl.
    http://forums.asp.net/p/1334446/2693367.aspx#2693367 .

    Please let me know the test result.
    --
    Sincerely,
    Zhi-Qiang Ni
    Microsoft Online Support
    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

    MSDN Managed Newsgroup support offering is for non-urgent issues where an
    initial response from the community or a Microsoft Support Engineer within
    2 business day is acceptable. Please note that each follow up response may
    take approximately 2 business days as the support professional working with
    you may need further investigation to reach the most efficient resolution.
    The offering is not appropriate for situations that require urgent,
    real-time or phone-based interactions. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
    ==================================================
    Zhi-Qiang Ni[MSFT], Jun 28, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QW5nZWw=?=

    COMBO BOX INSTEAD OF DROP DOWN BOX

    =?Utf-8?B?QW5nZWw=?=, Jun 29, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    591
    Ken Cox [Microsoft MVP]
    Jun 29, 2004
  2. Adrian
    Replies:
    1
    Views:
    203
    Bullschmidt
    Feb 18, 2004
  3. dthmtlgod

    Fill Combo Box based on another Combo Box

    dthmtlgod, Nov 16, 2005, in forum: ASP General
    Replies:
    1
    Views:
    562
    Bob Barrows [MVP]
    Nov 16, 2005
  4. Mike

    Combo Box and text box

    Mike, Sep 24, 2003, in forum: Javascript
    Replies:
    2
    Views:
    139
  5. SM
    Replies:
    2
    Views:
    347
Loading...

Share This Page