Button in Datalist Footer Error...

Discussion in 'ASP .Net' started by Chris, Aug 9, 2004.

  1. Chris

    Chris Guest

    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
     
    Chris, Aug 9, 2004
    #1
    1. Advertising

  2. Chris

    Rick Spiewak Guest

    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.

    "Chris" <> wrote in message
    news:...
    > 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
     
    Rick Spiewak, Aug 9, 2004
    #2
    1. Advertising

  3. Chris

    Chris Guest

    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


    "Rick Spiewak" <> wrote in message news:<>...
    > 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.
    >
    > "Chris" <> wrote in message
    > news:...
    > > 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
     
    Chris, Aug 9, 2004
    #3
  4. Chris

    Rick Spiewak Guest

    "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"

    "Chris" <> wrote in message
    news:...
    > 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
    >
    >
    > "Rick Spiewak" <> wrote in message

    news:<>...
    > > 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.
    > >
    > > "Chris" <> wrote in message
    > > news:...
    > > > 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
     
    Rick Spiewak, Aug 11, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KathyB
    Replies:
    1
    Views:
    441
  2. Paul Maidment
    Replies:
    1
    Views:
    4,447
    Nick Berardi
    Aug 7, 2003
  3. Nevyn Twyll
    Replies:
    8
    Views:
    1,958
  4. =?Utf-8?B?Q2hyaXMgRmluaw==?=

    Find Button Control in DataList Footer

    =?Utf-8?B?Q2hyaXMgRmluaw==?=, Sep 27, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    5,467
    Hermit Dave
    Sep 27, 2004
  5. Henrik
    Replies:
    1
    Views:
    460
    Ken Cox [Microsoft MVP]
    Jul 6, 2006
Loading...

Share This Page