Combo box in form

  • Thread starter Thread starter Jason
  • Start date Start date
J

Jason

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
 
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
==================================================
 
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.
 
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
==================================================
 
Back
Top