[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1080
  • Last Modified:

asp.net c# usercontrols

I have developed website that have mulitple steps to ask some questions.
for examples, step 1 asked for billing or shipping address
step 2 enter the address
step 3 review
step 4 insert all data into database
my issue is. I may have a lot of the different type of addresses
and how can I take the data such as shipping address, city, state, and zip, and how do i know which type of address?
0
riskyricky1972
Asked:
riskyricky1972
  • 2
1 Solution
 
gauravjoshiCommented:
Hi,

You could define business entities for each type of address, assuming you have different tables to store address, or you can create one address bussiness object with field 'type' of it.

use object to save and retrive data from the database, using formview control and object data source control

http://msdn2.microsoft.com/en-us/library/aa581783.aspx
follow the above link for formview and object data source  binding
Hope this helps

Gaurav Joshi
0
 
sanjeewajCommented:
Hi
Your data model should be defined in a way to support this operation.

You can define one Address table and a AddressType look up table.  In the ASP .NET page allow the user to select the address type.  

Is there any difficulty of allowing the user to select address type?  It looks that you need to detect the address type based on the values of addres data.

Sanjeewa Jayasinghe
0
 
Tom KnowltonWeb developerCommented:
Here is an .ASCX (web control) that I developed that distinguishes between KIND of address.

Perhaps it will help you.  Quite a bit of it uses custom controls I wrote, so it's usefullness is more in viewing the flow of how it was done and just being able to see one possible approach rather than being a "plug-and-play" component.
//ASPX page:
 
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="AddressInfo.ascx.cs" Inherits="adminusr__controls_AddressInfo" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<%@ Register Src="../../members/beta/scripts/controls/AllStates/AllStates.ascx" TagName="AllStates"
    TagPrefix="uc1" %>
 
<table>
<tr>
	<th colspan="2">
        <asp:Label ID="LabelHeader" runat="server" Text="Address Info:"></asp:Label>
    </th>
</tr>
<tr>
	<td width="25%">Full Name</td>
	<td width="75%">
		<asp:TextBox ID="fullname" runat="server"></asp:TextBox>&nbsp;<asp:RequiredFieldValidator
            ID="RequiredFieldValidator1" runat="server" ControlToValidate="fullname" Display="Dynamic"
            ErrorMessage="*Name Required" ValidationGroup="localvalidationgroup">*</asp:RequiredFieldValidator><span style="font-style:italic;">e.g.
                John Doe</span>
        </td>
</tr>
<tr>
	<td>Address Line 1</td>
	<td>
		<asp:TextBox ID="addressline1" runat="server"></asp:TextBox>&nbsp;<asp:RequiredFieldValidator
            ID="RequiredFieldValidator2" runat="server" ControlToValidate="addressline1"
            Display="Dynamic" ErrorMessage="*Address Required" ValidationGroup="localvalidationgroup">*</asp:RequiredFieldValidator><span style="font-style:italic;">e.g.
                123 Fake St</span>
	</td>
</tr>
<tr>
	<td>Address Line 2</td>
	<td>
		<asp:TextBox ID="addressline2" runat="server"></asp:TextBox>&nbsp;<span style="font-style:italic;">e.g.
            Apt. 36</span>
	</td>
</tr>
<tr>
	<td>City</td>
	<td>
		<asp:TextBox ID="city" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="city"
            Display="Dynamic" ErrorMessage="*City Required" ValidationGroup="localvalidationgroup">*</asp:RequiredFieldValidator></td>
</tr>
<tr>
	<td>State</td>
	<td><uc1:AllStates ID="AllStates" runat="server" /></td>
</tr>
<tr>
	<td>Zip</td>
	<td>
		<asp:TextBox ID="zip" runat="server"></asp:TextBox>&nbsp;<asp:RequiredFieldValidator
            ID="RequiredFieldValidator4" runat="server" ControlToValidate="zip" Display="Dynamic"
            ErrorMessage="*Zip Required" ValidationGroup="localvalidationgroup">*</asp:RequiredFieldValidator><span style="font-style:italic;">e.g.
                12345</span>
	</td>
</tr>
</table>
<cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender1" runat="server" TargetControlID="RequiredFieldValidator1">
</cc1:ValidatorCalloutExtender>
<cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender2" runat="server" TargetControlID="RequiredFieldValidator2">
</cc1:ValidatorCalloutExtender>
<cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender3" runat="server" TargetControlID="RequiredFieldValidator3">
</cc1:ValidatorCalloutExtender>
<cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender4" runat="server" TargetControlID="RequiredFieldValidator4">
</cc1:ValidatorCalloutExtender>
<cc1:MaskedEditExtender ID="MaskedEditExtender1" runat="server" TargetControlID="fullname" MaskType="None" Mask="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" Filtered="1234567890. ">
</cc1:MaskedEditExtender>
<cc1:MaskedEditExtender ID="MaskedEditExtender2" runat="server" TargetControlID="addressline1" MaskType="None" Mask="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" Filtered="1234567890. ">
</cc1:MaskedEditExtender>
<cc1:MaskedEditExtender ID="MaskedEditExtender3" runat="server" TargetControlID="addressline2" MaskType="None" Mask="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" Filtered="1234567890. ">
</cc1:MaskedEditExtender>
<cc1:MaskedEditExtender ID="MaskedEditExtender4" runat="server" TargetControlID="city" MaskType="None" Mask="AAAAAAAAAAAAAAAAAAAAA" Filtered="1234567890. ">
</cc1:MaskedEditExtender>
<cc1:MaskedEditExtender ID="MaskedEditExtender5" runat="server" TargetControlID="zip" MaskType="Number" Mask="99999">
</cc1:MaskedEditExtender>
 
 
 
 
 
 
 
//CODE BEHIND:
 
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;
using System.Data.SqlClient;
 
 
 
public partial class adminusr__controls_AddressInfo : System.Web.UI.UserControl, ICommonWebControlDBMethods
{
    public string _headerText;
       
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!Page.IsPostBack)
        //{
        //    FillAllStatesControl();
        //}
    }
 
    public string GetFullName
    {
        get { return this.fullname.Text.Trim(); }
    }
 
    public string GetAddressLine1
    {
        get { return this.addressline1.Text.Trim(); }
    }
 
    public string GetAddressLine2
    {
        get { return this.addressline2.Text.Trim(); }
    }
 
    public string GetCity
    {
        get { return this.city.Text.Trim(); }
    }
 
    public string GetState
    {
        get { return this.AllStates.GetStateSelected().ToString().Trim(); }
    }
 
    public string GetZip
    {
        get { return this.zip.Text.Trim(); }
    }
 
    [Browsable(true),
    Category("Header"),
    Description("Caption for Address Info Control")]
    public string AddressInfoHeaderText
    {
        get
        {
            return _headerText;
        }
        set
        {
            _headerText = value.Trim();
            this.LabelHeader.Text = _headerText;
        }
    }
 
    private CommonFunctions.AddressType _mailingAddress = CommonFunctions.AddressType.Physical;
 
    [Browsable(true),
    Category("Select Mailing Address Type"),
    Description("Select the mailing address type you want")]
    public CommonFunctions.AddressType MailingAddressType
    {
        get { return _mailingAddress; }
        set { _mailingAddress = value; }
    }
 
    public void AddNewRecord(string username, string connStringName)
    {
        string userID = CommonFunctions.GetUserIdGivenUserName(username);
        string sqlStatement = "INSERT INTO aspnet_MailingAddress (UserID, OccupantName, MailingAddressLine1, MailingAddressLine2, City, State, Zip, AddressType) VALUES ('" + userID + "', '" + this.fullname.Text.Trim() +"', '" + this.addressline1.Text.Trim() + "', '" + this.addressline2.Text.Trim() + "', '" + this.city.Text.Trim() + "', '" + this.AllStates.GetStateSelected().Trim() + "', '" + this.zip.Text.Trim() + "', " + (int)_mailingAddress + ")";
        CommonFunctions.RunSQLCommand(connStringName, sqlStatement);
    }
 
    public string GetUniqueIDForControl()
    {
        return this.GetUniqueIDForControl();
    }
 
 
    public void AddNewRecordToInfoWizardTempTable(string username, string connStringName)
    {
        //TempData1 is FullName
        //TempData2 is MailingAddressLine1
        //TempData3 is MailingAddressLine2
        //TempData4 is City
        //TempData5 is State
        //TempData6 is Zip
        //TempData7 is AddressType
 
        string ControlName = this.GetUniqueIDForControl();
        string userID = CommonFunctions.GetUserIdGivenUserName(username);
        string sqlStatement = "INSERT INTO aspnet_InfoWizardTempTable (UserID, ControlName, TempData1, TempData2, TempData3, TempData4, TempData5, TempData6, TempData7) VALUES ('" + userID + "', '" + ControlName + "', '" + this.fullname.Text.Trim() + "', '" + this.addressline1.Text.Trim() + "', '" + this.addressline2.Text.Trim() + "', '" + this.city.Text.Trim() + "', '" + this.AllStates.GetStateSelected().Trim() + "', '" + this.zip.Text.Trim() + "', '" + ((int)_mailingAddress).ToString() + "')";
        CommonFunctions.RunSQLCommand(connStringName, sqlStatement);
    }
 
    public void UpdateExistingRecord(string username, string connStringName, int Type)
    {
        string userID = CommonFunctions.GetUserIdGivenUserName(username);
        string sqlStatement = "UPDATE aspnet_MailingAddress SET OccupantName = '" + this.fullname.Text.Trim() + "', MailingAddressLine1 = '" + this.addressline1.Text.Trim() + "', MailingAddressLine2 = '" + this.addressline2.Text.Trim() + "', City = '" + this.city.Text.Trim() + "', State = '" + this.AllStates.GetStateSelected().Trim() + "', Zip = '" + this.zip.Text.Trim() + "' WHERE UserId = '" + userID + "' and AddressType = " + Type;
        int numRecs = CommonFunctions.RunSQLCommand(connStringName, sqlStatement);
    }
 
    public void UpdateExistingRecordInInfoWizardTempTable(string username, string connStringName, int atype)
    {
        //TempData1 is FullName
        //TempData2 is MailingAddressLine1
        //TempData3 is MailingAddressLine2
        //TempData4 is City
        //TempData5 is State
        //TempData6 is Zip
        //TempData7 is AddressType
 
        string ControlName = this.GetUniqueIDForControl();
        string userID = CommonFunctions.GetUserIdGivenUserName(username);
        string sqlStatement = "UPDATE aspnet_InfoWizardTempTable SET TempData1 = '" + this.fullname.Text.Trim() + "', TempData2 = '" + this.addressline1.Text.Trim() + "', TempData3 = '" + this.addressline2.Text.Trim() + "', TempData4 = '" + this.city.Text.Trim() + "', TempData5 = '" + this.AllStates.GetStateSelected().Trim() + "', TempData6 = '" + this.zip.Text.Trim() + "' WHERE UserId = '" + userID + "' and ControlName = '" + ControlName + "' and TempData7 = '" + atype.ToString() + "'";
        int numRecs = CommonFunctions.RunSQLCommand(connStringName, sqlStatement);
    }
 
    public void FillFields(string username, string connStringName, int atype)
    {
        string userID = CommonFunctions.GetUserIdGivenUserName(username);
        string sqlStatement = "select OccupantName, MailingAddressLine1, MailingAddressLine2, City, State, Zip, AddressType from aspnet_MailingAddress where UserId = '" + userID + "' and AddressType = " + atype;
        DataSet ds = CommonFunctions.ReturnDataSet(connStringName, sqlStatement);
 
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            this.fullname.Text = dr["OccupantName"].ToString().Trim();
            this.addressline1.Text = dr["MailingAddressLine1"].ToString().Trim();
            this.addressline2.Text = dr["MailingAddressLine2"].ToString().Trim();
            this.city.Text = dr["City"].ToString().Trim();
            this.AllStates.SetState(dr["State"].ToString().Trim());
            this.zip.Text = dr["Zip"].ToString().Trim();
        }
    }
 
    public void FillFieldsFromInfoWizardTempTable(string username, string connStringName, int atype)
    {
        string ControlName = this.GetUniqueIDForControl();
        string userID = CommonFunctions.GetUserIdGivenUserName(username);
        string sqlStatement = "select TempData1, TempData2, TempData3, TempData4, TempData5, TempData6 from aspnet_InfoWizardTempTable where UserId = '" + userID + "' and ControlName = '" + ControlName + "' and TempData7 = '" + (atype).ToString() + "'";
        DataSet ds = CommonFunctions.ReturnDataSet(connStringName, sqlStatement);
 
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            this.fullname.Text = dr["TempData1"].ToString().Trim();
            this.addressline1.Text = dr["TempData2"].ToString().Trim();
            this.addressline2.Text = dr["TempData3"].ToString().Trim();
            this.city.Text = dr["TempData4"].ToString().Trim();
            string state = dr["TempData5"].ToString().Trim();
            this.AllStates.SetState(dr["TempData5"].ToString().Trim());
            this.zip.Text = dr["TempData6"].ToString().Trim();
        }
    }
        
    public void FillAddressInfoGivenAddressType(string username, string connStringName, CommonFunctions.AddressType tempAddressType)
    {
        string userID = CommonFunctions.GetUserIdGivenUserName(username);
        string sqlStatement = "select OccupantName, MailingAddressLine1, MailingAddressLine2, City, State, Zip, AddressType from aspnet_MailingAddress where UserId = '" + userID + "' and AddressType = " + (int)tempAddressType;
        DataSet ds = CommonFunctions.ReturnDataSet(connStringName, sqlStatement);
 
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            this.fullname.Text = dr["OccupantName"].ToString().Trim();
            this.addressline1.Text = dr["MailingAddressLine1"].ToString().Trim();
            this.addressline2.Text = dr["MailingAddressLine2"].ToString().Trim();
            this.city.Text = dr["City"].ToString().Trim();
            this.AllStates.SetState(dr["State"].ToString().Trim());
            this.zip.Text = dr["Zip"].ToString().Trim();
        }
    }
 
    public void FillAddressInfoFromInfoWizardTempTableGivenAddressType(string username, string connStringName, CommonFunctions.AddressType tempAddressType)
    {
        string ControlName = this.GetUniqueIDForControl();
        string userID = CommonFunctions.GetUserIdGivenUserName(username);
        string sqlStatement = "select TempData1, TempData2, TempData3, TempData4, TempData5, TempData6 from aspnet_InfoWizardTempTable where UserId = '" + userID + "' and ControlName = '" + ControlName + "' and TempData7 = '" + ((int)tempAddressType).ToString() + "'";
        DataSet ds = CommonFunctions.ReturnDataSet(connStringName, sqlStatement);
 
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            this.fullname.Text = dr["TempData1"].ToString().Trim();
            this.addressline1.Text = dr["TempData2"].ToString().Trim();
            this.addressline2.Text = dr["TempData3"].ToString().Trim();
            this.city.Text = dr["TempData4"].ToString().Trim();
            this.AllStates.SetState(dr["TempData5"].ToString().Trim());
            this.zip.Text = dr["TempData6"].ToString().Trim();
        }
    }
 
    public void FillAllStatesControl()
    {
        this.AllStates.FillDropDownList();
    }
 
    public string GetControlUniqueID()
    {
        return this.GetUniqueIDForControl();
    }
 
 
    public void ClearFields()
    {
        this.fullname.Text = "";
        this.addressline1.Text = "";
        this.addressline2.Text = "";
        this.city.Text = "";        
        this.zip.Text = "";
    }
 
    public void MakeReadOnly(bool IsReadOnly)
    {
        CommonFunctions.SetControlReadOnly(ref this.fullname, IsReadOnly);
        CommonFunctions.SetControlReadOnly(ref this.addressline1, IsReadOnly);
        CommonFunctions.SetControlReadOnly(ref this.addressline2, IsReadOnly);
        CommonFunctions.SetControlReadOnly(ref this.city, IsReadOnly);        
        CommonFunctions.SetControlReadOnly(ref this.zip, IsReadOnly);        
    }
    
    public InfoForValidation ValidationResults()
    {
        string messages = "";
        bool passedFullNameValidation = (this.fullname.Text.Trim().Length > 0);
        if (passedFullNameValidation == false)
        {
            messages += "Name is required<br>";
        }
        bool passedAddressLine1Validation = this.addressline1.Text.Trim().Length > 0 ? true : false;
        if (passedAddressLine1Validation == false)
        {
            messages += "Address is required<br>";
        }
        //Note:  Address Line 2 is not required, so we don't validate it
        bool passedCityValidation = this.city.Text.Trim().Length > 0 ? true : false;
        if (passedCityValidation == false)
        {
            messages += "City is required<br>";
        }
        bool passedZipValidation = CommonFunctions.RegExValidator(this.zip.Text.Trim(), CommonFunctions.RegExZip);
        if (passedZipValidation == false)
        {
            messages += "Zip is required<br>";
        }
 
        InfoForValidation ifv = new InfoForValidation("PersonalInfo.ascx.cs", messages, (passedFullNameValidation & passedAddressLine1Validation & passedCityValidation & passedZipValidation));
 
        return ifv;
    }
 
    public string GetRequiredFieldForSave()
    {
        return this.fullname.Text.Trim();
    }
 
}

Open in new window

0
 
Tom KnowltonWeb developerCommented:
Thank you.
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now