Button in Datalist Footer Error...

C

Chris

Hi Folks,

well I have this "small" problem with the footertemplate in a
datalist.
I added a button and a textbox to update the db with the given String
of the Textbox.
I already searched for it and know now, that I have to place the
handling in Itemcreated. So when I click on the button everything is
fine except when the sub "createSubject" is called, then I get an
"Object reference not set" error.
This occurs when I try to get the value of the Textbox.

Is this in general the best approach for using a button with some
commands in a datalist Footer or am I just missing something...

Any Links, references, help would be really appreciated as this whole
datalist thing is driving me nuts...

Thanks in advance,
Chris

Below is my Code

Private Sub dlsNewSubject_ItemCreated(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.DataListItemEventArgs) Handles
dlsNewSubject.ItemCreated
If e.Item.ItemType = ListItemType.Footer Then
Dim cmdCreateSubject As Button =
CType(e.Item.FindControl("cmdCreateSubject"), Button)
AddHandler cmdCreateSubject.Click, AddressOf CreateSubject

End If
End Sub


Public Sub CreateSubject(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Dim dlSubject As DataList
Dim NewSubjectName As String
NewSubjectName = CType(dlSubject.FindControl("txtNewSubject"),
TextBox).Text
....
Do the database Stuff here
....
End Sub
 
R

Rick Spiewak

Instead of attaching a command handler to this button, set its commandname
property, and check for it during the ItemCommand event. If the item type is
footer, you should be able to use FindControl on the item to find your
textbox.
 
C

Chris

Hi Rick,

thanks for the reply. I tested your suggestion and put the Commandname
in the aspx page and the code as well in the Itemcommand event but it
doesn't fire there either, in fact if I debug it, the ItemCommand
doesn't even get touched.
Do I have to make a reference out of the ItemCreated to the
ItemCommand in some way and if so how can I do that?

Maybe it's just me, but I thought a button in the Datalist
footer/header must be a common thing, but yet I haven't found any
example that worked for me so far.

cheers,
Chris
 
R

Rick Spiewak

"It works on my machine" said the developer <g>.

Here's the .aspx part:
<asp:DataList id="DataList1" style="Z-INDEX: 105; LEFT: 360px; POSITION:
absolute; TOP: 276px" runat="server">
<FooterTemplate>
<asp:Button id="Button3" runat="server" Text="Button"
CommandName="CreateSubject"></asp:Button>
<asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:DataList>

Here's the code-behind piece I used to test it:

Private Sub DataList1_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataListCommandEventArgs) Handles
DataList1.ItemCommand
Dim i As Integer = 0
End Sub

Private Sub btnTestDataList_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnTestDataList.Click
Dim al As New ArrayList
al.Add("One")
al.Add("Two")
Me.DataList1.DataSource = al
Me.DataList1.DataBind()
End Sub

putting a breakpoint on the "dim i as integer" line, I see:
?e.CommandName
"CreateSubject"
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top