Can't get drop down list to load

B

Biva

Hello,

I have a drop down list control in a datagrid. I can't seem to populate the
control. Here is what my code looks like:

<EditItemTemplate>
<asp:DropDownList ID= "ddlUser5" DataSource = '<%# ddlUser5Bind() %>' Runat
=server>
</asp:DropDownList>
</EditItemTemplate>

Function ddlUser5DataSource() As ICollection
Dim myArray() As String = {"", "UseTax", "NonUse"}
Return myArray
End Function

Function ddlUser5Bind()
Try
ddlUser5.DataSource = ddlUser5DataSource()
ddlUser5.DataBind()


Catch ex As Exception
Current.Trace.Write(ex.ToString)
End Try

End Function

Any help would be appreciated.

Thanks,
Biva
 
J

Jacob Yang [MSFT]

Hi Biva,

Based on my test with your code, a System.NullReferenceException exception
will raise inside the ddlUser5Bind() function, for the ddlUser5 cannot be
found.

To make this task work, we should move the binding code from ddlUser5Bind()
to the ItemCreated event of the datagrid, and remove the invoke to the
ddlUser5Bind() from html source.

Here are my test sources:

1.aspx

<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="templateColumn.aspx.vb" Inherits="VBAspnet.templateColumn"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>templateColumn</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="MyDataGrid" runat="server" Width="328px"
Height="184px" AutoGenerateColumns="false"
ShowFooter="True" OnUpdateCommand="MyDataGrid_Update"
OnCancelCommand="MyDataGrid_Cancel"
OnEditCommand="MyDataGrid_Edit" CellPadding="2" BorderColor="black">
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
HeaderText="Edit Controls" CancelText="Cancel"
EditText="Edit">
<ItemStyle Wrap="False"></ItemStyle>
</asp:EditCommandColumn>
<asp:BoundColumn DataField="Item" ReadOnly="True"
HeaderText="Description"></asp:BoundColumn>
<asp:TemplateColumn>
<HeaderTemplate>
<B>Tax </B>
</HeaderTemplate>
<ItemTemplate>
<asp:Label id=Label1 runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "Tax") %>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:HyperLink id="HyperLink1" runat="server" Text="Microsoft"
NavigateUrl="http://www.microsoft.com"></asp:HyperLink>
</FooterTemplate>
<EditItemTemplate>
<asp:DropDownList id="ddlUser5" runat="server"
Width="130px"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Price"
HeaderText="Price"></asp:BoundColumn>
</Columns>
</asp:datagrid></form>
</body>
</HTML>

2.code-behind class

Public Class templateColumn
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents MyDataGrid As System.Web.UI.WebControls.DataGrid

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

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 Store As DataTable = New DataTable
Private StoreView As DataView

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If IsNothing(Session("StoreData")) Then

Dim dr As DataRow
Dim i As Integer

Store = New DataTable

Store.Columns.Add(New DataColumn("Tax", GetType(String)))
Store.Columns.Add(New DataColumn("Item", GetType(String)))
Store.Columns.Add(New DataColumn("Price", GetType(String)))

Session("StoreData") = Store

' Create sample data.
For i = 1 To 4

dr = Store.NewRow()

dr(0) = "0.0%"
dr(1) = "Item " & i.ToString()
dr(2) = (1.23 * (i + 1)).ToString()

Store.Rows.Add(dr)

Next i

Else
Store = Session("StoreData")

End If

StoreView = New DataView(Store)
StoreView.Sort = "Item"

If Not IsPostBack Then
BindGrid()
End If

End Sub

Sub MyDataGrid_Edit(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)

MyDataGrid.EditItemIndex = e.Item.ItemIndex
BindGrid()

End Sub

Sub MyDataGrid_Cancel(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)

MyDataGrid.EditItemIndex = -1
BindGrid()

End Sub

Sub MyDataGrid_Update(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)

' Get the text box that contains the price to edit.
' For bound columns the edited value is stored in a text box.
' The text box is the first control in the Controls collection.
Dim priceText As TextBox = e.Item.Cells(3).Controls(0)

' Get the check box that indicates whether to include tax from the
' TemplateColumn. Notice that in this case, the check box control is
' second control in the Controls collection.
Dim taxCheck As CheckBox = e.Item.Cells(2).Controls(1)

Dim item As String = e.Item.Cells(1).Text
Dim price As String = priceText.Text

Dim dr As DataRow

' With a database, use an update command. Since the data source is
' an in-memory DataTable, delete the old row and replace it with a
new one.

' Remove old entry.
StoreView.RowFilter = "Item='" & item & "'"
If StoreView.Count > 0 Then
StoreView.Delete(0)
End If
StoreView.RowFilter = ""

' Add new entry.
dr = Store.NewRow()

If taxCheck.Checked Then
dr(0) = "8.6%"
Else
dr(0) = "0.0%"
End If
dr(1) = item
dr(2) = price
Store.Rows.Add(dr)

MyDataGrid.EditItemIndex = -1
BindGrid()

End Sub

Sub BindGrid()

MyDataGrid.DataSource = StoreView
MyDataGrid.DataBind()

End Sub

Function ddlUser5DataSource() As ICollection
Dim myArray() As String = {"", "UseTax", "NonUse"}
Return myArray
End Function


Private Sub MyDataGrid_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
MyDataGrid.ItemCreated
Try
ddlUser5 = e.Item.FindControl("ddlUser5")
If (Not ddlUser5 Is Nothing) Then
ddlUser5.DataSource = ddlUser5DataSource()
ddlUser5.DataBind()
End If
Catch ex As Exception
Response.Write(ex.ToString)
End Try
End Sub
End Class

Does it answer your question? If I have misunderstood your concern, please
feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top