一点背景。还有一个主页。这是ASPX代码
<asp:WizardStep ID="WizardStepOrder" runat="server" StepType="Step">
<div>
<p>
Please review the items below and enter the quantity of each item for which you are requesting credit.
</p>
<asp:Label ID="InvoiceItemsInstructionsLabel" runat="server" />
</div>
<div style="padding: 0;">
<asp:GridView ID="InvoiceItemsGridView" runat="server" BorderStyle="None" AutoGenerateColumns="False" Width="100%">
<Columns>
<asp:BoundField DataField="Make" HeaderText="Make" />
<asp:BoundField DataField="PartNumber" HeaderText="Part Number" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:BoundField DataField="Ordered" HeaderText="Ordered" />
<asp:BoundField DataField="Shipped" HeaderText="Shipped" />
<asp:TemplateField HeaderText="Credit Code">
<ItemTemplate>
<asp:DropDownList ID="ddlCreditCode" runat="server" AutoPostBack="true">
<asp:ListItem Text="--Select Value--" Value=""></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
也是我的代码背后的vb
Dim ddlCreditCode As DropDownList = DirectCast(Me.WizardStepOrder.FindControl("ddlCreditCode"), DropDownList)
Dim sql As String
Dim cnn As SqlConnection
Dim cmd As SqlCommand
sql = String.Format("select CreditCode, CreditCodeDescription from myDatabase..CreditCodes")
cnn = DBConnection.GetNamedConnection(NamedConnection.myDatabase)
cmd = New SqlCommand(sql, cnn)
Try
cnn.Open()
ddlCreditCode.DataSource = cmd.ExecuteReader()
ddlCreditCode.DataTextField = "CreditCodeDescription"
ddlCreditCode.DataValueField = "CreditCode"
ddlCreditCode.DataBind()
Catch ex As Exception
Throw ex
Finally
cnn.Close()
cnn.Dispose()
End Try
ddlCreditCode一无所获。为什么?我试图用我的数据库中的值填充我的asp页面上的下拉列表。但为了做到这一点,我需要能够找到控制权。我读到一个问题,因为有一个母版页,我必须使用contentplaceholder并对其使用findcontrol,然后在该占位符上使用findcontrol,但这也不起作用。我认为它与itemtemplate或gridview有关。我是否必须遍历gridview或其他东西?
答案 0 :(得分:0)
你不这样做。改变你的代码
Try
//prevent issue on connection already open for some reason
if cnn.state=Closed then cnn.open
//declare a sqldataadpter
dim da as SqlDataAdapter(cmd)
//declare Dataset
dim ds as new Dataset
//fill dataset
da.Fill(ds)
ddlCreditCode.DataSource =ds.table(0)
ddlCreditCode.DataTextField = "CreditCodeDescription"
ddlCreditCode.DataValueField = "CreditCode"
ddlCreditCode.DataBind()
Catch ex As Exception
Throw ex
Finally
cnn.Close()
cnn.Dispose()
另外我个人邀请使用o使用使用声明,注意破坏引用主题:)
如果有效,请将答案标记为已回答
答案 1 :(得分:0)
我无法从您的代码中判断ddlCreditCode
是WizardStepOrder
的孩子还是兄弟姐妹(没有结束标记)。如果它不是直接的孩子,Me.WizardStepOrder.FindControl()
将找不到它。每MSDN:
仅当控件直接包含在指定容器中时,此方法才会找到控件;也就是说,该方法不会在控件内的控件层次结构中进行搜索。
答案 2 :(得分:0)
您需要使用RowDataBound事件来填充GridView中的控件
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If (e.Row.RowType = DataControlRowType.DataRow) Then
'Find the DropDownList in the Row
Dim ddlCreditCode As DropDownList = CType(e.Row.FindControl("ddlCreditCode"), DropDownList)
ddlCreditCode.DataSource =ds.table(0)
ddlCreditCode.DataTextField = "CreditCodeDescription"
ddlCreditCode.DataValueField = "CreditCode"
ddlCreditCode.DataBind()
End If
End Sub