开发者

DataBind() on CheckBoxList: Problem with getting .Selected

开发者 https://www.devze.com 2023-03-20 03:00 出处:网络
I\'m new to C# and ASP.NET. I have a CheckBoxList named DeploySelectList. If I manually add items to the list as follows, I have no problems getting the .Selected items开发者_运维问答 after the Submi

I'm new to C# and ASP.NET.

I have a CheckBoxList named DeploySelectList. If I manually add items to the list as follows, I have no problems getting the .Selected items开发者_运维问答 after the Submit button is clicked.

protected void Page_Load(object sender, EventArgs e) {
        DeploySelectList.Items.Add("test 1");
        DeploySelectList.Items.Add("test 2");
        DeploySelectList.Items.Add("test 3");
}

However, if I create a DataBinding to a List object, all DeploySelectList items always have .Selected = false.

protected void Page_Load(object sender, EventArgs e) {
        List<String> list = DBFunctions.getDeploymentSelection();
        DeploySelectList.DataSource = list;
        DeploySelectList.DataBind();
}

Why would this be happening?

Thanks.

EDIT: As per the comment below, adding the code in an if (!IsPostBack) block fixed the problem. I still don't understand why the problem was not the same when doing it manually vs. doing the DataBinding. It seems that in both cases the selection should be lost.

Should this question be deleted?


The link below shows you the order that the page events are fired.

http://msdn.microsoft.com/en-us/library/ms178472.aspx

And as already mentioned you need to stop rebuilding the list on postback in the page load. so that you can check the selected property when the event code is executed.

0

精彩评论

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