1

我正在处理以下代码,其中第一个转发器从 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>

我一直无法弄清楚我做错了什么。我已经尝试了许多不同的方法,我发现如何做到这一点并不断出错。预先感谢您提供的任何帮助。

4

1 回答 1

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();
        }
    }
}

希望能帮助到你!

于 2013-10-11T00:56:29.817 回答