PDA

View Full Version : ASP.NET DropDownList binding issue in a DataList


gtsikes
04-26-2004, 09:10 PM
Greetings,

I have a DataList Web Server control. The control displays some ADO.NET data. I have an itemtemplete that formats each row of data in a typical HTML table format. One field, section_description, was a DropDownList selected item text from a separate data entry page. The database table stores the selected value as an integer with FK references to a machine_section table. The machine_section table has fields section_id and section_description. The itemtemplete shows the section_description at runtime.

<td><%# DataBinder.Eval(Container.DataItem, "section_description") %></td>

However, in DataList edit mode. I want a DropDownList to show with the proper SelectedItemIndex for the value retrieved for that row.

When I try to bind to the dropdown list control in the EditItemTemplate it gives me a null referenece saying the id of the DropDownList does not exist.

How I can I overcome this?

allida77
04-27-2004, 10:17 PM
When I try to bind to the dropdown list control in the EditItemTemplate it gives me a null referenece saying the id of the DropDownList does not exist.

Can you post that code?

gtsikes
04-28-2004, 09:23 PM
The key was using the DataBinder class within the asp:dropdown tag itself.

Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
Dim strMachine as string = Request.QueryString("machine")
if not ispostback then
BindData()
lblTitle.text = "UCD Repair History for " & strMachine.ToUpper
end if
End Sub

sub BindData
Dim DBConn as System.Data.Odbc.OdbcConnection
Dim DBAdapter as System.Data.Odbc.OdbcDataAdapter
Dim DBAdapter2 as System.Data.Odbc.OdbcDataAdapter
Dim DSPageData as New DataSet
Dim strMachine as string = Request.QueryString("machine")
Dim SqlQuery as string

'---------------------
'Std Connection Script
'---------------------
Dim connectionString As String
Dim user As String = System.Configuration.ConfigurationSettings. _
AppSettings("SqlUID")
Dim pass As String = System.Configuration.ConfigurationSettings. _
AppSettings("SqlPWD")
connectionString = "DSN=WebSQL;DATABASE=ucd;APP=Microsoft® Visual Studio .NET;WSID=UWSIK" & _
"ESJ01;Network=DBMSSOCN"
connectionString &= ";UID=" & user
connectionString &= ";PWD=" & pass

'---------------------
'Script End
'---------------------
DBConn = New OdbcConnection(connectionString)

SqlQuery = "select r.complete, r.created_by, convert(varchar(10),r.created_date,101) created_date, _
r.edited_by, r.machine_id, r.repair_category, _
r.repair_comment, convert(varchar(10),r.repair_date,101) repair_date, r.repair_desc, r.repair_id, r.repaired_by, r.section_id, _
s.section_description _
from dbo.mach_repair r, dbo.mach_sections s _
where r.section_id = s.section_id and _
r.machine_id = '" & Request.QueryString("machine") & "' _
order by r.repair_id desc"

DBAdapter = New OdbcDataAdapter(SqlQuery, DBConn)
DBAdapter.Fill(DSPageData, "RepairList_All")
dgRepairs.DataSource= _
DSPageData.Tables("RepairList_All").DefaultView
dgRepairs.DataBind

'---------------------
'ddlMachineSection items
'---------------------start
DBAdapter2 = New OdbcDataAdapter("select section_description, section_id from mach_sections", DBConn)
DBAdapter2.Fill(DSPageData, "MachineSections")
'---------------------end
Session("dataset") = DSPageData
lblTitle.text = "UCD Repair History for " & strMachine.ToUpper
DBConn.Close()
end sub

By setting the Session("dataset") in the BindData sub, I can recall the dataset from the dataadapter in the dropdownlist tag within the edititemtemplate of the datalist. The following is the dropdownlist code followed by the entire datalist code.

<asp:dropdownlist
id="ddlMachineSection"
runat="server"
DataSource= '<%# CType(Session("dataset"), DataSet).tables("MachineSections").defaultview %>'
DataTextField="section_description"
DataValueField="section_id"
SelectedValue='<%# DataBinder.Eval(Container.DataItem, "section_id") %>'
/>


Datalist code

