如何在asp.net c#中以编程方式将数据从sql添加到列表视图
本文关键字:数据 sql 添加 视图 列表 方式 编程 asp net | 更新日期: 2023-09-27 18:07:30
我试图在我的页面上填充一个列表视图。我使用代码从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";
}
}
进入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>
可以正常工作。但我的问题是其他的记录呢?我不知道如何添加更多的记录到列表视图短添加:
<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 />
和递增。但这是硬编码。我如何在页面加载中做到这一点?我应该继续使用列表视图吗?开关控制?
编辑ok。这是域链接和供应链接的表结构:
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]
父表只是一个标题和它们继承的行作为外键。在我的代码中,用户从下拉列表中选择供应商,我在select语句中获得id。
有时,大多数情况下,结果将不止一个。
希望这能澄清情况
现在可以了。我创建了一个新页面,它为我想要的数据和标题找到外键。基于这两个变量,我可以从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());
}
}
之类的。不想透露太多