Home All Groups Group Topic Archive Search About

Datalist Edit button created in ITemplate class

Author
29 Mar 2005 4:47 PM
Charles
Hello,

I have found lots of examples and information on how to create an edit
button in the datalist control, however the edit button is done with the edit
template property.

I have created the edit button in a class that Implements ITemplate in the
InstantiateIn Sub.  The information of the row that the edit button is
clicked on will be displayed in textboxes (outside of the datalist control)
after postback for editing.  Below is some partial code:

Public Class CWebExpenses
    Inherits System.Web.UI.Page

    dlstExpenses.ItemTemplate = New ItemTemplateClass

        Private Class ItemTemplateClass
              Implements ITemplate

              Public Sub InstantiateIn(ByVal container As
System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn

               'code that sets up table and other cells

               td = New TableCell
               Dim _btnEdit As New Button
               _btnEdit.Text = "Edit"
               AddHandler _btnEdit.Click, AddressOf Edit_Click
               td.Controls.Add(_btnEdit)
               tr.Cells.Add(td)
End Sub

Private Sub Edit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
         'What should go in here?  I have an HtmlInputHidden in each row
          'that I would like to grab to fill in the textboxes that are
outside the datalist
         'control.
End Sub

End Class
End Class

Thank you for any help that you can give!

Charles

Author
29 Mar 2005 5:57 PM
Brock Allen
If I understand what you're trying to do, you want to have an Edit button
in an ItemTemplate for a DataList, but you don't want the normal Edit behavior
form the DataList. So then your question is what event do you handle? I'd
start with letting the DataList manage your promary key by setting the PrimaryKeyField
to be the column name of the PK. Then I'd suggest having your Button in your
ItemTemplate have a CommandName="MyEdit". You'd then want to handle the DataList's
ItemCommand event:

private void _list_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs
e)
{
    if (e.CommandName == "MyEdit")
    {
        object pk = _list.DataKeys[e.Item.ItemIndex];
        // now use pk to show info elsewhere
    }
}

So if the ItemCommand is your button, e.Item is the row in the DataList for
your button. Grab the PK form the DataList and then you have all the info
you need for that row. I hope this is what you were looking for.

-Brock
DevelopMentor
http://staff.develop.com/ballen



Show quoteHide quote
> Hello,
>
> I have found lots of examples and information on how to create an edit
> button in the datalist control, however the edit button is done with
> the edit template property.
>
> I have created the edit button in a class that Implements ITemplate in
> the InstantiateIn Sub.  The information of the row that the edit
> button is clicked on will be displayed in textboxes (outside of the
> datalist control) after postback for editing.  Below is some partial
> code:
>
> Public Class CWebExpenses
> Inherits System.Web.UI.Page
> dlstExpenses.ItemTemplate = New ItemTemplateClass
>
> Private Class ItemTemplateClass
> Implements ITemplate
> Public Sub InstantiateIn(ByVal container As
> System.Web.UI.Control) Implements
> System.Web.UI.ITemplate.InstantiateIn
>
> 'code that sets up table and other cells
>
> td = New TableCell
> Dim _btnEdit As New Button
> _btnEdit.Text = "Edit"
> AddHandler _btnEdit.Click, AddressOf Edit_Click
> td.Controls.Add(_btnEdit)
> tr.Cells.Add(td)
> End Sub
> Private Sub Edit_Click(ByVal sender As Object, ByVal e As
> System.EventArgs)
> 'What should go in here?  I have an HtmlInputHidden in each
> row
> 'that I would like to grab to fill in the textboxes that are
> outside the datalist
> 'control.
> End Sub
> End Class
> End Class
> Thank you for any help that you can give!
>
> Charles
>
Author
29 Mar 2005 6:57 PM
Charles
Thanks!  That is what I was looking for!  I was working on what came with the
DataList control while I was waiting for a reply and thought that would work
too.  Now I am wondering which way I want to go, with what the DataList
control gives me or what I have described.

Thanks again,
Charles


Show quoteHide quote
"Brock Allen" wrote:

> If I understand what you're trying to do, you want to have an Edit button
> in an ItemTemplate for a DataList, but you don't want the normal Edit behavior
> form the DataList. So then your question is what event do you handle? I'd
> start with letting the DataList manage your promary key by setting the PrimaryKeyField
> to be the column name of the PK. Then I'd suggest having your Button in your
> ItemTemplate have a CommandName="MyEdit". You'd then want to handle the DataList's
> ItemCommand event:
>
> private void _list_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs
> e)
> {
>     if (e.CommandName == "MyEdit")
>     {
>         object pk = _list.DataKeys[e.Item.ItemIndex];
>         // now use pk to show info elsewhere
>     }
> }
>
> So if the ItemCommand is your button, e.Item is the row in the DataList for
> your button. Grab the PK form the DataList and then you have all the info
> you need for that row. I hope this is what you were looking for.
>
> -Brock
> DevelopMentor
> http://staff.develop.com/ballen
>
>
>
> > Hello,
> >
> > I have found lots of examples and information on how to create an edit
> > button in the datalist control, however the edit button is done with
> > the edit template property.
> >
> > I have created the edit button in a class that Implements ITemplate in
> > the InstantiateIn Sub.  The information of the row that the edit
> > button is clicked on will be displayed in textboxes (outside of the
> > datalist control) after postback for editing.  Below is some partial
> > code:
> >
> > Public Class CWebExpenses
> > Inherits System.Web.UI.Page
> > dlstExpenses.ItemTemplate = New ItemTemplateClass
> >
> > Private Class ItemTemplateClass
> > Implements ITemplate
> > Public Sub InstantiateIn(ByVal container As
> > System.Web.UI.Control) Implements
> > System.Web.UI.ITemplate.InstantiateIn
> >
> > 'code that sets up table and other cells
> >
> > td = New TableCell
> > Dim _btnEdit As New Button
> > _btnEdit.Text = "Edit"
> > AddHandler _btnEdit.Click, AddressOf Edit_Click
> > td.Controls.Add(_btnEdit)
> > tr.Cells.Add(td)
> > End Sub
> > Private Sub Edit_Click(ByVal sender As Object, ByVal e As
> > System.EventArgs)
> > 'What should go in here?  I have an HtmlInputHidden in each
> > row
> > 'that I would like to grab to fill in the textboxes that are
> > outside the datalist
> > 'control.
> > End Sub
> > End Class
> > End Class
> > Thank you for any help that you can give!
> >
> > Charles
> >
>
>
>
>