Frage

I am working on the following codes which the first repeater gets certain information from a SQL database query and then the nested repeater uses the LabelID to do the second SQL query. I am getting this error:

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0103: The name 'NestedRepeater' does not exist in the current context

Source Error: Line 72: con.Open(); Line 73: SqlDataReader rdr2 = cmd.ExecuteReader(); Line 74: NestedRepeater.DataSource = rdr2; Line 75: NestedRepeater.DataBind(); Line 76: con.Close();

Source File: c:\websites\euchnernodus\PartSearch\usrPartSearch102.ascx.cs Line: 74

My code in C# minus actual connectionstring:

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>

I have not been able to figure out what I am doing wrong. I have tried many different ways I have found on how to do this and keep getting errors. Thank you in advance for any assistance given.

War es hilfreich?

Lösung

You need to bind the inner repeater inside outer repeater's ItemDtatBound event.

I would change the markup to add the event method:

<asp:Repeater ID="MyRepeater" runat="server" OnItemDataBound="MyRepeater_ItemDataBound">

In page_load I would call a method to databind the outer repeater:

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

Now in outer repeater's IemDataBound event I would find the inner repeater from item and bind it:

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

Hope it helps!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top