我从数据库里读数据的时候,从productlist表中读出来数据后,然后根据pro_id查询数据时却读不出来,为什么啊?
<%@ import namespace="system.data"%>
<%@ import namespace="system.data.sqlclient"%>
<script language="vb" runat="server">
sub page_load(sender as object,e as eventargs)
    If Not Page.IsPostBack Then
            Call myDataBind()
        End If
end sub
sub myDataBind()
    dim conn as new sqlconnection("server=(local);database=bai;uid=sa;pwd=997mb")
    dim cmd as new sqlcommand("select * from productlist",conn)
    conn.open()
    dim dr as sqldatareader=cmd.executereader()
    myDataList.datasource=dr
    myDataList.databind()
    conn.close()
    Dim intpro_id As Integer
    If Request.QueryString("pro_id")<>"" And (Not Page.IsPostBack) Then
            intpro_id=Request.QueryString("pro_id")                '获取传递过来的pro_id
            Session("pro_id")=intpro_id                            '保存到Session中
        Else
            '其它情况下从Session中获取
            intpro_id=Session("pro_id")            
        End If
    
end sub
</script>
<html>
<HEAD>
<TITLE>隆泰守和摄影器材行</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">
</HEAD>
<body leftmargin="5" topmargin="5">
<form runat="server">
    <table width="300" border="1" cellspacing="0" cellpadding="0" bordercolor="#D1DAF1" bgcolor="#D1DAF1" class="text" height="20" align="left">
        <tr align="left"> 
            <td>
                <asp:DataList id="myDataList" runat="server" RepeatDirection="Vertical" RepeatColumns="1"  style="POSITION: absolute">
                    <ItemTemplate>
                        <asp:HyperLink Text='<%# Container.DataItem("productname") %>' NavigateUrl='<%# "index.aspx?pro_id=" & Container.DataItem("pro_id") %>' runat="server"/>
                    </ItemTemplate>
                    <SeparatorTemplate>
                        &nbsp;&nbsp;
                    </SeparatorTemplate>
                </asp:DataList>            </td>
        </tr>
    </table>
</form>
</body>
</html>

解决方案 »

  1.   

    尽量少在页面里用<%%>标记,尽量写到代码里,在 itemdatabind事件里。
    使用DataBinder.getpropertyvalue(e.item.itemdata,"列名")
    的方式
      

  2.   

    <%@ import namespace="system.data"%>
    <%@ import namespace="system.data.sqlclient"%>
    <script language="vb" runat="server">
    sub page_load(sender as object,e as eventargs)
        If Not Page.IsPostBack Then
                Call myDataBind()
            End If
    intpro_id=Session("pro_id") //加上这一句           
    end sub
    sub myDataBind()
        dim conn as new sqlconnection("server=(local);database=bai;uid=sa;pwd=997mb")
        dim cmd as new sqlcommand("select * from productlist",conn)
        conn.open()
        dim dr as sqldatareader=cmd.executereader()
        myDataList.datasource=dr
        myDataList.databind()
        conn.close()
        Dim intpro_id As Integer //声明为public
        If Request.QueryString("pro_id")<>"" And (Not Page.IsPostBack) Then
                intpro_id=Request.QueryString("pro_id")                '获取传递过来的pro_id
                Session("pro_id")=intpro_id                            '保存到Session中
            Else
                '其它情况下从Session中获取
                intpro_id=Session("pro_id")            
            End If
        
    end sub
    </script>
    <html>
    <HEAD>
    <TITLE>隆泰守和摄影器材行</TITLE>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">
    </HEAD>
    <body leftmargin="5" topmargin="5">
    <form runat="server">
        <table width="300" border="1" cellspacing="0" cellpadding="0" bordercolor="#D1DAF1" bgcolor="#D1DAF1" class="text" height="20" align="left">
            <tr align="left"> 
                <td>
                    <asp:DataList id="myDataList" runat="server" RepeatDirection="Vertical" RepeatColumns="1"  style="POSITION: absolute">
                        <ItemTemplate>
                            <asp:HyperLink Text='<%# Container.DataItem("productname") %>' NavigateUrl='<%# "index.aspx?pro_id=" & Container.DataItem("pro_id") %>' runat="server"/>
                        </ItemTemplate>
                        <SeparatorTemplate>
                            &nbsp;&nbsp;
                        </SeparatorTemplate>
                    </asp:DataList>            </td>
            </tr>
        </table>
    </form>
    </body>
    </html>