简体   繁体   English

如何以编程方式将数据从sql添加到asp.net C#中的列表视图

[英]How to programmatically add data from sql to a listview in asp.net c#

I am trying to populate a listview on my page. 我正在尝试在页面上填充列表视图。 I use code to read data from sql: 我使用代码从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";
            }
        }

which goes into listview: 进入列表视图:

<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>

which works fine. 效果很好。 but my problem is what about the other records? 但是我的问题是其他记录呢? I have no idea how to add more records to the listview short of adding: 我不知道如何添加更多记录到列表视图中,而无需添加:

<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 />

and incrementing. 和递增。 But that is hard coded. 但这是硬编码的。 How do I do this on a page load? 如何在页面加载时执行此操作? Should I continue using a listview? 我应该继续使用列表视图吗? Switch controls? 切换控件?

EDIT 编辑

ok. 好。 this is the table structure for both domain links and supply links: 这是域链接和供应链接的表结构:

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]

and

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]

the parent table is just a title and the rowid they inherit from as a foreign key. 父表只是一个标题和它们作为外键继承的rowid。 in my code, a user selects a supplier from a dropdownlist, I get the id and that is in the select statement. 在我的代码中,用户从下拉列表中选择了一个供应商,我得到了ID,该ID在select语句中。

sometimes, most times, the results would be more than one. 有时,大多数情况下,结果将不止一个。

hope this clarifies the situation 希望这可以澄清情况

this is fine now. 现在很好。 I made a new page, that finds the foreign keys for the data that I want and the title. 我创建了一个新页面,该页面查找所需数据和标题的外键。 based on these two variables, I can draw the single row out of sql. 基于这两个变量,我可以从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());
        }
    }

something like that. 这样的事情。 don't want to give too much away 不想付出太多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM