Adding Items to Listbox of FirstWebform from Second Webform

Last post 12-26-2006 9:09 PM by Raymond Wen - MSFT. 4 replies.

Sort Posts:

  • Adding Items to Listbox of FirstWebform from Second Webform

    12-25-2006, 5:41 AM
    • Participant
      1,353 point Participant
    • Swati Jain
    • Member since 05-24-2006, 12:29 PM
    • Pune
    • Posts 731

    Hello All

    i have 2 aspx pages first.aspx and second.aspx

    first.aspx consists of a listbox.and  a button .

    when button is clicked it goes to next page(i.e second.aspx)

    Second.aspx is having two textboxes txtStartDate and txtEndDate and "Go Back" Button(html input button having similar functionality as that of Backbutton of browser)

    When "Go Back" is Clicked  this goes to First.aspx .

    I want that items should be added to the listbox of first.aspx

    according to  conacated value of  txtStartDate and txtEndDate  of second.aspx

    i have written code as follows but this is not working?

    what may be going wrong?

     


    Second Page
    protected void Button1_ServerClick(object sender, EventArgs e)-------------------------------------->"Go Button"
        {
            ListBox s = (ListBox)PreviousPage.FindControl("ListDate");
            s.Items.Add(new ListItem(txtStartDate.Text + "-" + txtEndDate.Text));

        }


    <input type="BUTTON" value="Go Back"
                onclick="history.go(-1)" id="Button1"   runat="server"  onserverclick=" Button1_ServerClick"/>

    Swati

    create template

    create site column

    create content type

    Add template and site columns to content type

    Add content type to document library



    ---------------------------------------
    public class Block : System.Web.UI.WebControls.WebParts.WebPart
    {
    string m_scriptBlock = "";
    string m_scriptKey = "scriptKey";

    //Key Property
    [Personalizable(PersonalizationScope.Shared), WebBrowsable(true),
    WebDisplayName("Script Key"),
    WebDescription("A unique key for the script.")]
    public string ScriptKey
    {
    get { return m_scriptKey; }
    set { m_scriptKey = value; }
    }

    //Script Property
    [Personalizable(PersonalizationScope.Shared), WebBrowsable(true),
    WebDisplayName("Script"),
    WebDescription("The JavaScript to insert in the page.")]
    public string Script
    {
    get { return m_scriptBlock; }
    set { m_scriptBlock = value; }
    }

    protected override void OnPreRender(EventArgs e)
    {
    base.OnPreRender(e);

    if (m_scriptBlock != "" &&
    !Page.ClientScript.IsClientScriptBlockRegistered(m_scriptKey))
    Page.ClientScript.RegisterClientScriptBlock(
    typeof(string), m_scriptKey, m_scriptBlock, true);

    }
    }

    -----------------------------------------






    protected override void CreateChildControls()
    {
    base.CreateChildControls();

    try
    {
    if (_userControl != defaultText)
    {
    _control = this.Page.LoadControl(_userControl);
    }
    else
    {
    _control = new LiteralControl(string.Format("To link to content, open the tool pane and then type a URL in the Link text box.", 1, 129, this.ID));
    }
    }
    catch (System.Exception ex)
    {
    _control = new LiteralControl(string.Format("Error: unable to load {0}
    Details: {1}", _userControl, ex.Message));
    }

    if (_control != null)
    {
    // Add to the Controls collection to support postback
    this.Controls.Add(_control);
    }
    }









    -------------------------------------------------------------------------
    opening the template inline in list view?

    Redirecting different content type to different folder



    C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES --for multiple sharepont apps


    ------------------------------------------------------
    For creating webPart automatic

    create blank project --> add new component --> webpart
    --------------------------------------------------------


    custom event Handler
    In class library

    orders.EventReceivers.Add
  • Re: Adding Items to Listbox of FirstWebform from Second Webform

    12-25-2006, 4:51 PM
    • Contributor
      2,507 point Contributor
    • omerkamal
    • Member since 02-06-2006, 2:47 PM
    • Germany
    • Posts 513

    The answer was a bit long so I posted on our website.

    please , see the link below

    http://www.dotnet-friends.com/Tutorials/ASP/TUTinASP58575dd0-7349-4741-9deb-3c6998bf16dd.aspx

     

    Happy Coding!

  • Re: Adding Items to Listbox of FirstWebform from Second Webform

    12-25-2006, 9:16 PM
    • Contributor
      4,382 point Contributor
    • jessjing
    • Member since 09-26-2006, 8:03 AM
    • Posts 872

    hi,

    don't use history.go() to do that, coz history.go() just track the url of the page you just visited. After the previous page postbacks, it is destroyed.

    so you want to access the ListBox in the previous page, it cannot find it.

    try to store the textbox' value in session and then use Response.Redirect to the previous page

    first page

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["text1"] != null && Session["text2"] != null)
            {
                ListBox1.Items.Add(Session["text1"].ToString());
                ListBox1.Items.Add(Session["text2"].ToString());
             
            }
           
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default4.aspx");
        }
    }

    second page

    public partial class Default4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          
        }

      
        protected void Goback_Click(object sender, EventArgs e)
        {
            Session["text1"] = TextBox1.Text;
            Session["text2"] = TextBox2.Text;
            Response.Redirect("Default.aspx");
        }
    }

  • Re: Adding Items to Listbox of FirstWebform from Second Webform

    12-26-2006, 1:15 AM
    • Participant
      1,353 point Participant
    • Swati Jain
    • Member since 05-24-2006, 12:29 PM
    • Pune
    • Posts 731

    First item is replaced with the second instead of getting added

     

    first page

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["text1"] != null && Session["text2"] != null)
            {
                ListBox1.Items.Add(Session["text1"].ToString());
                ListBox1.Items.Add(Session["text2"].ToString());
             
            }
           
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default4.aspx");
        }
    }


    second page

    public partial class Default4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          
        }

      
        protected void Goback_Click(object sender, EventArgs e)
        {
            Session["text1"] = TextBox1.Text;
            Session["text2"] = TextBox2.Text;
            Response.Redirect("Default.aspx");
        }
    }

     


     i have already tried that,

    But First item in the listbox is replaced with the second one ,

    As Postback occurs ,thats why i have tried with history.go(-1),

    Is there any other way to keep the items without replacing

    Swati

    create template

    create site column

    create content type

    Add template and site columns to content type

    Add content type to document library



    ---------------------------------------
    public class Block : System.Web.UI.WebControls.WebParts.WebPart
    {
    string m_scriptBlock = "";
    string m_scriptKey = "scriptKey";

    //Key Property
    [Personalizable(PersonalizationScope.Shared), WebBrowsable(true),
    WebDisplayName("Script Key"),
    WebDescription("A unique key for the script.")]
    public string ScriptKey
    {
    get { return m_scriptKey; }
    set { m_scriptKey = value; }
    }

    //Script Property
    [Personalizable(PersonalizationScope.Shared), WebBrowsable(true),
    WebDisplayName("Script"),
    WebDescription("The JavaScript to insert in the page.")]
    public string Script
    {
    get { return m_scriptBlock; }
    set { m_scriptBlock = value; }
    }

    protected override void OnPreRender(EventArgs e)
    {
    base.OnPreRender(e);

    if (m_scriptBlock != "" &&
    !Page.ClientScript.IsClientScriptBlockRegistered(m_scriptKey))
    Page.ClientScript.RegisterClientScriptBlock(
    typeof(string), m_scriptKey, m_scriptBlock, true);

    }
    }

    -----------------------------------------






    protected override void CreateChildControls()
    {
    base.CreateChildControls();

    try
    {
    if (_userControl != defaultText)
    {
    _control = this.Page.LoadControl(_userControl);
    }
    else
    {
    _control = new LiteralControl(string.Format("To link to content, open the tool pane and then type a URL in the Link text box.", 1, 129, this.ID));
    }
    }
    catch (System.Exception ex)
    {
    _control = new LiteralControl(string.Format("Error: unable to load {0}
    Details: {1}", _userControl, ex.Message));
    }

    if (_control != null)
    {
    // Add to the Controls collection to support postback
    this.Controls.Add(_control);
    }
    }









    -------------------------------------------------------------------------
    opening the template inline in list view?

    Redirecting different content type to different folder



    C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES --for multiple sharepont apps


    ------------------------------------------------------
    For creating webPart automatic

    create blank project --> add new component --> webpart
    --------------------------------------------------------


    custom event Handler
    In class library

    orders.EventReceivers.Add
  • Re: Adding Items to Listbox of FirstWebform from Second Webform

    12-26-2006, 9:09 PM
    Answer
    Hi,
    Session is a key-value pair collection, and the value added in the second page has the same key with the previous one, so the value will be replaced.
    Keep the items in a array, and store the whole array in the session.
    in page 1:
    if (Session["list"] != null)
    {
    ArrayList al = Session["al"] as ArrayList;
    for( int i = 0; i < al.Count; i++ )
    ListBox1.Items.Add(al[i]);
    }

    in page 2:
    ArrayList al;
    if (Session["list"] != null)
    {
    al = Session["al"] as ArrayList;
    }
    else
    {
    al = new ArrayList();
    Session["list"] = al;
    }
    al.Add("new item");
    Hope it helps.
Page 1 of 1 (5 items)