English 中文(简体)
How to Get Updates(changed Items) From CheckboxList?
原标题:

I have a Checkbox list in my page and its datasource set programatically in PreLoad() event:

protected void Page_PreLoad()
{
        if (!Page.IsPostBack)
        {
        CheckBoxList1.DataSource = NoK.AcceptedNoks((Guid)Membership.GetUser().ProviderUserKey);
        CheckBoxList1.DataTextField = "FullName";
        CheckBoxList1.DataValueField = "NoKId";
        CheckBoxList1.DataBind();
        }
foreach (ListItem chk in CheckBoxList1.Items)
{
    if (PrivateMessage.HasAccess(Request.QueryString["MessageId"], chk.Value))
    {
        chk.Selected = true;
    }
}

}

as you see in foreach will check for whether an item must be checked or Not. and it works nice. this means that end-user can Edit list Items and by default some of Item has been checked. now I want to get items by clicking a Button:

protected void UpdateRightBtn_Click(object sender, EventArgs e)
{
    var SelectedNokIds =
        CheckBoxList1.Items
        .OfType<ListItem>()
        .Where(li =>
            li.Selected == true)
            .Select(l => new Guid(l.Value));
}

but the Items in SelectedNokIds are still Old Items and if user change checkboxes no effect apeares in SelectedNokIds. Why???

Please Help!

问题回答

It looks like it is because you are re-setting the values again at postback, effectively clearing the user s selection. You need to only initialize the values when it is not a postback.

foreach (ListItem chk in CheckBoxList1.Items){ if (PrivateMessage.HasAccess(Request.QueryString["MessageId"], chk.Value)) { chk.Selected = true; }}

This line fires on every page load, so that would reset the selection (at least the ones that set selected to true). Shouldn t that be within !Page.IsPostback too? And in the update button, you could rebind there...

If you need to figure out what changed, you need to query the items in the data source again, and cross-reference those against the new selection list.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签