ASP.Net Custom UserControl Error: AddressForm1 does not contain a definition for Street

Last Reply 4 months ago By pandeyism

Posted 4 months ago

Error :

Server Error in '/' Application.


Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS1061: 'AddressControl.AddressForm1' does not contain a definition for 'Street' and no extension method 'Street' accepting a first argument of type 'AddressControl.AddressForm1' could be found (are you missing a using directive or an assembly reference?) Source Error:

 
Line 8:      protected void btnSubmit_Click(object Sender,EventArgs e)
Line 9:      {
Line 10:         ltlResult.Text = AddressForm1.Street;
Line 11:     }
Line 12: </script>

Source File: c:\Users\ZENITHH\Documents\Visual Studio 2017\Projects\AddressControl\AddressControl\CheckOut.aspx    Line: 10          

The code for  AddressForm Control  . File name : AddresForm.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AddressForm.ascx.cs" Inherits="AddressControl.AddressForm1" %>

<script runat="server">
    public string Title
    {
        get { return ltlTitle.Text; }
        set { ltlTitle.Text = value; }
    }
    public string Street
    {
        get { return txtStreet.Text; }
        set { txtStreet.Text = value; }
    }
    public string State
    {
        get { return txtState.Text; }
        set { txtState.Text = value; }
    }
</script>
<fieldset>
    <legend>
         <asp:Literal ID="ltlTitle" Text="Address Form" runat="server" />
    </legend>
    <div class="addressLabel">
        <asp:Label id="lblstreet" Text="Street" runat="server" />
    </div>
    <div class="addressField">
        <asp:TextBox ID="txtStreet" runat="server" />
        <asp:RegularExpressionValidator ID="reqStreet" Text="(Required)" ControlToValidate="txtStreet" runat="server" />
        <br class="clear" />        
        <div class="addressLabel">
            <asp:Label ID="lblState" text="State" AccociatedControlID="txtState" runat ="server" />
            <asp:TextBox ID="txtState" runat="server" />
           <asp:RequiredFieldValidator ID="reqState" Text="(Required)" ControlToValidate="txtState" runat="server" />
        </div>
    </div>
</fieldset>
</script>

And the file that is using the contol file name :-  Checkout.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CheckOut.aspx.cs" Inherits="AddressControl.CheckOut" %>
<%@ Register TagPrefix="User" TagName="AddressForm" Src="~/AddressForm.ascx" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<script runat="server">
    protected void btnSubmit_Click(object Sender,EventArgs e)
    {
        ltlResult.Text = AddressForm1.Street;
    }
</script>
<head runat="server">
    <title>Address Form</title>
</head>
 
<body>
    <form id="form1" runat="server">
        <div>
            <user:AddressForm ID="AddressForm1" title ="Billing Address" runat ="server" />
            <br />
            <asp:Button Id="btnSubmit" Text="Submit Form" onclick="btnSubmit_Click" runat="server" />
            <asp:Literal ID="ltlResult" runat="server" />
        </div>
    </form>
</body>
</html>

Need to help to solve this . 

You are viewing reply posted by: pandeyism 4 months ago.
Posted 4 months ago Modified on 4 months ago

Hi arpitv,

Refer below code is working

HTML

AddresForm.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="AddresForm.ascx.cs"
    Inherits="WebUserControl" %>
<script runat="server">
    public string Title
    {
        get { return ltlTitle.Text; }
        set { ltlTitle.Text = value; }
    }
    public string Street
    {
        get { return txtStreet.Text; }
        set { txtStreet.Text = value; }
    }
    public string State
    {
        get { return txtState.Text; }
        set { txtState.Text = value; }
    }
</script>
<fieldset>
    <legend>
        <asp:Literal ID="ltlTitle" Text="Address Form " runat="server" />
    </legend>
    <div class="addressField">
        <asp:Label ID="lblstreet" Text="Street" runat="server" />
        <asp:TextBox ID="txtStreet" runat="server" />
        <asp:RequiredFieldValidator ID="reqStreet" Text="(Required)" ControlToValidate="txtStreet"
            ForeColor="Red" runat="server" />
        <br class="clear" />
    </div>
    <div class="addressLabel">
        <asp:Label ID="lblState" Text="State" AccociatedControlID="txtState" runat="server" />
        <asp:TextBox ID="txtState" runat="server" />
        <asp:RequiredFieldValidator ID="reqState" Text="(Required)" ControlToValidate="txtState"
            ForeColor="Red" runat="server" />
    </div>
</fieldset>

Checkout.aspx

<%@ Register TagPrefix="User" TagName="AddressForm" Src="~/AddresForm.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script runat="server">
        protected void btnSubmit_Click(object Sender, EventArgs e)
        {
            ltlResult.Text = "Street Name is " + AddressForm1.Street;
            Literal1.Text = "State Name is " + AddressForm1.State;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <User:AddressForm ID="AddressForm1" Title="Billing Address" runat="server" />
        <br />
        <asp:Button ID="btnSubmit" Text="Submit Form" OnClick="btnSubmit_Click" runat="server" />
        <br />
        <asp:Literal ID="ltlResult" runat="server" />
        <br />
        <asp:Literal ID="Literal1" runat="server" />
    </div>
    </form>
</body>
</html>

Screenshot