【发布时间】:2018-03-11 18:47:56
【问题描述】:
这是下拉列表和数据列表代码
<div>
Sort by Category:
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>All</asp:ListItem>
<asp:ListItem>Decoration</asp:ListItem>
<asp:ListItem>Catering</asp:ListItem>
<asp:ListItem>Entertainment</asp:ListItem>
<asp:ListItem>Sound</asp:ListItem>
<asp:ListItem>Others</asp:ListItem>
</asp:DropDownList>
<asp:DataList ID="DataList1" runat="server"
GridLines="Both" RepeatColumns="4" RepeatDirection="Horizontal"
Width="1000px" DataSourceID="SqlDataSource1" >
<ItemTemplate>
<table class="nav-justified">
<tr>
<td class="text-center">
<strong>
<asp:Label ID="Label1" runat="server"
Text='<%# Eval("serName") %>'></asp:Label>
</strong>
</td>
</tr>
<tr>
<td>
<asp:Image ID="Image1" runat="server" Height="179px"
ImageUrl='<%# Eval("serImg") %>' Width="191px"/>
</td>
</tr>
<tr>
<td>
<strong>
<asp:Label ID="Label2" runat="server" Text="Rs"></asp:Label>
<asp:Label ID="Label3" runat="server"
Text='<%# Eval("sprice") %>'>
</asp:Label>
</strong>
</td>
</tr>
<tr>
<td class="text-center">
<asp:Button ID="Button1" runat="server" Text="Details" />
</td>
</tr>
</table>
<br />
</ItemTemplate>
</asp:DataList>
</div>
这是 .cs 代码。其目的是根据下拉列表中选择的类别过滤列表中的数据
protected void Page_Load(object sender, EventArgs e)
{
String conString = ConfigurationManager.ConnectionStrings["regcon"].ConnectionString;
string query = "select * from addService where serCategory=@cat";
SqlCommand cmd = new SqlCommand(query);
cmd.Parameters.AddWithValue("@cat", DropDownList1.SelectedItem.Value);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
DataList1.DataSource = ds;
DataList1.DataBind();
}
}
}
}
我收到错误“DataSource 和 DataSourceID 都在 'DataList1' 上定义。删除一个定义。”当我运行它时。删除 Datalist1 或 DataSourceID 会出错。我该如何解决这个问题?
【问题讨论】:
-
修复方法是...删除一个定义。
-
@Adarsh 你看到我的回答了吗?
-
@wazz 是的,它奏效了。谢谢。
标签: c# asp.net visual-studio-2017