0

我正在尝试在我的页面上填充列表视图。我使用代码从 sql 中读取数据:

string cxnstr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(cxnstr);
        ds = new DataSet("ds");
        conn.Open();

        SqlDataAdapter da = new SqlDataAdapter("select d.[Title],d.[Link],d.[Description],s.[Title],s.[Link],s.[Description] from DomainLinks d,SupplierLinks s where s.SuppRowID = " + SuppRowID + " or d.SuppRowID = " + SuppRowID, conn);
            da.Fill(ds);


        if (ds.Tables.Count > 0)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {

                lstView.DataSource = ds;

                lstView.DataBind();

            }
            else
            {

                lblError.Text = "There are no links for that type";
            }
        }

进入列表视图:

<asp:ListView ID="lstView" runat="server">
    <ItemTemplate>
        <asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' />
            <br />
        <a href='<%#Eval("Link") %>' runat="server"><asp:Label ID="LinkLabel" runat="server" Text='<%# Eval("Link") %>' /></a>
            <br />
        <asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' />
            <br />
</asp:ListView>

效果很好。但我的问题是其他记录呢?我不知道如何在列表视图中添加更多记录,而不是添加:

<asp:Label ID="Title1Label" runat="server" Text='<%# Eval("Title1") %>' />
            <br />
        <a href='<%#Eval("Link1") %>' runat="server"><asp:Label ID="Link1Label" runat="server" Text='<%# Eval("Link1") %>' /></a>
            <br />
        <asp:Label ID="Description1Label" runat="server" Text='<%# Eval("Description1") %>' />
            <br />

和递增。但这是硬编码的。如何在页面加载时执行此操作?我应该继续使用列表视图吗?切换控制?

编辑

行。这是域链接和供应链接的表结构:

CREATE TABLE [dbo].[SupplierLinks](
[RowID] [int] IDENTITY(1,1) NOT NULL,
[SuppRowID] [int] NOT NULL,
[Title] [varchar](250) NULL,
[Link] [varchar](max) NULL,
[Description] [varchar](max) NULL,
PRIMARY KEY CLUSTERED 
(
    [RowID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[DomainLinks](
[RowID] [int] IDENTITY(1,1) NOT NULL,
[SuppRowID] [int] NOT NULL,
[Title] [varchar](250) NULL,
[Link] [varchar](max) NULL,
[Description] [varchar](max) NULL,
PRIMARY KEY CLUSTERED 
(
    [RowID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

父表只是一个标题和它们作为外键继承的 rowid。在我的代码中,用户从下拉列表中选择了一个供应商,我得到了 ID,它位于 select 语句中。

有时,大多数时候,结果会不止一个。

希望这可以澄清情况

4

1 回答 1

0

现在很好。我创建了一个新页面,找到了我想要的数据和标题的外键。基于这两个变量,我可以从sql中画出单行。

protected void hypTit_Click(object sender, EventArgs e)
    {
        if (sender is LinkButton)
        {
            lstArt.Visible = true;
            LinkButton btn = (LinkButton)sender;
            string buttonpressed = btn.Text.ToString();
            fillVals(buttonpressed);
        }
    }

    protected void fillVals(String name)
    {
        string cxnstr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(cxnstr);
        conn.Open();
        DataSet ds;
        try
        {
            ds = new DataSet("ds");
            SqlDataAdapter da = new SqlDataAdapter("select Title,[Description]as[Article] from StandardReplies where CatRowID = " + catID + " and Title like '" + name + "'", conn);
            da.Fill(ds);
            lstArt.DataSource = ds;
            lstArt.DataBind();
        }
        catch (SqlException ex)
        {
            Response.Write(ex.ToString());
        }
    }

类似的东西。不想放弃太多

于 2013-04-24T05:50:31.193 回答