views:

34

answers:

0

Lets assume there are list of users and under them there are several attributes. The users appear in checkboxlist. When a button is clicked it retrieves the attributes of the selected users and show them in other checkboxlists. Now when the attributes are selected and another button is pressed the selected attributes will be saved. My problem is when i try to retrieve the attributes it cannot memorize the attributes whether it is selected or not. I have used viewstate but could not find a solution. My code is as below:

<table style="width:100%;">
    <tr>
        <td>
            <asp:Label ID="Label1" runat="server" Text="Select person from the list:"></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <asp:CheckBoxList ID="CheckBoxList1" runat="server">
            </asp:CheckBoxList>
        </td>
    </tr>
    <tr>
        <td>
            <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
        </td>
    </tr>
    <tr>
        <td>
            <table style="width:100%;">
                <tr>
                    <td style="width: 148px">
            <asp:Button ID="Button1" runat="server" Text="Get Attributes" 
                onclick="Button1_Click" />
                    </td>
                    <td style="width: 157px">
                        <asp:Button ID="Button2" runat="server" Text="Save Attributes" 
                            onclick="Button2_Click" />
                    </td>
                    <td>
                        &nbsp;</td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;</td>
    </tr>
</table>

and code is

protected override void LoadViewState(object savedState)
{
    base.LoadViewState(savedState);

    if (Convert.ToBoolean(ViewState["createState"]))
    {
        LoadUsersAttribute();
        //ViewState["createState"] = ;
    }
}
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        LoadUser1();
        ViewState["createState"] = false;
    }
    else
    {
        //LoadUser1();
        //calculateAttributes();
    }
}

private void LoadUsersAttribute()
{
    foreach (ListItem lic in CheckBoxList1.Items)
    {
        if (lic.Selected)
        {
            Label labelName = new Label();
            labelName.Text = lic.Text+"'s Attributes are:";
            PlaceHolder1.Controls.Add(labelName);
            AttributeEntity AttributeEntityVariable = new AttributeEntity();
            AttributeBusinessLayer AttributeBusinessLayerVariable = new AttributeBusinessLayer();
            AttributeEntityVariable.User_Key = int.Parse(lic.Value);
            CheckBoxList CheckBoxListVar = new CheckBoxList();
            CheckBoxListVar.DataSource = AttributeBusinessLayerVariable.SearchByUser_KeyList(AttributeEntityVariable);
            CheckBoxListVar.DataTextField = "Attribute_Name";
            CheckBoxListVar.DataValueField = "Attribute_key";
            CheckBoxListVar.DataBind();
            CheckBoxListVar.ID = "c"+lic.Value;
            cblList.Add(CheckBoxListVar);
            PlaceHolder1.Controls.Add(CheckBoxListVar);

        }
    }
    string str = PlaceHolder1.Controls.ToString();
    ViewState["createState"] = true;
}

private void LoadUser1()
{
    UserBusinessLayer UserBusinessLayerVariabel = new UserBusinessLayer();
    List<UserEntity> UserEntityVariableList = UserBusinessLayerVariabel.GetUserList();
    List<CheckBox> checkBoxList = new List<CheckBox>();
    List<int> UserKey = new List<int>();
    CheckBoxList1.DataSource = UserEntityVariableList;
    CheckBoxList1.DataTextField = "User_Name";
    CheckBoxList1.DataValueField = "User_Key";
    CheckBoxList1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
    if (!Convert.ToBoolean(ViewState["createState"]))
    {
        LoadUsersAttribute();
        //ViewState["createState"] = false;
    }
}
protected void Button2_Click(object sender, EventArgs e)
{
    calculateAttributes();
}

private void calculateAttributes()
{
    LoadUsersAttribute();
    foreach (ListItem lic in CheckBoxList1.Items)
    {
        if (PlaceHolder1.FindControl("c" + lic.Value) != null)
        {
            CheckBoxList chkbxlstvar = (CheckBoxList)PlaceHolder1.FindControl("c" + lic.Value);
            foreach (ListItem licv in chkbxlstvar.Items)
            {
                if (licv.Selected)
                {

                }
            }
        }
    }
}

Please help me solve this problem. Thanks...