I have several template columns inside of a datagrid.
Inside of these template columns are databound listboxes:
<asp:TemplateCo lumn HeaderText="Cre w Chiefs">
<ItemTemplate >
<asp:listbox AutoPostBack="F alse"
BackColor="#fff fff" id="lstCrewChie f" runat="server"
Rows="1" DataSource="<%# DsCrewChief1 %>" Enabled="True"
SelectionMode=" Single" DataTextField=" UserName"
DataValueField= "UserName" />
</asp:listbox>
</ItemTemplate>
</asp:TemplateCol umn>
<asp:TemplateCo lumn HeaderText="EMT 's/Drivers">
<ItemTemplate >
<asp:listbox AutoPostBack="F alse"
BackColor="#fff fff" id="lstEMTDrive r" runat="server"
Rows="1" DataSource="<%# DsEMTDriver1 %>" Enabled="True"
SelectionMode=" Single" DataTextField=" UserName"
DataValueField= "UserName" />
</asp:listbox>
</ItemTemplate>
</asp:TemplateCol umn>
<asp:TemplateCo lumn HeaderText="Rid ers">
<ItemTemplate >
<asp:listbox AutoPostBack="F alse"
BackColor="#fff fff" id="lstRider" runat="server" Rows="1"
DataSource="<%# DsRider1 %>" Enabled="True"
SelectionMode=" Multiple" DataTextField=" UserName"
DataValueField= "UserName" />
</asp:listbox>
</ItemTemplate>
</asp:TemplateCol umn>
I would like to do 2 things with these listboxes: One, is
to set the selected value in the listbox to the values I
retrieve in the database. The second, is to set the
selected values in the listboxes to values before I write
the data to the database.
I'm confused about the ITEMDatabound event of the
datagrid. Is this event used to get the values of the
listboxes BEFORE I write the data to the database (which I
think it is)? If so, is there an event I need to use to
SET the values in the listboxes to the values I retrieved
from the database? If there is no event, how do I go about
setting the values of the listboxes?
Also, how do I set and retrieve values from a listbox that
can have multiple selections...
Below is the code I have for the ItemDataBound
event.......... .
Private Sub DataGrid1_ItemD ataBound(ByVal sender As
Object, ByVal e As
System.Web.UI.W ebControls.Data GridItemEventAr gs) Handles
DataGrid1.ItemD ataBound
'Get the correct values for the listboxes
If e.Item.ItemType = ListItemType.It em Then
Dim lstCrewChiefTem p As ListBox
Dim lstEMTsDriverTe mp As ListBox
Dim lstRiderTemp As ListBox
lstCrewChiefTem p = e.Item.FindCont rol
("lstCrewChief" )
lstCrewChiefTem p.SelectedIndex =
lstCrewChiefTem p.Items.IndexOf
(lstCrewChiefTe mp.Items.FindBy Value(e.Item.Da taItem
("Username") ))
lstEMTsDriverTe mp = e.Item.FindCont rol
("lstEMTDriver" )
lstEMTsDriverTe mp.SelectedInde x =
lstEMTsDriverTe mp.Items.IndexO f
(lstEMTsDriverT emp.Items.FindB yValue(e.Item.D ataItem
("Username") ))
lstRiderTemp = e.Item.FindCont rol("lstRider")
lstRiderTemp.Se lectedIndex =
lstRiderTemp.It ems.IndexOf(lst RiderTemp.Items .FindByValue
(e.Item.DataIte m("Username") ))
End If
End Sub
Thanks,
Bill.
Inside of these template columns are databound listboxes:
<asp:TemplateCo lumn HeaderText="Cre w Chiefs">
<ItemTemplate >
<asp:listbox AutoPostBack="F alse"
BackColor="#fff fff" id="lstCrewChie f" runat="server"
Rows="1" DataSource="<%# DsCrewChief1 %>" Enabled="True"
SelectionMode=" Single" DataTextField=" UserName"
DataValueField= "UserName" />
</asp:listbox>
</ItemTemplate>
</asp:TemplateCol umn>
<asp:TemplateCo lumn HeaderText="EMT 's/Drivers">
<ItemTemplate >
<asp:listbox AutoPostBack="F alse"
BackColor="#fff fff" id="lstEMTDrive r" runat="server"
Rows="1" DataSource="<%# DsEMTDriver1 %>" Enabled="True"
SelectionMode=" Single" DataTextField=" UserName"
DataValueField= "UserName" />
</asp:listbox>
</ItemTemplate>
</asp:TemplateCol umn>
<asp:TemplateCo lumn HeaderText="Rid ers">
<ItemTemplate >
<asp:listbox AutoPostBack="F alse"
BackColor="#fff fff" id="lstRider" runat="server" Rows="1"
DataSource="<%# DsRider1 %>" Enabled="True"
SelectionMode=" Multiple" DataTextField=" UserName"
DataValueField= "UserName" />
</asp:listbox>
</ItemTemplate>
</asp:TemplateCol umn>
I would like to do 2 things with these listboxes: One, is
to set the selected value in the listbox to the values I
retrieve in the database. The second, is to set the
selected values in the listboxes to values before I write
the data to the database.
I'm confused about the ITEMDatabound event of the
datagrid. Is this event used to get the values of the
listboxes BEFORE I write the data to the database (which I
think it is)? If so, is there an event I need to use to
SET the values in the listboxes to the values I retrieved
from the database? If there is no event, how do I go about
setting the values of the listboxes?
Also, how do I set and retrieve values from a listbox that
can have multiple selections...
Below is the code I have for the ItemDataBound
event.......... .
Private Sub DataGrid1_ItemD ataBound(ByVal sender As
Object, ByVal e As
System.Web.UI.W ebControls.Data GridItemEventAr gs) Handles
DataGrid1.ItemD ataBound
'Get the correct values for the listboxes
If e.Item.ItemType = ListItemType.It em Then
Dim lstCrewChiefTem p As ListBox
Dim lstEMTsDriverTe mp As ListBox
Dim lstRiderTemp As ListBox
lstCrewChiefTem p = e.Item.FindCont rol
("lstCrewChief" )
lstCrewChiefTem p.SelectedIndex =
lstCrewChiefTem p.Items.IndexOf
(lstCrewChiefTe mp.Items.FindBy Value(e.Item.Da taItem
("Username") ))
lstEMTsDriverTe mp = e.Item.FindCont rol
("lstEMTDriver" )
lstEMTsDriverTe mp.SelectedInde x =
lstEMTsDriverTe mp.Items.IndexO f
(lstEMTsDriverT emp.Items.FindB yValue(e.Item.D ataItem
("Username") ))
lstRiderTemp = e.Item.FindCont rol("lstRider")
lstRiderTemp.Se lectedIndex =
lstRiderTemp.It ems.IndexOf(lst RiderTemp.Items .FindByValue
(e.Item.DataIte m("Username") ))
End If
End Sub
Thanks,
Bill.
Comment