How can I get selected Checklistbox Items to Literal in Placeholde

Discussion in 'ASP .Net Web Controls' started by Christiaan Nieuwlaat, May 25, 2005.

  1. 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


    }
    Christiaan Nieuwlaat, May 25, 2005
    #1
    1. Advertising

  2. Christiaan Nieuwlaat

    Brock Allen Guest

    > 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
    Brock Allen, May 25, 2005
    #2
    1. Advertising

  3. Re: How can I get selected Checklistbox Items to Literal in Placeh

    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
    >
    >
    >
    >
    Christiaan Nieuwlaat, May 26, 2005
    #3
  4. Christiaan Nieuwlaat

    Brock Allen Guest

    Re: How can I get selected Checklistbox Items to Literal in Placeh

    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...450?q=brock dynamic controls viewstate&rnum=1

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



    > 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
    Brock Allen, May 30, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. D. Shane Fowlkes
    Replies:
    0
    Views:
    559
    D. Shane Fowlkes
    Apr 7, 2004
  2. =?Utf-8?B?RmVkZXJpY28=?=
    Replies:
    5
    Views:
    4,944
    Chilá
    Feb 1, 2006
  3. Bart Van Hemelen
    Replies:
    2
    Views:
    4,655
    Bart Van Hemelen
    Sep 4, 2006
  4. Iain
    Replies:
    3
    Views:
    920
  5. Anonieko Ramos

    What's wrong with rpc-literal? Why use doc-literal?

    Anonieko Ramos, Sep 27, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    366
    Anonieko Ramos
    Sep 27, 2004
Loading...

Share This Page