Results 1 to 5 of 5

Thread: DDL_SelectedIndexChanged event

  1. #1

    Thread Starter
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    DDL_SelectedIndexChanged event

    When I change the index of a dropdownlist programmatically, the list's SelectedIndexChanged event doesn't fire, when I do it manually it does. Is there a way to force the event programmatically?

  2. #2
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Re: DDL_SelectedIndexChanged event

    yeah
    Code:
    ddl.ClearSelection();
    ddl.SelectedIndex = 1; //be sure to clear selected first. it will throw an error
    ddl_selChange(dll, EventArgs.Empty);
    but, I believe the event should fire on it's own it seems to do it for me
    Magiaus

    If I helped give me some points.

  3. #3
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Re: DDL_SelectedIndexChanged event

    Ideally, whatever code you have within the selectedIndexChanged event handler would call another procedure to do the work (work procedure). To do so programatically then means you just call the work procedure.

    Code:
    private void InitializeComponent()
    		{    
    			this.Button1.Click += new System.EventHandler(this.SubmitClickHandler);
    			this.Load += new System.EventHandler(this.Page_Load);
    			this.dropdownlist1.SelectedIndexChanged +=new EventHandler(DropDownList1SelectedIndexChanged);
    
    		}
    		#endregion
    
    		private void SubmitClickHandler(object sender, System.EventArgs e)
    		{
    			int i = 3;
    
    			//for this example, always true, and calls the worker function
    			if (i==3)
    				DoTheWork();
    		}
    
    		private void DropDownList1SelectedIndexChanged(object sender, EventArgs e)
    		{
    			//call the worker function
    			DoTheWork();
    		}
    
    		private void DoTheWork()
    		{
                //do the work here
    		}

  4. #4

    Thread Starter
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: DDL_SelectedIndexChanged event

    Well, I've pulled my hair out all day trying to convert this and that with some success. But, I still can not figure this out, and it's driving me bonkers.
    Here's some code I used to test:

    VB Code:
    1. Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
    2.         cboSubYesNo.ClearSelection()
    3.         cboSubYesNo.SelectedIndex = 1
    4.     End Sub
    5.  
    6.     Private Sub cboSubYesNo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboSubYesNo.SelectedIndexChanged
    7.         MessageBox("I'm Here")
    8.         SubYesNo_PopulateData()
    9.     End Sub

    When I click the button, the value changes, but I get no message box. When I manually change the value, I get the message box. I'm totally stumped. All help appreciated.

  5. #5
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267

    Re: DDL_SelectedIndexChanged event

    I was wrong the event does not fire.

    method 1
    Code:
    Public Class _Default
        Inherits System.Web.UI.Page
        Protected WithEvents ddl As System.Web.UI.WebControls.DropDownList
        Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    
    #Region " Web Form Designer Generated Code "
    
        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    
        End Sub
    
        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub
    
    #End Region
    
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If Not IsPostBack Then
                Dim i As Integer = 0
                For i = 0 To 20
                    ddl.Items.Add(i.ToString())
                Next
            End If
        End Sub
    
        Private Sub ddl_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddl.SelectedIndexChanged
            Response.Write("sel changed")
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ddl.ClearSelection()
            ddl.Items(5).Selected = True
            ddl_SelectedIndexChanged(ddl, EventArgs.Empty)
        End Sub
    End Class
    Method 2
    Code:
    Public Class _Default
        Inherits System.Web.UI.Page
        Protected WithEvents ddl As System.Web.UI.WebControls.DropDownList
        Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    
    #Region " Web Form Designer Generated Code "
    
        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    
        End Sub
    
        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub
    
    #End Region
    
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If Not IsPostBack Then
                Dim i As Integer = 0
                For i = 0 To 20
                    ddl.Items.Add(i.ToString())
                Next
            End If
        End Sub
    
        Private Sub ddl_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddl.SelectedIndexChanged
            ddlChange()
        End Sub
        Private Sub ddlChange()
            Response.Write("sel changed")
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ddl.ClearSelection()
            ddl.Items(5).Selected = True
            ddlChange()
        End Sub
    End Class
    Both ways acomplish the same thing.

    Method 1 is better because there is no chance of not firing all the code that should fire.

    Method 2 is the better because it is the correct way to implement code that needs to be excecuted in more than one location.

    If I was working solo I would go with method 2, if I was working with a group of prgrammers I would go with method 1. I would go with Method 1 in a team dev because a lot of times some one will come in make a change and not fully examine all the code; thus you run the risk of having some behavior added directly to the event and not your event method. For a dll especialy it doesn't matter because the EventArgs are useless so it is fine to pass in empty.

    Note also that in team dev you could use method 2 and simply place a comment to work with the ddlChange Method...... stylistic chocies suck.....
    Magiaus

    If I helped give me some points.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width