ASP.NET

Moderators: None (Apply to moderate this forum)
Number of threads: 1727
Number of posts: 3292

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Cannot get the value and textbox and save it to viewstate Posted by silencer07 on 25 Jul 2008 at 10:07 AM
i dont know why but i cant get the values of my textbox and save it to viewstate. please help me i badly needed it. Please scan my code and tell me what i have screwed up. Thank you

i designed my page using expression studio and coded the logic using visual studio professional edition

1. i want to get the value of my asp:textbox text values
2. i want to save those information to a "Supplier object" so i can retrieve them later when i am about to insert it at database
3. when the user pressed upload, his inputs should be retained in those textboxes

for some reason i cant attach my register.aspx file but this is my cs for that

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 System.IO;
using System.Runtime;

public partial class register_register : System.Web.UI.Page
{
    supplier Supplier;
    string businessPermitPath;
    string mayorsPermitPath;
    protected void Page_Load(object sender, EventArgs e)
    {
        this.EnableViewState = true;
        if (ViewState["supplier"] == null)
        {
            Supplier = new supplier();
            ViewState["supplier"] = Supplier;
            Response.Write("supplier==null");
        }
        if (this.IsPostBack)
        {
            Response.Write("postback=true");
            Supplier = (supplier)ViewState["supplier"];
            txt_CompanyName.Text = Supplier.CompanyName;
            txt_Address.Text = Supplier.Address;
            txt_Username.Text = Supplier.UserName;
            txt_Password.Text = Supplier.Password;
            txt_Contact1.Text = Supplier.ContactPerson1;
            txt_Email1.Text = Supplier.Email1;
            txt_Number1.Text = Supplier.Number1;
            txt_Contact2.Text = Supplier.ContactPerson2;
            txt_Email2.Text = Supplier.Email2;
            txt_Number2.Text = Supplier.Number2;
        }
    }
    protected void Page_PreRender(Object sender, EventArgs e)
    {
        // Persist variables.
        ViewState["supplier"] = Supplier;
        Response.Write("supplier persisted");
    }

    protected void UploadBussinessPermit_Click(object sender, EventArgs e)
    {
        if ((business_permit.PostedFile != null) && (business_permit.PostedFile.ContentLength > 0))
        {
            

            if (Supplier.CompanyName == null || Supplier.CompanyName == "")
                Response.Write("<br>i have no company name</br>");
            else
                Response.Write("<br>meron akong company name</br>");

            string fn = System.IO.Path.GetFileName(business_permit.PostedFile.FileName);
            System.IO.Directory.CreateDirectory(Server.MapPath("../Data\\") + Supplier.CompanyName);
            businessPermitPath = Server.MapPath("../Data") + "\\" + fn;
            try
            {
                if (File.Exists(businessPermitPath))
                    lbl_businesspermit.Text = "*Please Specify a Different File Name";
                else
                {
                    business_permit.PostedFile.SaveAs(businessPermitPath);
                    lbl_businesspermit.Text = "*Upload Successful(" + fn +")";
                }
            }
            finally {  }
        }
        else
        {
            lbl_businesspermit.Text="*No File Selected";
        }

        Supplier = (supplier)ViewState["supplier"];
        Supplier.CompanyName = txt_CompanyName.Text;

        Response.Write("txt_CompanyName.Text is" + txt_CompanyName.Text);

        Supplier.Address = txt_Address.Text;
        Supplier.UserName = txt_Username.Text;
        Supplier.Password = txt_Password.Text;
        Supplier.ContactPerson1 = txt_Contact1.Text;
        Supplier.Email1 = txt_Email1.Text;
        Supplier.Number1 = txt_Number1.Text;
        Supplier.ContactPerson2 = txt_Contact2.Text;
        Supplier.Email2 = txt_Email2.Text;
        Supplier.Number2 = txt_Number2.Text;
        ViewState["supplier"] = Supplier;

        supplier temp = (supplier)ViewState["supplier"];
        if (temp.CompanyName == null || temp.CompanyName == "")
            Response.Write("<br>i have no company name in businesspermit  </br>");
        else
            Response.Write("<br>i do havecompany name in businesspermit</br>");

        //saveUserInputs(Supplier);
    }
    protected void UploadMayorsPermit_Click(object sender, EventArgs e)
    {
        Supplier = (supplier)ViewState["*supplier"];
        if ((mayors_permit.PostedFile != null) && (mayors_permit.PostedFile.ContentLength > 0))
        {
            string fn = System.IO.Path.GetFileName(mayors_permit.PostedFile.FileName);
            System.IO.Directory.CreateDirectory(Server.MapPath("../Data" ) + Supplier.CompanyName);
            mayorsPermitPath = Server.MapPath("../Data") + "\\" + fn;
            try
            {
                if (File.Exists(mayorsPermitPath))
                    lbl_mayorspermit.Text = "*Please Specify a Different File Name";
                else
                {
                    mayors_permit.PostedFile.SaveAs(mayorsPermitPath);
                    lbl_mayorspermit.Text = "**Upload Successful("+fn+")";
                }
            }
            finally { }
        }
        else
        {
            lbl_mayorspermit.Text = "*No File Selected";
        }
        
        saveUserInputs(Supplier);
    }
    protected void btn_Register_Click(object sender, EventArgs e)
    {

    }

    private void saveUserInputs(supplier arg)
    {
        arg.CompanyName = txt_CompanyName.Text;
        arg.Address = txt_Address.Text;
        arg.UserName = txt_Username.Text;
        arg.Password = txt_Password.Text;
        arg.ContactPerson1 = txt_Contact1.Text;
        arg.Email1 = txt_Email1.Text;
        arg.Number1 = txt_Number1.Text;
        arg.ContactPerson2 = txt_Contact2.Text;
        arg.Email2 = txt_Email2.Text;
        arg.Number2 = txt_Number2.Text;
        ViewState["supplier"] = arg;
        supplier temp = (supplier)ViewState["supplier"];
        if (temp.CompanyName == null || temp.CompanyName == "")
            Response.Write("<br>i have no company name</br>");
        else
            Response.Write("<br>i do have company name</br>");
    }
}
Report
Re: Cannot get the value and textbox and save it to viewstate Posted by bradwang on 3 Aug 2008 at 7:50 PM
If ViewState cannot work out properly, why not using Session instead?

I'm Brad Wang...
.NET Freelancer from China
MSN: brad_wang_cn@hotmail.com
Skype: brad_wang



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.