<asp:Datalist
id="dgRepairs"
runat="server"
BorderColor="black"
CellPadding="5"
CellSpacing="5"
OnItemCommand="Item_Command"
OnEditCommand="Edit_Command"
OnUpdateCommand="Update_Command"
OnCancelCommand="Cancel_Command"
RepeatDirection="Vertical"
RepeatColumns="1"
RepeatLayout="Table"
ShowBorder="True">
<ItemStyle BackColor="Silver" />
<AlternatingItemStyle BackColor="DarkGray" />
<SelectedItemStyle BackColor="Yellow" />
<HeaderTemplate>
<asp:Image id="SeparatorImage"
ImageUrl="../Reports/img/MechanoLj7.gif"
runat="server"/>
</HeaderTemplate>
<itemtemplate>
<table BORDER=2>
<tr><thead>
<th>Repair ID</th>
<th>Date Entered</th>
<th>Date Complete</th>
<th>Repaired By</th>
<th>Machine Section</th>
<th>Repair Type</th>
<th>Compelete</th>
<th>&nbsp;</th>
</tr></thead>
<tr>
<td><asp:Label id="N_repair_id"
Text='<%# DataBinder.Eval(Container.DataItem, "repair_id") %>'
runat="server"/>
</td>
<td><%# DataBinder.Eval(Container.DataItem, "created_date") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "repair_date") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "repaired_by") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "section_description") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "repair_category") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "complete") %></td>
<td><asp:LinkButton id="EditButton" Text="Edit" CommandName="Edit" runat="server"/></td>
</tr>
<tr><thead>
<th colspan=4>Repair Description</th>
<th colspan=4>Repair Comments</th>
</tr></thead>
<tr>
<td colspan=4><%# DataBinder.Eval(Container.DataItem, "repair_desc") %></td>
<td colspan=4><%# DataBinder.Eval(Container.DataItem, "repair_comment") %></td>
</tr>
</table>
</itemtemplate>
<SeparatorTemplate>
<asp:Image id="SeparatorImage"
ImageUrl="../Reports/img/MechanoLj7.gif"
runat="server"/>
</SeparatorTemplate>
<EditItemTemplate>
<table BORDER=2 >
<tr><thead>
<th>Repair ID</th>
<th>Date Entered</th>
<th>Date Complete</th>
<th>Repaired By</th>
<th>Machine Section</th>
<th>Repair Type</th>
<th>Compelete</th>
<th>&nbsp;</th>
</tr></thead>
<tr>
<td><asp:Label id="E_repair_id"
Text='<%# DataBinder.Eval(Container.DataItem, "repair_id") %>'
runat="server"/>
</td>
<td><asp:Textbox id="E_created_date" columns=10
Text='<%# DataBinder.Eval(Container.DataItem, "created_date") %>'
runat="server"/>
</td>
<td><asp:TextBox id="E_repair_date" columns=10
Text='<%# DataBinder.Eval(Container.DataItem, "repair_date") %>'
runat="server"/>
</td>
<td><asp:TextBox id="E_repaired_by" columns=10
MaxLength="32"
Text='<%# DataBinder.Eval(Container.DataItem, "repaired_by") %>'
runat="server"/>
</td>
<td><asp:dropdownlist
id="ddlMachineSection"
runat="server"
DataSource= '<%# CType(Session("dataset"), DataSet).tables("MachineSections").defaultview %>'
DataTextField="section_description"
DataValueField="section_id"
SelectedValue='<%# DataBinder.Eval(Container.DataItem, "section_id") %>'
/>
</td>
<td ><asp:LinkButton id="UpdateButton"
Text="Update"
CommandName="Update"
runat="server"/>
<asp:LinkButton id="CancelButton"
Text="Cancel"
CommandName="Cancel"
CausesValidation="False"
runat="server"/></td>
</tr>
<tr><thead>
<th colspan=4>Repair Description</th>
<th colspan=4>Repair Comments</th>
</tr></thead>
<tr>
<td colspan=4><asp:TextBox id="E_repair_desc"
MaxLength="255"
columns=30
rows="3"
TextMode="MultiLine"
Text='<%# DataBinder.Eval(Container.DataItem, "repair_desc") %>'
runat="server"/>
</td>
<td colspan=4>
<asp:TextBox id="E_repair_comment"
MaxLength="255"
columns=30
rows="3"
TextMode="MultiLine"
Text='<%# DataBinder.Eval(Container.DataItem, "repair_comment") %>'
runat="server"/>
</tr>
</table>
</EditItemTemplate>

</asp:DataList>