locked
embedded controls RRS feed

  • Question

  • User-1047007741 posted

    hi, i have 3 user controls. The first is a dropdownlist (A). The second (B) contains the dropdownlist (A). The third (C) contains some validators which need to be enabled/disabled based on the value selected in the dropdownlist (on B). How can i achieve this? Ive created a property to set the validator.enabled to true/false in control (C). I've tried getting the value of the dropdownlist through a property in (B) and in that property setting the property of C to true/false. It doesnt work. Im thinking i need to set the validator property (C) in the selectedindexchanged property of A...but how do i link the evnt of  A to B? if anyone has a simple way of doing this please help! thanks :)

    Saturday, May 17, 2008 9:07 AM

Answers

  • User853727733 posted

     Ok, I think I got it. Actually is kind of simple, you'll have to expose an event in your control that replicates the SelectIndexChanged of the dropdownlist. I built a demo control and page to illustrate:

    Control:

    <%@ Control Language="C#" ClassName="UCExposeEvent" %>

    <script runat="server">

       
       
        public event EventHandler IndexChanged;  
       
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IndexChanged != null)
            {
                IndexChanged(sender, e);
            }
        }
    </script>

    <asp:DropDownList ID="DropDownList1" runat="server"
        onselectedindexchanged="DropDownList1_SelectedIndexChanged"
        AutoPostBack="True">
        <asp:ListItem>one</asp:ListItem>
        <asp:ListItem>two</asp:ListItem>
        <asp:ListItem>three</asp:ListItem>
    </asp:DropDownList>

    Demo page:

    <%@ Page Language="C#" %>

    <%@ Register src="UCExposeEvent.ascx" TagName="UCExposeEvent" TagPrefix="uc1" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void UCExposeEvent1_SelectedIndexChanged(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("it works");
        }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <br />
            <uc1:UCExposeEvent ID="UCExposeEvent1" OnIndexChanged="UCExposeEvent1_SelectedIndexChanged"
                runat="server" />
        </div>
        </form>
    </body>
    </html>
     

    Hope it helps. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 17, 2008 10:41 AM
  • User853727733 posted

     What you want is that your uc1 has an event that will fire when you change the dropdownlist, the only problem is that you don't have access to the inner event so you have to expose it. It's like you are propagating the event of the dropdownlist.

    Your first control will be like this:

    //this will create the event in you control
    public event EventHandler IndexChanged;  
    private TermsEdit _TermsEditControl;
    public TermsEdit TermsEditControl
    
    {
    
    get
    
    {
    if (_TermsEditControl == null)
    
    {
    _TermsEditControl = new TermsEdit();
    
    }
    return _TermsEditControl;
    
    }
    set { _TermsEditControl = value; }
    
    }
    public void RequireTerms()
    
    {
    if (DropDownListUse.SelectedIndex == 1)
    
    { TermsEditControl.RequireTermsValidation = true; }
    
    else
    { TermsEditControl.RequireTermsValidation = false; }
    
    }
    protected void DropDownListUse_SelectedIndexChanged(object sender, EventArgs e)
    
    {
    
    RequireTerms();
            //this will fire the event to propagate the SelectIndexChanged
            if (IndexChanged != null)
            {
                IndexChanged(sender, e);
            }
    
    }
    Since this user control is inside another control this will have to have the same event. In the end you'll use the new IndexChanged event you created to change the Property of you validation user control.
    Does this helps you? I hope so.
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 17, 2008 11:35 AM

