开发者

How to reset ListBox.Rows property to default (without hard coding the default value)

I have an ASP.NET Li开发者_开发知识库stBox on a page, and as postbacks occur, I change the items in the list. If there are >= 10 items in the list, I set the Rows property = 10. But if there are less than 10 items, I'd like to set the Rows back to whatever the default value is for Rows.

I see from examining the reflected code that the default value is 4, but I'd rather not hard code it to 4 in my code and instead somehow just reset it to the default.

Is there a way to do this?


You can fetch the default value during the page's Init phase. From the documentation:

In this stage of the page's life cycle, declared server controls on the page are initialized to their default state; however, the view state of each control is not yet populated.

So you can do something like:

private int _defaultRows;

protected void Page_Init(object sender, EventArgs e)
{
    _defaultRows = yourListBox.Rows;
}

protected void Page_PreRender(object sender, EventArgs e)
{
    if (yourListBox.Items.Count >= 10) {
        yourListBox.Rows = 10;
    } else {
        yourListBox.Rows = _defaultRows;
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