Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

datalist dynamic edititemtemplate and update a record

Status
Not open for further replies.

harmmeijer

Programmer
Mar 1, 2001
869
CN
My datalist loads its edititemtemplate dynamically because it lists an array of documents.
There are about 10 classes that inherrit from document (like passport or driving licsence).
The datasource of the datalist is an array of documents (so they could be passports or driving licsence or ...)

When the edit command is clicked I get the correct template:
Dim dc As document = lstDocuments.DataSource(lstDocuments.EditItemIndex)
lstDocuments.EditItemTemplate = Page.LoadTemplate(dc.getEditItemTemplate)

here is what a passport template looks like
Code:
<%@ Language="vb" %>
<h1>Edit your passport</h1><br>
<hr>
<asp:TextBox 
	ID="txtFirstName" 
	Runat="server"
	text='<%#DataBinder.Eval(CType(Container, DataListItem).DataItem, "firstname")%>'
			 
	>
</asp:TextBox>
<asp:TextBox 
	ID="txtLastName" 
	Runat="server"
	text='<%#DataBinder.Eval(CType(Container, DataListItem).DataItem, "lastname")%>'
	>
</asp:TextBox><br>
<asp:LinkButton id="UpdateButton" 
                 Text="Update" 
                 CommandName="Update" 
                 runat="server"/>

The thing is when I click the update link the OnUpdateCommand does not get called.

When I don't load my editItemTemplate dynamically and put the following code in the datalist
Code:
<EditItemTemplate >
	<asp:LinkButton id="UpdateButton" 
                 Text="Update" 
                 CommandName="Update" 
                 runat="server"/>
</EditItemTemplate>

Than the OnUpdateCommand will get called when I click the link.

How can I update a record of my datalist without hard coding the editItemTemplate?

this sample
and many that are basically the same show how to load a template dynamically but I could not find one that can update
a record as well



Greetings, Harm Meijer
 
Here is a (not) working example of what i'm trying to do
test.aspx
Code:
<%@ Page Language="vb" Codebehind="test.aspx.vb" Inherits="testNet.test1"%>
<html>
  <head>
    <title>test</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">
		<asp:DataList 
			Runat="server" 
			OnUpdateCommand="updateRecord" 
			OnItemCommand="itemCommand" 
			ID="dlst">
			<ItemTemplate>
				<%#DataBinder.Eval(Container.DataItem, "val")%>
				<asp:Button 
					id="cmdEdit"
					Runat='server' 
					text='Edit'
				></asp:Button>
			</ItemTemplate>
		</asp:DataList>
    </form>
  </body>
</html>

the code behind test.aspx.vb
Code:
    Public WithEvents dlst As DataList
    Dim d(1) As data
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        d(0) = New Data()
        d(0).Val = "first record"
        d(1) = New Data()
        d(1).Val = "second record"
        dlst.DataSource = d
        If Not Me.IsPostBack Then
            dlst.DataBind()
        End If
    End Sub
    Public Sub updateRecord(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs)
        Response.Write("updaterecord has been called")
        ' this never gets called anyway
    End Sub
    Public Sub itemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs)
        ' at the time someone clicks edit on a record I know what template to use so I have to load it here
        Response.Write("itemCommand has been called")
        dlst.EditItemIndex = 1
        dlst.EditItemTemplate = Me.LoadTemplate("test.ascx")
        dlst.DataBind()
    End Sub
    Class data
        Private _val As String
        Public Property Val() As String
            Get
                Return Me._val
            End Get
            Set(ByVal Value As String)
                Me._val = Value
            End Set
        End Property
    End Class
End Class

the ascx (test.ascx)
Code:
<%@ Language="vb"  %>
				<asp:button id="UpdateButton"
					Text="Update" 
					CommandName="Update" 
					runat="server"/>

When the editItemTemplate comes from test.ascx (loaded dynamically) no event is fired when I click on the update button.



Greetings, Harm Meijer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top