All replies

  • User853727733 posted

     I didn't understand the situation completely but if you post your code I'll be glad to help!

     

    Saturday, May 17, 2008 9:36 AM
  • User-1047007741 posted

    my codes in a bit of a mess roght now, but this is what i have:

    UserControl 3: (Name: TermsEdit; This contains the property that enables/disables the validators)

    public bool RequireValidation

    {

    set

    {

    RequiredFieldValidatorT1.Enabled = value;

    RequiredFieldValidatorT2.Enabled = value;

    RequiredFieldValidatorT3.Enabled = value;

    RequiredFieldValidatorT4.Enabled = value;

    RequiredFieldValidatorT1End.Enabled = value;

    RequiredFieldValidatorT2End.Enabled = value;

    RequiredFieldValidatorT3End.Enabled = value;

    RequiredFieldValidatorT4End.Enabled = value;

    LabelAsteriskT1.Visible = value;

    LabelAsteriskT2t.Visible = value;

    LabelAsteriskT3.Visible = value;

    LabelAsteriskT4.Visible = value;

    LabelAsteriskT1End.Visible = value;

    LabelAsteriskT2End.Visible = value;

    LabelAsteriskT3End.Visible = value;LabelAsteriskT4End.Visible = value;

    }

    }

     

    UserControl 2: (Name: DropDownListUse);This is the dropdownlist control...its a user control that has its own datasource...i made it a usercontrol cos i use it in many places)

    private TermsEdit _TermsEditControl;
    public TermsEdit TermsEditControl

    {

    get

    {

    if (_TermsEditControl == null)

    {

    _TermsEditControl =
    new TermsEdit();

    }

    return _TermsEditControl;

    }

    set { _TermsEditControl = value; }

    }

    public void RequireTerms()

    {

    if (DropDownListUse.SelectedIndex == 1)

    { TermsEditControl.RequireTermsValidation = true; }

    else

    { TermsEditControl.RequireTermsValidation = false; }

    }

    protected void DropDownListUse_SelectedIndexChanged(object sender, EventArgs e)

    {

    RequireTerms();

    }

     

    UserControl 1: (Name: Details);This is the control that contains some textboxes and Usercontrol 2 i.e. the custom dropdownlist.....When the user selects an item with index 1, i need the validators on usercontrol3 to be enabled. If any other index is selected i want the validators

    to be disabled. The problem im having is how do i access the selectedindexchanged event of usercontrol 2 from usercontrol1? to summarise, uc2 is embedded in uc1 and i need to set a property of uc3 when the selectedindex changes on uc2 which is within uc1.....i know this sounds complicated....struggling to explain it but hope this helps

    Saturday, May 17, 2008 10:00 AM
  • User853727733 posted

     Ok, I think I got it. Actually is kind of simple, you'll have to expose an event in your control that replicates the SelectIndexChanged of the dropdownlist. I built a demo control and page to illustrate:

    Control:

    <%@ Control Language="C#" ClassName="UCExposeEvent" %>

    <script runat="server">

       
       
        public event EventHandler IndexChanged;  
       
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IndexChanged != null)
            {
                IndexChanged(sender, e);
            }
        }
    </script>

    <asp:DropDownList ID="DropDownList1" runat="server"
        onselectedindexchanged="DropDownList1_SelectedIndexChanged"
        AutoPostBack="True">
        <asp:ListItem>one</asp:ListItem>
        <asp:ListItem>two</asp:ListItem>
        <asp:ListItem>three</asp:ListItem>
    </asp:DropDownList>

    Demo page:

    <%@ Page Language="C#" %>

    <%@ Register src="UCExposeEvent.ascx" TagName="UCExposeEvent" TagPrefix="uc1" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void UCExposeEvent1_SelectedIndexChanged(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("it works");
        }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <br />
            <uc1:UCExposeEvent ID="UCExposeEvent1" OnIndexChanged="UCExposeEvent1_SelectedIndexChanged"
                runat="server" />
        </div>
        </form>
    </body>
    </html>
     

    Hope it helps. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 17, 2008 10:41 AM
  • User-1047007741 posted

    hi, thanks for this, but im still confused. I dont know what should go where. Arent i supposed to be exposing the event from 1 control to another? where would i put my code to set the propery of the validators? sorry, but im new to events....if u could just explain a bit how this exposing works thatd be awesome.thanks:)

    Saturday, May 17, 2008 11:06 AM
  • User853727733 posted

     What you want is that your uc1 has an event that will fire when you change the dropdownlist, the only problem is that you don't have access to the inner event so you have to expose it. It's like you are propagating the event of the dropdownlist.

    Your first control will be like this:

    //this will create the event in you control
    public event EventHandler IndexChanged;  
    private TermsEdit _TermsEditControl;
    public TermsEdit TermsEditControl
    
    {
    
    get
    
    {
    if (_TermsEditControl == null)
    
    {
    _TermsEditControl = new TermsEdit();
    
    }
    return _TermsEditControl;
    
    }
    set { _TermsEditControl = value; }
    
    }
    public void RequireTerms()
    
    {
    if (DropDownListUse.SelectedIndex == 1)
    
    { TermsEditControl.RequireTermsValidation = true; }
    
    else
    { TermsEditControl.RequireTermsValidation = false; }
    
    }
    protected void DropDownListUse_SelectedIndexChanged(object sender, EventArgs e)
    
    {
    
    RequireTerms();
            //this will fire the event to propagate the SelectIndexChanged
            if (IndexChanged != null)
            {
                IndexChanged(sender, e);
            }
    
    }
    Since this user control is inside another control this will have to have the same event. In the end you'll use the new IndexChanged event you created to change the Property of you validation user control.
    Does this helps you? I hope so.
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 17, 2008 11:35 AM
  • User-1047007741 posted
    thanks, this helps alot. Just a question, what eventargs do i pass? Do i need to specify any?
    Sunday, May 18, 2008 7:09 AM
  • User853727733 posted

     In the example I provided you I pass the EventArgs of the inner event but you don't need to specify any thing, you can just pass new EventArgs() or even null, that's up to you.

    Sunday, May 18, 2008 10:28 AM
  • User-1047007741 posted
    thanks alot :)
    Sunday, May 18, 2008 1:32 PM