PDA

View Full Version : How to add items to checkboxgroup or radiogroup from code behind on a post back?



vedagopal2004
Oct 28, 2009, 7:05 AM
Hi Coolite Team,

Here is my code and what I wanted achieve.
I have one user control in which I used CheckBoxGroup Control.
In the code behind based on the Options for a passed question Item the options should be shown as checkboxes.
which is done in LoadData method.

From another user control I am passing the QuestionItem to the below control and tried to load the items.

But it is not working as expected.

Please note that I am not using any AjaxEvents or Listeners and just using "AutoPostBack=true" to fire the server side events.

OptionsQuestion.ascx



<ext:CheckboxGroup ID="cbgOptionsQuestion" runat="server">
<Items>
<ext:Checkbox Enabled="true" Checked="true" runat="server" BoxLabel="No Options">
</ext:Checkbox>
</Items>
</ext:CheckboxGroup>


OptionsQuestion.aspx.cs



using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BizHR.UI.BaseClasses;
using System.Collections.Generic;
using BizHR.BusinessLayer.Recruitment;

namespace BizHR.UI.UserControls.Recruitment
{
public partial class OptionsQuestion : UserControlBase, BizHR.UI.Interfaces.IUserControlDataActions
{
public Question QuestionItem
{
get
{
if (ViewState["QItem"] == null)
ViewState["QItem"] = new Question();

return (Question)ViewState["QItem"];
}
set
{
ViewState["QItem"] = value;
}
}

public bool ValidationRequired
{
get
{
if (ViewState["valRequired"] == null)
ViewState["valRequired"] = false;

return (bool)ViewState["valRequired"];
}
set
{
ViewState["valRequired"] = value;
}
}

public string Answer
{
get
{
if (ViewState["answer"] == null)
ViewState["answer"] = string.Empty;

return (string)ViewState["answer"];
}
set
{
ViewState["answer"] = value;
}
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadData(new System.IO.StringWriter());

UpdateControlValues();
}

protected void UpdateControlValues()
{
if (ValidationRequired)
{
string ans = string.Empty;
foreach (Coolite.Ext.Web.Checkbox cbox in cbgOptionsQuestion.CheckedItems)
{
ans += cbox.Value.ToString() + ",";
}

ans = ans.TrimEnd(",".ToCharArray());
Answer = ans;
}
else
{
Answer = string.Empty;
}
}

#region IUserControlDataActions Members

public bool LoadData(System.IO.StringWriter swErrors)
{
cbgOptionsQuestion.FieldLabel = QuestionItem.QuestionName;

List<QuestionOptions> lstOptions = QuestionItem.GetOptions();
string[] answers = { };
if (Answer != null &amp;&amp; !Answer.Equals(string.Empty))
{
answers = Answer.Split(",".ToCharArray());
}
if (lstOptions.Count > 0)
cbgOptionsQuestion.Items.Clear();

foreach (QuestionOptions qo in lstOptions)
{
Coolite.Ext.Web.Checkbox chkBox = new Coolite.Ext.Web.Checkbox();
chkBox.BoxLabel = qo.OptionName;
chkBox.Value = qo.OptionId;
chkBox.Checked = IsOptionSelected(answers, qo.OptionId);
cbgOptionsQuestion.Items.Add(chkBox);
}
return Bizbites.UtilityFunctions.IsEmpty(swErrors);
}

bool IsOptionSelected(string[] answers, int optionId)
{
if (answers.Length > 0)
{
return answers.Contains(optionId.ToString());
}

return false;
}

public bool SaveData(System.IO.StringWriter swErrors)
{
//Nothing to do here
return true;
}

public bool ValidateData(System.IO.StringWriter swErrors)
{
if (ValidationRequired)
{
if (cbgOptionsQuestion.CheckedItems.Count == 0)
{
swErrors.WriteLine(this.BasePage.GetGlobalValidati onMessage(BizHR.Enumerations.CustomEnumarations.Va lidationMessageType.requiredfieldvalidator), QuestionItem.QuestionName);
}
}

return Bizbites.UtilityFunctions.IsEmpty(swErrors);
}

public bool ManagePermissions(System.IO.StringWriter swErrors)
{
//Nothing to do here
return true;
}

public bool DeleteData(System.IO.StringWriter swErrors)
{
//Nothing to do here
return true;
}

public Guid DataKeyId
{
get
{
return ViewState["KeyId"] != null ? (Guid)ViewState["KeyId"] : Guid.Empty;
}
set
{
ViewState["KeyId"] = value;
}
}

#endregion
}
}


From another user control