Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Cannot get the value and textbox and save it to viewstate

silencer07silencer07 Posts: 1Member
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

[code]
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("
i have no company name");
else
Response.Write("
meron akong company name");

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("
i have no company name in businesspermit ");
else
Response.Write("
i do havecompany name in businesspermit");

//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("
i have no company name");
else
Response.Write("
i do have company name");
}
}
[/code]

Comments

  • bradwangbradwang Posts: 197Member
    If ViewState cannot work out properly, why not using Session instead?

    I'm Brad Wang...
    .NET Freelancer from China
    MSN: [email protected]
    Skype: brad_wang
Sign In or Register to comment.