我正在处理以下代码,第一个转发器从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>
我无法弄清楚我做错了什么。我已经尝试了很多不同的方法,我发现如何做到这一点,并不断收到错误。提前感谢您提供任何帮助。
答案 0 :(得分:0)
您需要将内部转发器绑定在外部转发器的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事件中,我会从item找到内部转发器并绑定它:
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();
}
}
}
希望它有所帮助!