C
Christiaan Nieuwlaat
Hi,
I'm in need of a solution for the following problem:
I've got an ASP.NET page which contains a placeholder.
The placeholder will contain a dynamically added checklistbox control where
the request["mode"] == "edit", and it should contain literal controls for
each selected item of the checklistbox when request["mode"] != "edit".
The problem is that I can get it to show the checklistbox on editmode, but
it seems to forget the selected values when the page has a postback.
Could someone please help me to get it working?
Here is the code behind:
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.HtmlControls;
using sesam.common;
using sesam.controls;
using System.Web.UI.WebControls;
using sesam.core.test;
namespace sesam.pages.order
{
/// <summary>
/// Summary description for projects.
/// </summary>
public class SurveyPage : BasePage
{
protected Content selector;
protected Content commontContent;
protected Content resultContent;
protected sesam.controls.Layout Default;
protected HtmlForm mainForm;
protected System.Web.UI.HtmlControls.HtmlForm Form1;
protected Label lbProdcat, lbOrder, lbDate, lbExaminor;
protected DropDownList DDArticle,DDAssesment;
protected Content pageTitle;
protected PlaceHolder AssessmentItems;
protected CheckBoxList cblAssessmentItems;
protected TextBox ArtComment;
protected BaseOrder myOrder;
// data sets
protected DataSet DSOrders = new DataSet();
protected DataSet DSArticles = new DataSet();
protected SqlDataAdapter DAOrders,DAArticles;
protected sesam.controls.Content commonContent;
protected sesam.controls.Content watchContent;
protected sesam.controls.Content pageContent;
protected SqlConnection CNCommon, CNGeneralAssessment;
public string getProdCatString(int id)
{
SqlConnection CNPCS = new
SqlConnection(ConfigurationSettings.AppSettings["SESAM_DSN"]);
SqlCommand CMDPCS = new SqlCommand("SELECT Name from tblTerm where TermId
= @pcid",CNPCS);
CMDPCS.Parameters.Add("@pcid",SqlDbType.Int);
CMDPCS.Parameters["@pcid"].Value = id;
CNPCS.Open();
string retval = (string)CMDPCS.ExecuteScalar();
CNPCS.Close();
return retval;
}
public SurveyPage()
{
this.Load += new EventHandler(Page_Load);
}
private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["CurrentArticle"] = null;
if (Request["TorId"] == null)
ViewState["TorId"] = 0;
else
ViewState["TorId"] = int.Parse(Request["TorId"]);
myOrder = new BaseOrder((int)ViewState["TorId"]);
lbOrder.Text = myOrder.id.ToString() + " - [" +myOrder.laminator+"] - "
+ myOrder.entrydate.ToShortDateString();
lbProdcat.Text = getProdCatString(myOrder.productcategoryId);
DDArticle.DataSource = myOrder.articles;
DDArticle.DataTextField = "name";
DDArticle.DataValueField = "id";
DDArticle.DataBind();
lbDate.Text = DateTime.Now.ToShortDateString();
lbExaminor.Text = "C. Nau"; // this should be changed to the logged in
user
Article CurrentArticle =
(Article)myOrder.articles[DDArticle.SelectedIndex];
ArtComment.Text = CurrentArticle.comment;
ListItem FindedItem =
DDAssesment.Items.FindByValue(CurrentArticle.licassessment);
if ( FindedItem != null )
FindedItem.Selected = true;
// setup different datasources
CNCommon = new
SqlConnection(ConfigurationSettings.AppSettings["SESAM_DSN"]);
CNCommon.Close();
}
else
{
myOrder = new BaseOrder((int)ViewState["TorId"]);
Article CurrentArticle =
(Article)myOrder.articles[DDArticle.SelectedIndex];
ArtComment.Text = CurrentArticle.comment;
/*ListItem FindedItem =
DDAssesment.Items.FindByValue(CurrentArticle.licassessment);
if ( FindedItem != null )
FindedItem.Selected = true;*/
}
if (Request["mode"] != "edit")
{
DDAssesment.Enabled = false;
Label NoAssess = new Label();
NoAssess.Text = "- No Assessment Available";
AssessmentItems.Controls.Add(NoAssess);
AssessmentItems.Controls.Add(cblAssessmentItems);
int selectedcount = 0;
foreach (ListItem li in cblAssessmentItems.Items)
{
selectedcount += li.Selected ? 1:0;
}
Literal AssItem = new Literal();
AssItem.Text = selectedcount.ToString();
}
else
{
cblAssessmentItems = new CheckBoxList();
cblAssessmentItems.Items.Add("The article complies with technical
standards");
cblAssessmentItems.Items.Add("The article doesn`t comply with technical
standards");
cblAssessmentItems.Items.Add("The article should be retested");
cblAssessmentItems.Items.Add("The article complies with technical
standards after improvement of the above mentioned disapproved criteria");
cblAssessmentItems.Items.Add("Please confirm by letter");
cblAssessmentItems.Items.Add("The article doesn't obtain a license");
AssessmentItems.Controls.Add(cblAssessmentItems);
}
ViewState["CurrentArticle"] = DDArticle.SelectedItem.Value;
}
public string GetWorkingArticle()
{
return DDArticle.SelectedItem.Value;
}
#region Web Form Designer generated code
protected override void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
}
#endregion
}
I'm in need of a solution for the following problem:
I've got an ASP.NET page which contains a placeholder.
The placeholder will contain a dynamically added checklistbox control where
the request["mode"] == "edit", and it should contain literal controls for
each selected item of the checklistbox when request["mode"] != "edit".
The problem is that I can get it to show the checklistbox on editmode, but
it seems to forget the selected values when the page has a postback.
Could someone please help me to get it working?
Here is the code behind:
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.HtmlControls;
using sesam.common;
using sesam.controls;
using System.Web.UI.WebControls;
using sesam.core.test;
namespace sesam.pages.order
{
/// <summary>
/// Summary description for projects.
/// </summary>
public class SurveyPage : BasePage
{
protected Content selector;
protected Content commontContent;
protected Content resultContent;
protected sesam.controls.Layout Default;
protected HtmlForm mainForm;
protected System.Web.UI.HtmlControls.HtmlForm Form1;
protected Label lbProdcat, lbOrder, lbDate, lbExaminor;
protected DropDownList DDArticle,DDAssesment;
protected Content pageTitle;
protected PlaceHolder AssessmentItems;
protected CheckBoxList cblAssessmentItems;
protected TextBox ArtComment;
protected BaseOrder myOrder;
// data sets
protected DataSet DSOrders = new DataSet();
protected DataSet DSArticles = new DataSet();
protected SqlDataAdapter DAOrders,DAArticles;
protected sesam.controls.Content commonContent;
protected sesam.controls.Content watchContent;
protected sesam.controls.Content pageContent;
protected SqlConnection CNCommon, CNGeneralAssessment;
public string getProdCatString(int id)
{
SqlConnection CNPCS = new
SqlConnection(ConfigurationSettings.AppSettings["SESAM_DSN"]);
SqlCommand CMDPCS = new SqlCommand("SELECT Name from tblTerm where TermId
= @pcid",CNPCS);
CMDPCS.Parameters.Add("@pcid",SqlDbType.Int);
CMDPCS.Parameters["@pcid"].Value = id;
CNPCS.Open();
string retval = (string)CMDPCS.ExecuteScalar();
CNPCS.Close();
return retval;
}
public SurveyPage()
{
this.Load += new EventHandler(Page_Load);
}
private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["CurrentArticle"] = null;
if (Request["TorId"] == null)
ViewState["TorId"] = 0;
else
ViewState["TorId"] = int.Parse(Request["TorId"]);
myOrder = new BaseOrder((int)ViewState["TorId"]);
lbOrder.Text = myOrder.id.ToString() + " - [" +myOrder.laminator+"] - "
+ myOrder.entrydate.ToShortDateString();
lbProdcat.Text = getProdCatString(myOrder.productcategoryId);
DDArticle.DataSource = myOrder.articles;
DDArticle.DataTextField = "name";
DDArticle.DataValueField = "id";
DDArticle.DataBind();
lbDate.Text = DateTime.Now.ToShortDateString();
lbExaminor.Text = "C. Nau"; // this should be changed to the logged in
user
Article CurrentArticle =
(Article)myOrder.articles[DDArticle.SelectedIndex];
ArtComment.Text = CurrentArticle.comment;
ListItem FindedItem =
DDAssesment.Items.FindByValue(CurrentArticle.licassessment);
if ( FindedItem != null )
FindedItem.Selected = true;
// setup different datasources
CNCommon = new
SqlConnection(ConfigurationSettings.AppSettings["SESAM_DSN"]);
CNCommon.Close();
}
else
{
myOrder = new BaseOrder((int)ViewState["TorId"]);
Article CurrentArticle =
(Article)myOrder.articles[DDArticle.SelectedIndex];
ArtComment.Text = CurrentArticle.comment;
/*ListItem FindedItem =
DDAssesment.Items.FindByValue(CurrentArticle.licassessment);
if ( FindedItem != null )
FindedItem.Selected = true;*/
}
if (Request["mode"] != "edit")
{
DDAssesment.Enabled = false;
Label NoAssess = new Label();
NoAssess.Text = "- No Assessment Available";
AssessmentItems.Controls.Add(NoAssess);
AssessmentItems.Controls.Add(cblAssessmentItems);
int selectedcount = 0;
foreach (ListItem li in cblAssessmentItems.Items)
{
selectedcount += li.Selected ? 1:0;
}
Literal AssItem = new Literal();
AssItem.Text = selectedcount.ToString();
}
else
{
cblAssessmentItems = new CheckBoxList();
cblAssessmentItems.Items.Add("The article complies with technical
standards");
cblAssessmentItems.Items.Add("The article doesn`t comply with technical
standards");
cblAssessmentItems.Items.Add("The article should be retested");
cblAssessmentItems.Items.Add("The article complies with technical
standards after improvement of the above mentioned disapproved criteria");
cblAssessmentItems.Items.Add("Please confirm by letter");
cblAssessmentItems.Items.Add("The article doesn't obtain a license");
AssessmentItems.Controls.Add(cblAssessmentItems);
}
ViewState["CurrentArticle"] = DDArticle.SelectedItem.Value;
}
public string GetWorkingArticle()
{
return DDArticle.SelectedItem.Value;
}
#region Web Form Designer generated code
protected override void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
}
#endregion
}