|
-
Apr 27th, 2005, 12:47 PM
#1
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?
-
Apr 27th, 2005, 01:20 PM
#2
Frenzied Member
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.
-
Apr 27th, 2005, 01:22 PM
#3
I wonder how many charact
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
}
-
Apr 27th, 2005, 05:56 PM
#4
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:
Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
cboSubYesNo.ClearSelection()
cboSubYesNo.SelectedIndex = 1
End Sub
Private Sub cboSubYesNo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboSubYesNo.SelectedIndexChanged
MessageBox("I'm Here")
SubYesNo_PopulateData()
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.
-
Apr 27th, 2005, 09:54 PM
#5
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|