I have an ordering page where the quantity of each product is limited to
the maximum number allowed. The quantity is entered through a dynamic drop
down list, with the data source is defined by calling a function returning
the data view. The product list is bound to a repeater.
I tried using findcontrol to retrieve the selected value, but it returns
0. How can I return the selected drop down list value?
Please see below is the aspx code.
<%@ Page Language="VB" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ import Namespace="System.Data.Common" %>
<script runat="server">
dim cnx as SQLConnection
dim objSQLCommand as SqlCommand
dim ObjDataReader As SqlDataReader
cnx = new SqlConnection("Server=LocalHost;uid=sa;pwd=;Database=ite")
sub Page_Load(Src As Object, E As EventArgs)
dim intProjectID as integer
intProjectID = 1
objSQLCommand = New SqlCommand("usp_select_Product_by_ProjectID", cnx)
objSQLCommand.CommandType = CommandType.StoredProcedure
objSQLCommand.Parameters.Add(New SqlParameter("@ProjectID",
SqlDbType.int))
objSQLCommand.Parameters("@ProjectID").Value = intProjectID
cnx.Open()
myRepeater.DataSource = objSQLCommand.ExecuteReader()
myRepeater.DataBind()
cnx.Close()
end sub
sub btnInsert_Click (Sender As Object, E as EventArgs)
objSqlCommand = New SqlCommand("usp_insert_SalesOrderMain",cnx)
objSqlCommand.CommandType = CommandType.StoredProcedure
dim objParameter As SqlParameter
dim objOutputParameter As SqlParameter
'Build parameters for input/output procedure
'
'
'
'Last input parameter
objParameter = New SqlParameter("@CallStartDate",SqlDbType.DateTime)
objSqlCommand.Parameters.Add(objParameter)
objParameter.Direction = ParameterDirection.Input
objParameter.Value = txtCallStartDate.text
'Output parameter, SalesOrderID
objOutputParameter = New SqlParameter("@SalesOrderID",SqlDbType.Int)
objSqlCommand.Parameters.Add(objOutputParameter)
objOutputParameter.Direction = ParameterDirection.Output
cnx.open()
objDataReader = objSqlCommand.ExecuteReader()
dim intSalesOrderID as Integer
intSalesOrderID = objOutputParameter.Value
cnx.Close()
'Loop through repeater
Dim I As Long
Dim CurrentDropDownList As DropDownList
Dim CurrentLabel As Label
objSqlCommand = New SqlCommand("usp_insert_SalesOrderItem",cnx)
objSqlCommand.CommandType = CommandType.StoredProcedure
'Loop
for I=0 To myRepeater.Items.Count -1
CurrentLabel = myRepeater.Items(I).FindControl("lblProductID")
CurrentDropDownList = myRepeater.Items(I).FindControl("ddl")
objParameter = New SqlParameter("@Quantity",SqlDbType.Int)
objSqlCommand.Parameters.Add(objParameter)
objParameter.Direction = ParameterDirection.Input
'CurrentDropDownList.SelectedItem.Text returns 0
objParameter.Value = CurrentDropDownList.SelectedItem.Text
objParameter = New SqlParameter("@SalesOrderID",SqlDbType.Int)
objSqlCommand.Parameters.Add(objParameter)
objParameter.Direction = ParameterDirection.Input
objParameter.Value = intSalesOrderID
objParameter = New SqlParameter("@ProductID",SqlDbType.Int)
objSqlCommand.Parameters.Add(objParameter)
objParameter.Direction = ParameterDirection.Input
objParameter.Value = cint(CurrentLabel.Text)
cnx.open()
dim ObjDataReader2 As SqlDataReader
objDataReader2 = objSqlCommand.ExecuteReader()
cnx.Close()
next
end sub
function CreateDataSource(maxQuantity as integer) As ICollection
Dim dt As New DataTable()
Dim dr As DataRow
dt.Columns.Add(New DataColumn("Quantity", GetType(Int32)))
Dim i As Integer
For i = 0 To maxQuantity
dr = dt.NewRow()
dr(0) = i
dt.Rows.Add(dr)
Next i
Dim dv As New DataView(dt)
Return dv
end function
</script>
<html>
<head>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp:Repeater id="myRepeater" runat="server">
<HeaderTemplate>
<table border="1" cellspacing="0" cellpadding="5">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label id="lblProductID" runat="server"
text='<%# Container.DataItem("ProductID") %>'></asp:Label>
</td>
<td>
<%# Container.DataItem("ProductDescription") %
></td>
<td>
<%# Container.DataItem("Price") %>
</td>
<td>
<asp:DropDownList id="ddl" datasource='<%#
CreateDataSource(Container.DataItem("maxQuantity")) %>'
DataValueField= "Quantity" DataValueText= "Quantity"
runat="server"></asp:DropDownList>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<p>
<asp:Button id="btnInsert" onclick="btnInsert_Click"
runat="server" Text="Button"></asp:Button>
</form>
</body>
</html>