Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting details from a user control

Status
Not open for further replies.

dazza12345

Programmer
Nov 25, 2005
35
GB
Hi all, ive just started using user controls. Im trying to create a user control wthat will allow me to insert a date (i.e. with three drop down lists, one for the day, one for the month and one for the year).

Ive included the user control into muy .aspx file succesfully (by dragging the user control file into the location i want it on the page).

How do I go about getting the values selected in each of the drop down boxes is the user control when I click the 'Save' button in my .aspx file?

The Id of the user control is Date1/

Cheers
 
Make public properties on the user control so that they are exposed to anything that needs to get their values (and set their values with the dropdownlist values).


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hi, ive added 3 public variable, that are set when the dropdown menu is selected. (see folloing code)

Code:
public partial class Date : System.Web.UI.MobileControls.MobileUserControl
{
    public string Day;
    public string Month;
    public string Year;

    protected void day_SelectedIndexChanged(object sender, EventArgs e)
    {
        Day = day.Text;
    }
    protected void month_SelectedIndexChanged(object sender, EventArgs e)
    {
        Month = month.Text;
    }
    protected void year_SelectedIndexChanged(object sender, EventArgs e)
    {
        Year = year.Text;
    }
}

However, when trying to access these vairables in my .aspx file, they cannot be found.

Any ideas?

Also, what if I need to add multiple date user controls on the same page. Will the Date public variable (specified in the date user control) not be overwritten?

Cheers
 
Make public properties on the user control
You've made public strings not public properties.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Sorry about that. Ive changed them to be public properties, however, i still cant access them from my .aspx file.

Code from the date.ascx.cs file:
Code:
public partial class Date : System.Web.UI.MobileControls.MobileUserControl
{
    public String Day
    {
        get
        {
            return day.Text;
        }
    }

    public String Month
    {
        get
        {
            return month.Text;
        }
    }

    public String Year
    {
        get
        {
            return year.Text;
        }
    }

}
 
Strange, works fine for me. e.g

User Control:
Code:
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="WebUserControl.ascx.vb" Inherits="WebUserControl" %>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="1" Value="1" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem Text="2" Value="2" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server">
<asp:ListItem Text="3" Value="3" Selected="True"></asp:ListItem>
</asp:DropDownList>

User Control - Code Behind:
Code:
Partial Class WebUserControl
    Inherits System.Web.UI.UserControl

    Public ReadOnly Property DDL1() As String
        Get
            Return DropDownList1.SelectedItem.Value
        End Get
    End Property

    Public ReadOnly Property DDL2() As String
        Get
            Return DropDownList2.SelectedItem.Value
        End Get
    End Property

    Public ReadOnly Property DDL3() As String
        Get
            Return DropDownList3.SelectedItem.Value
        End Get
    End Property
End Class

Web Page:
Code:
<%@ Page Language="VB" MasterPageFile="~/PipelineTracker.master" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" title="Untitled Page" %>
<%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <uc1:WebUserControl ID="WebUserControl1" runat="server" />
</asp:Content>

Web Page - Code Behind
Code:
Partial Class Default2
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.Write(WebUserControl1.DDL1)
        Response.Write(WebUserControl1.DDL2)
        Response.Write(WebUserControl1.DDL3)
    End Sub
End Class


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top