ClassyLinks
asked on
Set SelectedIndex on Dynamically Generated DropDownList
Hey folks!
I've got a very large datagrid, filled with client data to be edited, populated from tblClient.
In this grid, I've got 6 dropdownlists populated with selections from separate tables. How do I get the selected index of each dropdownlist to equal the value entered in the tblclient??
I tried "selecedIndex='<%#Containe r.DataItem ("FieldNam e")%> but that just gave me the second or third in the list, not the actual match.
Is that at all clear??
Thanks to all for your help!
I've got a very large datagrid, filled with client data to be edited, populated from tblClient.
In this grid, I've got 6 dropdownlists populated with selections from separate tables. How do I get the selected index of each dropdownlist to equal the value entered in the tblclient??
I tried "selecedIndex='<%#Containe
Is that at all clear??
Thanks to all for your help!
ASKER
Thanks Michael.
So, Would I put it in the BindGrid, or where I'm populating the dropdown??
for example, by ddl is populated with this:
'************************* ********** ********
Private Function BindState()
Dim myCommand As SqlCommand = New SqlCommand("sp_listareas_d isplay", Connect)
myCommand.CommandType = CommandType.StoredProcedur e
'myCommand.Parameters.Add( "@RegionId ", RegionID)
Connect.Open()
Return myCommand.ExecuteReader(Co mmandBehav ior.CloseC onnection)
End Function
Dim strCurrentState as Integer
Private Sub DG_ItemDataBound(s as object, e as DataGridItemEventArgs)
Dim cboStateTemp As DropDownList = CType(e.Item.FindControl(" ClientOrig inAreaID") , DropDownList)
Dim strTemp as String = cboStateTemp.SelectedItem. Value
Dim myDropDown as DropDownList
myDropDown = Ctype(e.Item.FindControl(" ClientOrig inAreaID") , DropDownList)
End Sub
'************************* ********** ********
Adding:
myDropDown.items.FindByVal ue(e.Item. DataItem(" ClientOrig inAreaID") ).selected = True
Just before the end of the sub doesn't have any impact.
Nor does putting the same code just before I bind the page. Where should I put it?
So, Would I put it in the BindGrid, or where I'm populating the dropdown??
for example, by ddl is populated with this:
'*************************
Private Function BindState()
Dim myCommand As SqlCommand = New SqlCommand("sp_listareas_d
myCommand.CommandType = CommandType.StoredProcedur
'myCommand.Parameters.Add(
Connect.Open()
Return myCommand.ExecuteReader(Co
End Function
Dim strCurrentState as Integer
Private Sub DG_ItemDataBound(s as object, e as DataGridItemEventArgs)
Dim cboStateTemp As DropDownList = CType(e.Item.FindControl("
Dim strTemp as String = cboStateTemp.SelectedItem.
Dim myDropDown as DropDownList
myDropDown = Ctype(e.Item.FindControl("
End Sub
'*************************
Adding:
myDropDown.items.FindByVal
Just before the end of the sub doesn't have any impact.
Nor does putting the same code just before I bind the page. Where should I put it?
Private Sub DG_ItemDataBound(s as object, e as DataGridItemEventArgs)
Dim cboStateTemp As DropDownList = CType(e.Item.FindControl(" ClientOrig inAreaID") , DropDownList)
Dim strTemp as String = cboStateTemp.SelectedItem. Value
mcboStateTemp.items.FindBy Value(e.It em.DataIte m("ClientO riginAreaI D")).selec ted = True
End Sub
Dim cboStateTemp As DropDownList = CType(e.Item.FindControl("
Dim strTemp as String = cboStateTemp.SelectedItem.
mcboStateTemp.items.FindBy
End Sub
cboStateTemp.items.FindByV alue(e.Ite m.DataItem ("ClientOr iginAreaID ")).select ed = True
or
cboStateTemp.items.FindByV alue(e.Ite m.cells[2] .Text).sel ected = True
if 2 is the index where clinetoriginalareaid in datagrid
or
cboStateTemp.items.FindByV
if 2 is the index where clinetoriginalareaid in datagrid
I prefer to do it cboStateTemp.SelectedIndex = cboStateTemp.Items.IndexOf (cboStateT emp.Items. FindByValu e(e.Item.c ells[2].Te xt))
The reason I prefer to do it this way is because if you do it like cboStateTemp.items.FindByV alue(e.Ite m.cells[2] .Text).sel ected = True and the e.Item.cells[2].Text is not found in the dropdownlist, it will throw an error. The way I list will not throw the error.
The reason I prefer to do it this way is because if you do it like cboStateTemp.items.FindByV
ASKER
I have tried as suggested above, but the selected index does not change. :(
does it throw any exception...
cboStateTemp.items.FindByV alue("any team id ").selected = True
hard code it for a while and see if that helps..
cboStateTemp.items.FindByV alue("2"). selected = True
cboStateTemp.items.FindByV
hard code it for a while and see if that helps..
cboStateTemp.items.FindByV
If you ever run FindByValue and it doesn't cause any errors, then it did at least something. (If it can't find that listitem, it'll let you know with a big fat exception) Are you positive you have the correct data in which to assign this ddl set?
Did you try both methods? Did you get an error thrown? Are you sure it is calling DG_ItemDataBound?
ASKER
I must have something totally wrong...because even "hard coding" doesn't have an effect.
make sure it is going to dg_itemdatabound....try it in debug mode...
cboStateTemp.items.FindByV alue(cboSt ateTemp.Se lectedItem .Value).se lected = false;
cboStateTemp.items.FindByV alue("any team id ").selected = True
cboStateTemp.items.FindByV
cboStateTemp.items.FindByV
You can turn trace mode on and use Trace.Write to figure out where it is getting. Can you post your entire dg_itemdatabound function if it is calling it?
you might also want to wrap your code in ItemDataBound with this, so you won't do this on header/footer items
If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then
'all your code
End If
If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then
'all your code
End If
ASKER
Ok....I don't know what you are referring to with trace mode.
Here is the dg_itemdatabound.....
'************************* ********** ********
Private Function BindState()
Dim myCommand As SqlCommand = New SqlCommand("sp_listareas_d isplay", Connect)
myCommand.CommandType = CommandType.StoredProcedur e
'myCommand.Parameters.Add( "@RegionId ", RegionID)
Connect.Open()
Return myCommand.ExecuteReader(Co mmandBehav ior.CloseC onnection)
End Function
Dim strCurrentState as Integer
Private Sub DG_ItemDataBound(s as object, e as DataGridItemEventArgs)
Dim cboStateTemp As DropDownList = CType(e.Item.FindControl(" ClientOrig inAreaID") , DropDownList)
Dim strTemp as String = cboStateTemp.SelectedItem. Value
cboStateTemp.items.FindByV alue(e.Ite m.DataItem ("ClientOr iginAreaID ")).select ed = True
Dim myDropDown as DropDownList
myDropDown = Ctype(e.Item.FindControl(" ClientOrig inAreaID") , DropDownList)
End Sub
'************************* ********** ********
And the ddl calls it here:
<asp:DropDownList
id="ClientOriginAreaID"
DataSource="<%# BindState() %>"
DataTextField="AREAName"
DataValueField="AREAID"
runat="server"/>
What am I missing??
Here is the dg_itemdatabound.....
'*************************
Private Function BindState()
Dim myCommand As SqlCommand = New SqlCommand("sp_listareas_d
myCommand.CommandType = CommandType.StoredProcedur
'myCommand.Parameters.Add(
Connect.Open()
Return myCommand.ExecuteReader(Co
End Function
Dim strCurrentState as Integer
Private Sub DG_ItemDataBound(s as object, e as DataGridItemEventArgs)
Dim cboStateTemp As DropDownList = CType(e.Item.FindControl("
Dim strTemp as String = cboStateTemp.SelectedItem.
cboStateTemp.items.FindByV
Dim myDropDown as DropDownList
myDropDown = Ctype(e.Item.FindControl("
End Sub
'*************************
And the ddl calls it here:
<asp:DropDownList
id="ClientOriginAreaID"
DataSource="<%# BindState() %>"
DataTextField="AREAName"
DataValueField="AREAID"
runat="server"/>
What am I missing??
You need to hook up the call to DG_ItemDataBound. Are you doing this? Are you using Visual Studio.NET?
Trace is a neat little feature that let's you insert debugging print statements
http://www.dotnetjunkies.com/quickstart/aspplus/doc/tracelogpage.aspx
http://www.dotnetjunkies.com/quickstart/aspplus/doc/tracelogpage.aspx
Private Function BindState()
i think bindstate should be either protected or public...
and is bindstate called.....
and it should eithr return dataset or datatable
i think bindstate should be either protected or public...
and is bindstate called.....
and it should eithr return dataset or datatable
ASKER
I have VS.net...but don't use it...never really got the hang of the interface...use text editor instead.
Making the function Protected or Public doesn't help.
bindState is called with the dropdownlist (called via the DataSource property)
What do you mean by: "You need to hook up the call to DG_ItemDataBound"??
The field is indeed being bound as the ddl is populated.....just cannot get the right value selected :(
Making the function Protected or Public doesn't help.
bindState is called with the dropdownlist (called via the DataSource property)
What do you mean by: "You need to hook up the call to DG_ItemDataBound"??
The field is indeed being bound as the ddl is populated.....just cannot get the right value selected :(
override protected void OnInit(EventArgs e)
{ this.DataGrid1.ItemDataBou nd += new System.Web.UI.WebControls. DataGridIt emEventHan dler(this. DataGrid1_ ItemDataBo und);
}
{ this.DataGrid1.ItemDataBou
}
ASKER
Where should I put that Praneetha??
in oninit method...
all this is automatically done when u use vs.net..so try using it...
oninit is called for every request
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.ItemDataBou nd += new System.Web.UI.WebControls. DataGridIt emEventHan dler(this. DataGrid1_ ItemDataBo und);
this.Button1.Click += new System.EventHandler(this.B utton1_Cli ck);
this.Load += new System.EventHandler(this.P age_Load);
}
all this is automatically done when u use vs.net..so try using it...
oninit is called for every request
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.ItemDataBou
this.Button1.Click += new System.EventHandler(this.B
this.Load += new System.EventHandler(this.P
}
Yep...that's what I meant. If you used VS.Net, it makes it simple to add it, that's why I asked, but the line above will work except of course you need to put in DB_ItemDataBound instead of this.DataGrid1_ItemDataBou nd.
ASKER
Should this be INSIDE a sub??
I keep getting
Name 'override' is not declared.
I keep getting
Name 'override' is not declared.
ASKER
OT....
This would have been Sooooooooooo easy in Classic ASP. .Net seems to make things so much more complex to do a simple little function.
<sigh>
This would have been Sooooooooooo easy in Classic ASP. .Net seems to make things so much more complex to do a simple little function.
<sigh>
does your class derive from
public class webpage1 : System.Web.UI.Page
{
//here override oninit...
do u have codebehind file...
webform.aspx.cs
public class webpage1 : System.Web.UI.Page
{
//here override oninit...
do u have codebehind file...
webform.aspx.cs
It should be inside your page class. I'm actually surprised you don't have it already. Are you not doing anything on load of the page? Are you using code behind or are you putting everything in the aspx page?
ASKER
everything is on the aspx page.
do you mean it should be in my Page_Load?? It still tells me override is not declared.
Sub Page_load(ByVal sender As System.Object, ByVal e As System.EventArgs)
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ContactGrid.ItemDataB ound += new System.Web.UI.WebControls. DataGridIt emEventHan dler(this. DG_ItemDat aBound);
//this.Button1.Click += new System.EventHandler(this.B utton1_Cli ck);
this.Load += new System.EventHandler(this.P age_Load);
}
RegionID = Session("RegionID")
Dim ID AS Integer
If Not IsPostBack then
BindDataGrid(ID)
end if
End Sub
do you mean it should be in my Page_Load?? It still tells me override is not declared.
Sub Page_load(ByVal sender As System.Object, ByVal e As System.EventArgs)
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ContactGrid.ItemDataB
//this.Button1.Click += new System.EventHandler(this.B
this.Load += new System.EventHandler(this.P
}
RegionID = Session("RegionID")
Dim ID AS Integer
If Not IsPostBack then
BindDataGrid(ID)
end if
End Sub
ok this is C# code..i guess it is diff in vb.net..sorry about that
No. It shouldn't be in your page_load. I think we were both under the assumption that you had a code behind page. I've actually never written a non-code behind page, but my first guess is to ignore the OnInit stuff and instead in your datagrid tag, add "onitemdatabound=DG_ItemDa taBound" I've never done that before though, so I don't really know if it'll work...
OT You may want to consider spending the time to get a hang of VS.NET. It REALLY cuts down on dev time and makes it much easier to set stuff like this up...
ASKER
ok....putting the onitemdatabound=DG_ItemDat aBound" in the grid at least moves it along to an error.
System.NullReferenceExcept ion: Object reference not set to an instance of an object.
Line 139:Public Sub DG_ItemDataBound(s as object, e as DataGridItemEventArgs)
Line 140: Dim cboStateTemp As DropDownList = CType(e.Item.FindControl(" ClientOrig inAreaID") , DropDownList)
Line 141: Dim strTemp as String = cboStateTemp.SelectedItem. Value
Line 142: cboStateTemp.items.FindByV alue(e.Ite m.DataItem ("ClientOr iginAreaID ")).select ed = True
System.NullReferenceExcept
Line 139:Public Sub DG_ItemDataBound(s as object, e as DataGridItemEventArgs)
Line 140: Dim cboStateTemp As DropDownList = CType(e.Item.FindControl("
Line 141: Dim strTemp as String = cboStateTemp.SelectedItem.
Line 142: cboStateTemp.items.FindByV
:Public Sub DG_ItemDataBound(s as object, e as DataGridItemEventArgs)
{
If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then
'all your code
End If
use that
{
If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then
'all your code
End If
use that
do you have a control on the page with id=ClientOriginAreaID runat=server?
Which line is bolded as the line of the error?
Which line is bolded as the line of the error?
ASKER
Praneetha...that gives me:
Exception Details: System.Web.HttpException: A DropDownList cannot have multiple items selected.
Public Sub DG_ItemDataBound(s as object, e as DataGridItemEventArgs)
If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then
Dim cboStateTemp As DropDownList = CType(e.Item.FindControl(" ClientOrig inAreaID") , DropDownList)
Dim strTemp as String = cboStateTemp.SelectedItem. Value
cboStateTemp.items.FindByV alue(e.Ite m.DataItem ("ClientOr iginAreaID ")).select ed = True
Dim myDropDown as DropDownList
myDropDown = Ctype(e.Item.FindControl(" ClientOrig inAreaID") , DropDownList)
End If
End Sub
Exception Details: System.Web.HttpException: A DropDownList cannot have multiple items selected.
Public Sub DG_ItemDataBound(s as object, e as DataGridItemEventArgs)
If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then
Dim cboStateTemp As DropDownList = CType(e.Item.FindControl("
Dim strTemp as String = cboStateTemp.SelectedItem.
cboStateTemp.items.FindByV
Dim myDropDown as DropDownList
myDropDown = Ctype(e.Item.FindControl("
End If
End Sub
Ignore my questions, praneetha hit it on the head!
Try using my way of selecting from way above...it won't give you that error...
ASKER
sorry muellerfan,
It was line 141, and yes, the ddl is called ClientOriginAreaID
It was line 141, and yes, the ddl is called ClientOriginAreaID
cboStateTemp.items.FindByV
cboStateTemp.items.FindByV
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
With both suggestions I get the same error:
Line 142: Dim strTemp as String = cboStateTemp.SelectedItem. Value
Exception Details: System.NullReferenceExcept ion: Object reference not set to an instance of an object.
Line 142: Dim strTemp as String = cboStateTemp.SelectedItem.
Exception Details: System.NullReferenceExcept
comment that link you don;t need it...
Dim strTemp as String = cboStateTemp.SelectedItem. Value
Dim strTemp as String = cboStateTemp.SelectedItem.
I don't think you need that line of code where you have it. What do you use strTemp for?
ASKER
No Joy.
No matter which way I slice it, still get Object reference not set to an instance of an object.
Public Function BindState()
Dim myCommand As SqlCommand = New SqlCommand("sp_listareas_d isplay", Connect)
myCommand.CommandType = CommandType.StoredProcedur e
'myCommand.Parameters.Add( "@RegionId ", RegionID)
Connect.Open()
Return myCommand.ExecuteReader(Co mmandBehav ior.CloseC onnection)
End Function
Dim strCurrentState as Integer
Public Sub DG_ItemDataBound(s as object, e as DataGridItemEventArgs)
' If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then
Dim cboStateTemp As DropDownList = CType(e.Item.FindControl(" ClientOrig inAreaID") , DropDownList)
'Dim strTemp as String = cboStateTemp.SelectedItem. Value
'cboStateTemp.items.FindBy Value(cboS tateTemp.S electedIte m.Value).s elected = False
'cboStateTemp.items.FindBy Value(e.It em.DataIte m("ClientO riginAreaI D")).selec ted = True
cboStateTemp.SelectedIndex = cboStateTemp.Items.IndexOf (cboStateT emp.Items. FindByValu e(e.Item.D ataItem("C lientOrigi nAreaID")) )
' Dim myDropDown as DropDownList
' myDropDown = Ctype(e.Item.FindControl(" ClientOrig inAreaID") , DropDownList)
' End If
End Sub
No matter which way I slice it, still get Object reference not set to an instance of an object.
Public Function BindState()
Dim myCommand As SqlCommand = New SqlCommand("sp_listareas_d
myCommand.CommandType = CommandType.StoredProcedur
'myCommand.Parameters.Add(
Connect.Open()
Return myCommand.ExecuteReader(Co
End Function
Dim strCurrentState as Integer
Public Sub DG_ItemDataBound(s as object, e as DataGridItemEventArgs)
' If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then
Dim cboStateTemp As DropDownList = CType(e.Item.FindControl("
'Dim strTemp as String = cboStateTemp.SelectedItem.
'cboStateTemp.items.FindBy
'cboStateTemp.items.FindBy
cboStateTemp.SelectedIndex
' Dim myDropDown as DropDownList
' myDropDown = Ctype(e.Item.FindControl("
' End If
End Sub
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That did it!
Brilliant
Now I can get it working for the other 5 ddl's and I'll be laughing.
Thanks to both for all your patience.
Brilliant
Now I can get it working for the other 5 ddl's and I'll be laughing.
Thanks to both for all your patience.
ASKER
before you run away....maybe there is an easier way.
Currently I have 6 identical functions the one above. I called them DG_ItemDataBound1, DG_ItemDataBound2 etc.
IF the datagrid is calling the DG_ItemDataBound in the onitemdatabound property, How can I have multple values in there so that it runs all 6 functions to populate all 6 ddls??
Currently I have 6 identical functions the one above. I called them DG_ItemDataBound1, DG_ItemDataBound2 etc.
IF the datagrid is calling the DG_ItemDataBound in the onitemdatabound property, How can I have multple values in there so that it runs all 6 functions to populate all 6 ddls??
The ItemDataBound event is raised after an item is data bound to the Datagrid control. This event provides you with the last opportunity to access the data item before it is displayed on the client. After this event is raised, the data item is nulled out and no longer available.(from msdn)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsdatalistclassitemdataboundtopic.asp
Dim cboStateTemp As DropDownList = CType(e.Item.FindControl(" ClientOrig inAreaID") , DropDownList)
Dim cboStateTemp1 As DropDownList = CType(e.Item.FindControl(" ClientOrig inAreaID1" ), DropDownList)
do that in the same function....
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsdatalistclassitemdataboundtopic.asp
Dim cboStateTemp As DropDownList = CType(e.Item.FindControl("
Dim cboStateTemp1 As DropDownList = CType(e.Item.FindControl("
do that in the same function....
i suggest you start using vs.net 2002 ...i am sure you will find lot of things simplified...
Sorry for the delay...I was at lunch. Yes, you can do as praneetha says and put all six dropdownlist sets in the same DG_ItemDataBound. Have you tried this yet? Does it work?
ASKER
This is great! Thanks to you both. For anyone checking out this question and wanting the final code, here is what I'm using:
6 dropdown lists populated this way:
'************************* ********** ********
Public Sub DG_ItemDataBound(s as object, e as DataGridItemEventArgs)
If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then
Dim cboStateTemp As DropDownList = CType(e.Item.FindControl(" ClientOrig inAreaID") , DropDownList)
cboStateTemp.SelectedIndex = cboStateTemp.Items.IndexOf (cboStateT emp.Items. FindByValu e(e.Item.D ataItem("C lientOrigi nAreaID")) )
Dim cboStateTemp2 As DropDownList = CType(e.Item.FindControl(" ClientReas on1ID"), DropDownList)
cboStateTemp2.SelectedInde x = cboStateTemp2.Items.IndexO f(cboState Temp2.Item s.FindByVa lue(e.Item .DataItem( "ClientRea son1ID")))
Dim cboStateTemp3 As DropDownList = CType(e.Item.FindControl(" ClientReas on2ID"), DropDownList)
cboStateTemp3.SelectedInde x = cboStateTemp3.Items.IndexO f(cboState Temp3.Item s.FindByVa lue(e.Item .DataItem( "ClientRea son2ID")))
Dim cboStateTemp4 As DropDownList = CType(e.Item.FindControl(" Client1stc hoiceAreaI D"), DropDownList)
cboStateTemp4.SelectedInde x = cboStateTemp4.Items.IndexO f(cboState Temp4.Item s.FindByVa lue(e.Item .DataItem( "Client1st choiceArea ID")))
Dim cboStateTemp5 As DropDownList = CType(e.Item.FindControl(" Client1stC hoiceResid enceType") , DropDownList)
cboStateTemp5.SelectedInde x = cboStateTemp5.Items.IndexO f(cboState Temp5.Item s.FindByVa lue(e.Item .DataItem( "Client1st ChoiceResi denceType" )))
Dim cboStateTemp6 As DropDownList = CType(e.Item.FindControl(" Client2ndC hoiceAreaI D"), DropDownList)
cboStateTemp6.SelectedInde x = cboStateTemp6.Items.IndexO f(cboState Temp6.Item s.FindByVa lue(e.Item .DataItem( "Client2nd ChoiceArea ID")))
Dim cboStateTemp7 As DropDownList = CType(e.Item.FindControl(" Client2ndC hoiceResid enceType") , DropDownList)
cboStateTemp7.SelectedInde x = cboStateTemp7.Items.IndexO f(cboState Temp7.Item s.FindByVa lue(e.Item .DataItem( "Client2nd ChoiceResi denceType" )))
End If
End Sub
'************************* ********** ********
'************************* ********** ********
Private Function BindState()
Dim myCommand As SqlCommand = New SqlCommand("sp_listareas_d isplay", Connect)
myCommand.CommandType = CommandType.StoredProcedur e
'myCommand.Parameters.Add( "@RegionId ", RegionID)
Connect.Open()
Return myCommand.ExecuteReader(Co mmandBehav ior.CloseC onnection)
End Function
'************************* ********** *******
'************************* ********** ********
Private Function BindState1()
Dim myCommand As SqlCommand = New SqlCommand("sp_listreason_ display", Connect)
myCommand.CommandType = CommandType.StoredProcedur e
'myCommand.Parameters.Add( "@RegionId ", RegionID)
Connect.Open()
Return myCommand.ExecuteReader(Co mmandBehav ior.CloseC onnection)
End Function
'************************* ********** ********
'************************* ********** ********
Private Function BindState2()
Dim myCommand As SqlCommand = New SqlCommand("sp_listrestype _display", Connect)
myCommand.CommandType = CommandType.StoredProcedur e
'myCommand.Parameters.Add( "@RegionId ", RegionID)
Connect.Open()
Return myCommand.ExecuteReader(Co mmandBehav ior.CloseC onnection)
End Function
'************************* ********** ********
Then each ddl calls the appropriate function in its datasource property like this:
<asp:dropdownlist
Runat="server"
DataSource="<%# BindState1() %>"
rows="1"
id="ClientReason1ID"
DataTextField="ReasonforLe aving"
DataValueField="ReasonID" />
Hope this helps the next person struggling with the same thing.
Cheers!
6 dropdown lists populated this way:
'*************************
Public Sub DG_ItemDataBound(s as object, e as DataGridItemEventArgs)
If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then
Dim cboStateTemp As DropDownList = CType(e.Item.FindControl("
cboStateTemp.SelectedIndex
Dim cboStateTemp2 As DropDownList = CType(e.Item.FindControl("
cboStateTemp2.SelectedInde
Dim cboStateTemp3 As DropDownList = CType(e.Item.FindControl("
cboStateTemp3.SelectedInde
Dim cboStateTemp4 As DropDownList = CType(e.Item.FindControl("
cboStateTemp4.SelectedInde
Dim cboStateTemp5 As DropDownList = CType(e.Item.FindControl("
cboStateTemp5.SelectedInde
Dim cboStateTemp6 As DropDownList = CType(e.Item.FindControl("
cboStateTemp6.SelectedInde
Dim cboStateTemp7 As DropDownList = CType(e.Item.FindControl("
cboStateTemp7.SelectedInde
End If
End Sub
'*************************
'*************************
Private Function BindState()
Dim myCommand As SqlCommand = New SqlCommand("sp_listareas_d
myCommand.CommandType = CommandType.StoredProcedur
'myCommand.Parameters.Add(
Connect.Open()
Return myCommand.ExecuteReader(Co
End Function
'*************************
'*************************
Private Function BindState1()
Dim myCommand As SqlCommand = New SqlCommand("sp_listreason_
myCommand.CommandType = CommandType.StoredProcedur
'myCommand.Parameters.Add(
Connect.Open()
Return myCommand.ExecuteReader(Co
End Function
'*************************
'*************************
Private Function BindState2()
Dim myCommand As SqlCommand = New SqlCommand("sp_listrestype
myCommand.CommandType = CommandType.StoredProcedur
'myCommand.Parameters.Add(
Connect.Open()
Return myCommand.ExecuteReader(Co
End Function
'*************************
Then each ddl calls the appropriate function in its datasource property like this:
<asp:dropdownlist
Runat="server"
DataSource="<%# BindState1() %>"
rows="1"
id="ClientReason1ID"
DataTextField="ReasonforLe
DataValueField="ReasonID" />
Hope this helps the next person struggling with the same thing.
Cheers!
thisDDL.items.FindByValue(
Hope this gets you started,
--Michael