Home All Groups Group Topic Archive Search About

How can I get selected Checklistbox Items to Literal in Placeholde

Author
25 May 2005 10:00 AM
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


    }

Author
25 May 2005 5:53 PM
Brock Allen
> 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.

If you have dynamically added controls to the form, then you need to recreate
them upon every postback. Override CreateChildControls to do this. Once you've
recreated them upon every postback, then they will maintain their postback
data and state.

-Brock
DevelopMentor
http://staff.develop.com/ballen
Author
26 May 2005 1:01 PM
Christiaan Nieuwlaat
Thank you, but when I do that they'll be visible in every mode the page is in.

What I'm trying to achieve is this:

pseudo code:

If page mode != edit
  if states not already fetched from database do this first
  if states altered in postback use alterations
  Display state texts as literal controls in the placeholder

else
  if states not already fetched from database do this first
  if states altered in postback use alterations
  show checkboxes with respective states

And because the checkboxes should only be visible when the page is in edit
mode, I'm unsure if creating them in CreateChildControls is possible...

Thank you,

Chris

Show quoteHide quote
"Brock Allen" wrote:

> > 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.
>
> If you have dynamically added controls to the form, then you need to recreate
> them upon every postback. Override CreateChildControls to do this. Once you've
> recreated them upon every postback, then they will maintain their postback
> data and state.
>
> -Brock
> DevelopMentor
> http://staff.develop.com/ballen
>
>
>
>
Author
30 May 2005 5:55 PM
Brock Allen
Well, here's a link to a sample that does this. Hopefully you can work it
to do what you want:

http://groups-beta.google.com/group/microsoft.public.dotnet.framework.aspnet/browse_thread/thread/5e4ba4cfe5902a4b/a5717a97bd32c450?q=brock+dynamic+controls+viewstate&rnum=1

-Brock
DevelopMentor
http://staff.develop.com/ballen



Show quoteHide quote
> Thank you, but when I do that they'll be visible in every mode the
> page is in.
>
> What I'm trying to achieve is this:
>
> pseudo code:
>
> If page mode != edit
> if states not already fetched from database do this first
> if states altered in postback use alterations
> Display state texts as literal controls in the placeholder
> else
> if states not already fetched from database do this first
> if states altered in postback use alterations
> show checkboxes with respective states
> And because the checkboxes should only be visible when the page is in
> edit mode, I'm unsure if creating them in CreateChildControls is
> possible...
>
> Thank you,
>
> Chris
>
> "Brock Allen" wrote:
>
>>> 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.
>>>
>> If you have dynamically added controls to the form, then you need to
>> recreate them upon every postback. Override CreateChildControls to do
>> this. Once you've recreated them upon every postback, then they will
>> maintain their postback data and state.
>>
>> -Brock
>> DevelopMentor
>> http://staff.develop.com/ballen