0

早上堆栈溢出,

我有一个中继器,在我的 aspx 页面中有以下代码;

<asp:Repeater ID="Contactinforepeater" runat="server">
<HeaderTemplate>
<h1>Contact Information</h1>
</HeaderTemplate>

<ItemTemplate>
<table width="50%">
<tr>
<td colspan="2"><%#Container.DataItem("position")%></td>
</tr>
<tr>
<td>Name:</td>
<td><%#Container.DataItem("surname")%></td>
</tr>
<tr>
<td>Telephone:</td>
<td><%#Container.DataItem("telephone")%></td>
</tr>
<tr>
<td>Fax:</td>
<td><%#Container.DataItem("fax")%></td>
</tr>
<tr>
<td>Email:</td>
<td><%#Container.DataItem("email")%></td>
</tr>
</table>
</ItemTemplate>

<SeparatorTemplate>
<br /><hr /><br />
</SeparatorTemplate>

</asp:Repeater>

然后我在我的 aspx.vb 中有这段代码来获取数据;

If did = 0 Then
            s = "sql works on db server"
            x = New SqlCommand(s, c)
            x.Parameters.Add("@contentid", Data.SqlDbType.Int)
            x.Parameters("@contentid").Value = contentid
            c.Open()
            r = x.ExecuteReader
            r.Read()
            Contactinforepeater.DataSource = r
            Contactinforepeater.DataBind()
        End If
        c.Close()
        r.Close()

        If Not did = 0 Then
            s = "sql works on db server"
            x = New SqlCommand(s, c)
            x.Parameters.Add("@contentid", SqlDbType.Int)
            x.Parameters("@contentid").Value = contentid
            x.Parameters.Add("@did", SqlDbType.Int)
            x.Parameters("@did").Value = did
            c.Open()
            r = x.ExecuteReader
            r.Read()
            Contactinforepeater.DataSource = r
            Contactinforepeater.DataBind()
        End If
        r.Close()
        c.Close()

如果“did”是或不是“0”,我仍然没有数据输出到页面。我只是从标题模板中获取“联系信息”h1 标题。

我已经在 sqlsms 中测试了 s 的值,它工作正常。职位、姓氏、电话、传真、电子邮件都存在于数据库中。我正在检查的特定页面存在,并附有一组联系信息。

我哪里错了?

谢谢!

附言。我的语法看起来正确吗?

pps。我也对达到相同结果的不同方式持开放态度。我通过 sqldatasource 进行了尝试,但在使用变量作为参数时遇到了问题(没有选择它们的选项,只有控件、查询字符串等)

4

1 回答 1

0

解决方案是将代码隐藏块更改为;

 If r.HasRows Then
                Contactinforepeater.DataSource = r
                Contactinforepeater.DataBind()
            End If

干杯

于 2010-04-15T07:54:35.623 回答