Wednesday, April 6, 2011

Preserve datalist data on load event in c#

I have datalist containing radio buttons and check boxes. If I select some of them and then redirect to another aspx page. when I come to previous page the radio buttons and check boxes in datalist must be selected. I have selected data in session variable. How to do this using c#.

From stackoverflow
  • Using the ItemDataBound event I would retrieve the radio button and compare it's value to your Session variable

    something like this:

    protected void MyDataList_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        RadioButton myRadioButton = e.Items.FindControl("myRadioButton") as RadioButton;
    
        if (myRadioButton == null) { }
        else
        {
            if (string.IsNullOrEmpty(Session[myRadioButton.Value])) {}
            else myRadioButton.Checked = true;        
        }
    }
    
    hunter : After reading your comment you might want to add in the proper control server-side. It would be a lot cleaner. In my example you could just use a switch statement on your inputType type and create and add the proper control to the Item.

0 comments:

Post a Comment