开发者

Datalist in ASP.NET will not display?

I have been working on dynamically generating some labels using a DataList in ASP.NET code for a few days now, and cannot, for the life of me, get the control to display to the screen. I am fairly sure that the issue is some small syntactical thing, or an HTML tag that I forgot to set. I have confirmed via the debugger that the list of data is, in fact, being pulled into the datasource, and when I view the HTML code 'design' view, the control does display. However, when I run the page, the control is not visible. Any insight as to why this is happening would be very much appreciated.

Below is the code relevant to the question...

C# partial class:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InventoryService service = new InventoryService();

            ArrayList foundYears = service.FindYears();
            DataSet ds = new DataSet();
            ds.Tables.Add("Years");
            ds.Tables[0].Columns.Add("Year");

            foreach (string yr in foundYears)
            {
                if (yr != "")
                {
                    DataRow dr = ds.Tables[0].NewRow();
                    dr["Year"] = yr;
                    ds.Tables[0].Rows.Add(dr);
                }
            }

            DataList headerRepeater = new DataList();
            headerRepeater.DataSource = ds.Tables[0];
            headerRepeater.DataKeyField = "Year";
            headerRepeater.DataBind();
            headerRepeater.Visible = true;
        }
    }

HTML partial class:

<table width="100%" align="center">
    <tr>
        <td style="width: 868px">
             <asp:DataList id='headerRepeater' Runat='server' CellPadding='5' CellSpacing='15' GridLines='Vertical' HorizontalAlign='Left' RepeatColumns='30' RepeatDirection='Horizontal' RepeatLayout='Table' ShowFooter='Fal开发者_StackOverflowse' ShowHeader='False' Visible = 'True' CssClass='DataList' DataKeyField ='Year'>
            <ItemTemplate>
                <asp:Label runat='server' ID='lblItemName' Text='<%# DataBinder.Eval(Container.DataItem, "Year") %>'>
                </asp:Label>
            </ItemTemplate>
        </asp:DataList>
    </td>
</tr>
<tr>


You are creating a new local variable when you say:

DataList headerRepeater = new DataList(); .

Remove this line from the Page_Load the page should work fine.

The data list is already added to the Page Control collection by the time Page Load event is triggered


// Why do you create a new variable named 'headerRepeater' here?    
DataList headerRepeater = new DataList(); 
headerRepeater.DataSource = ds.Tables[0];
headerRepeater.DataKeyField = "Year";
headerRepeater.DataBind();
headerRepeater.Visible = true;

Remove the following line:

DataList headerRepeater = new DataList(); 


Your are creating a new instance of a DataList and haven't added it to the page controls.

DataList headerRepeater = new DataList(); //The problem is here ControlThatWillHoldTheDataList.Controls.Add(headerRepeater); //My Addition
headerRepeater.DataSource = ds.Tables[0];
headerRepeater.DataKeyField = "Year";
headerRepeater.DataBind();
headerRepeater.Visible = true;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