[英]Error in code dealing with Nested Repeaters and Databound label from a SQL database query
我正在处理以下代码,第一个转发器从SQL数据库查询中获取某些信息,然后嵌套的转发器使用LabelID进行第二个SQL查询。 我收到此错误:
说明:编译服务于此请求所需的资源期间发生错误。 请查看以下特定的错误详细信息,并适当地修改您的源代码。
编译器错误消息:CS0103:名称“ NestedRepeater”在当前上下文中不存在
源错误:第72行:con.Open(); 第73行:SqlDataReader rdr2 = cmd.ExecuteReader(); 第74行:NestedRepeater.DataSource = rdr2; 第75行:NestedRepeater.DataBind(); 第76行:con.Close();
源文件:c:\\ websites \\ euchnernodus \\ PartSearch \\ usrPartSearch102.ascx.cs行:74
我在C#中的代码减去实际的连接字符串:
public partial class usrPartSearch102 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
string filename = this.Parent.Page.Title;
Label parLbl = (Label)Parent.FindControl("Label1");
if (parLbl != null)
{
Labelpart.Text = parLbl.Text;
}
string b = Labelpart.Text;
string part = b;
string fam = b.Substring(0, 2);
Labelfam.Text = fam;
string act = b.Substring(4, 2);
Labelact.Text = act;
string switches = b.Substring(2, 2);
Labelswitch.Text = switches;
string switch02 = "02";
string switch03 = "03";
string switch11 = "11";
string switch12 = "12";
string str = "SELECT [ID], [ProductName] FROM [bvc_Product] WHERE (([ProductName] LIKE '%' + @param1 + '%') AND ([ProductName] LIKE '%' + @param2 + '%') AND ([ProductName] NOT LIKE '%' + @param3 + '%') AND (([ProductName] LIKE '%' + @param4 + '%') OR ([ProductName] LIKE '%' + @param5 + '%') OR ([ProductName] LIKE '%' + @param6 + '%') OR ([ProductName] LIKE '%' + @param7 + '%') OR ([ProductName] LIKE '%' + @param8 + '%')))";
SqlConnection con = new SqlConnection(connectionStrings);
SqlCommand cmd = new SqlCommand(str, con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@param1", fam);
cmd.Parameters.AddWithValue("@param2", act);
cmd.Parameters.AddWithValue("@param3", part);
cmd.Parameters.AddWithValue("@param4", switches);
cmd.Parameters.AddWithValue("@param5", switch02);
cmd.Parameters.AddWithValue("@param6", switch03);
cmd.Parameters.AddWithValue("@param7", switch11);
cmd.Parameters.AddWithValue("@param8", switch12);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
MyRepeater.DataSource = rdr;
MyRepeater.DataBind();
con.Close();
foreach(RepeaterItem ri in MyRepeater.Items)
{
string ID = MyRepeater.FindControl("lblID").ToString();
string str2 = "SELECT [PropertyID], [PropertyValue] FROM [bvc_ProductPropertyValue] WHERE [ProductID] = @param9";
SqlCommand cmd2 = new SqlCommand(str2, con);
cmd2.CommandType = CommandType.Text;
cmd2.Parameters.AddWithValue("param9", ID);
con.Open();
SqlDataReader rdr2 = cmd.ExecuteReader();
NestedRepeater.DataSource = rdr2;
NestedRepeater.DataBind();
con.Close();
}
}
public class Listing
{
public string ID { get; set; }
public string ProductName { get; set; }
public string PropertyValue { get; set; }
public string PropertyID { get; set;}
public string ProductID { get; set;}
}
}
ASP.NET
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="usrPartSearch102.ascx.cs" Inherits="usrPartSearch102" %>
<asp:Label ID="Label1" runat="server" Text="Please check individual parts for availability."></asp:Label>
<br />
<asp:Label ID="Labelpart" runat="server" Visible="true"></asp:Label><br />
<asp:Label ID="Labelfam" runat="server" Visible="true"></asp:Label><br />
<asp:Label ID="Labelact" runat="server" Visible="true"></asp:Label><br />
<asp:Label ID="Labelswitch" runat="server" Visible="true"></asp:Label><br />
<asp:Label ID="Label02" runat="server" Visible="True" Text="02"></asp:Label><br />
<asp:Label ID="Label03" runat="server" Visible="True" Text="03"></asp:Label><br />
<asp:Label ID="Label11" runat="server" Visible="True" Text="11"></asp:Label><br />
<asp:Label ID="Label12" runat="server" Visible="True" Text="12"></asp:Label><br />
<body>
<div>
<table class="auto-style1">
<tr>
<td>
<asp:Repeater ID="MyRepeater" runat="server" >
<HeaderTemplate>
<Table style="font: 8pt verdana" Border="1">
<tr style="background-color:#FFFFFF">
<tr>
<th>Product Name</th>
<th>Article ID</th>
<th>Item Description</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr style="background-color:#FFFFCC" Border="1">
<tr>
<td>
<%# DataBinder.Eval(Container.DataItem,"ProductName") %>
</td>
<td>
<asp:Label ID="lblID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"ID") %>'></asp:Label>
</td>
<td>
<asp:Repeater ID="NestedRepeater" runat="server">
<HeaderTemplate>
<Table style="font: 8pt verdana">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# DataBinder.Eval(Container.DataItem, "PropertyValue") %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</Table>
</FooterTemplate>
</asp:Repeater>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tabel>
</FooterTemplate>
</asp:Repeater>
</td>
</tr>
</table>
</div>
</body>
我无法弄清楚我在做什么错。 我已经尝试过许多不同的方法来发现如何做到这一点,并不断出错。 预先感谢您提供的任何帮助。
您需要将内部中继器绑定到外部中继器的ItemDtatBound事件中。
我将更改标记以添加事件方法:
<asp:Repeater ID="MyRepeater" runat="server" OnItemDataBound="MyRepeater_ItemDataBound">
在page_load中,我将调用一种方法来对外部中继器进行数据绑定:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadMyRepeater();
}
}
private void LoadMyRepeater()
{
string filename = this.Parent.Page.Title;
Label parLbl = (Label)Parent.FindControl("Label1");
if (parLbl != null)
{
Labelpart.Text = parLbl.Text;
}
string b = Labelpart.Text;
string part = b;
string fam = b.Substring(0, 2);
Labelfam.Text = fam;
string act = b.Substring(4, 2);
Labelact.Text = act;
string switches = b.Substring(2, 2);
Labelswitch.Text = switches;
string switch02 = "02";
string switch03 = "03";
string switch11 = "11";
string switch12 = "12";
string str = "SELECT [ID], [ProductName] FROM [bvc_Product] WHERE (([ProductName] LIKE '%' + @param1 + '%') AND ([ProductName] LIKE '%' + @param2 + '%') AND ([ProductName] NOT LIKE '%' + @param3 + '%') AND (([ProductName] LIKE '%' + @param4 + '%') OR ([ProductName] LIKE '%' + @param5 + '%') OR ([ProductName] LIKE '%' + @param6 + '%') OR ([ProductName] LIKE '%' + @param7 + '%') OR ([ProductName] LIKE '%' + @param8 + '%')))";
SqlConnection con = new SqlConnection(connectionStrings);
SqlCommand cmd = new SqlCommand(str, con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@param1", fam);
cmd.Parameters.AddWithValue("@param2", act);
cmd.Parameters.AddWithValue("@param3", part);
cmd.Parameters.AddWithValue("@param4", switches);
cmd.Parameters.AddWithValue("@param5", switch02);
cmd.Parameters.AddWithValue("@param6", switch03);
cmd.Parameters.AddWithValue("@param7", switch11);
cmd.Parameters.AddWithValue("@param8", switch12);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
MyRepeater.DataSource = rdr;
MyRepeater.DataBind();
con.Close();
}
现在,在外部转发器的IemDataBound事件中,我将从项目中找到内部转发器并将其绑定:
protected void MyRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string ID = e.Item.FindControl("lblID").ToString();
System.Web.UI.WebControls.Repeater rpt = (System.Web.UI.WebControls.Repeater)e.Item.FindControl("NestedRepeater");
string str = "SELECT [ID], [ProductName] FROM [bvc_Product] WHERE (([ProductName] LIKE '%' + @param1 + '%') AND ([ProductName] LIKE '%' + @param2 + '%') AND ([ProductName] NOT LIKE '%' + @param3 + '%') AND (([ProductName] LIKE '%' + @param4 + '%') OR ([ProductName] LIKE '%' + @param5 + '%') OR ([ProductName] LIKE '%' + @param6 + '%') OR ([ProductName] LIKE '%' + @param7 + '%') OR ([ProductName] LIKE '%' + @param8 + '%')))";
SqlConnection con = new SqlConnection(connectionStrings);
if(rpt!= null)
{
string str2 = "SELECT [PropertyID], [PropertyValue] FROM [bvc_ProductPropertyValue] WHERE [ProductID] = @param9";
SqlCommand cmd2 = new SqlCommand(str2, con);
cmd2.CommandType = CommandType.Text;
cmd2.Parameters.AddWithValue("param9", ID);
con.Open();
SqlDataReader rdr2 = cmd2.ExecuteReader();
rpt.DataSource = rdr2;
rpt.DataBind();
con.Close();
}
}
}
希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.