I guess u dont set Selected Item. Hope this sample can help u although I didnt get u clearly.
Code:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<Script Runat="Server">
Sub Page_Load( s As Object, e As EventArgs )
If Not isPostBack Then
BindData
End If
End Sub
Sub BindData
Dim myConnection As SqlConnection
Dim myCommand As SQLCommand
myConnection = New SqlConnection( "Server=Localhost;uid=sa;pwd=secret;Database=Pubs" )
myCommand = New SqlCommand( "Select au_id, au_lname, au_fname, phone from Authors order by au_lname", myConnection )
myConnection.Open()
myDataList.DataSource = myCommand.ExecuteReader()
myDataList.DataBind()
myConnection.Close()
End Sub
Sub editAuthor( s As Object, e As DataListCommandEventArgs )
myDataList.EditItemIndex = e.Item.ItemIndex
BindData
End Sub
Sub cancelEdit( s As Object, e As DataListCommandEventArgs )
myDataList.EditItemIndex = -1
BindData
End Sub
Sub deleteAuthor( s As Object, e As DataListCommandEventArgs )
Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim sqlString As String
myConnection = New SqlConnection( "Server=Localhost;uid=sa;pwd=secret;Database=Pubs" )
sqlString = "Delete Authors Where au_id=@authorID"
myCommand = New SqlCommand( sqlString, myConnection )
myCommand.Parameters.Add( New SQLParameter( "@authorID", SqlDbType.VarChar, 11 ))
myCommand.Parameters( "@authorID" ).Value = myDataList.DataKeys.Item( e.Item.ItemIndex )
myConnection.Open()
myCommand.ExecuteNonQuery
myDataList.DataBind()
myConnection.Close()
myDataList.EditItemIndex = -1
BindData
End Sub
Sub updateAuthor( s As Object, e As DataListCommandEventArgs )
Dim myConnection As SQLConnection
Dim myCommand As SQLCommand
Dim sqlString As String
myConnection = New SQLConnection( "Server=Localhost;uid=sa;pwd=secret;Database=Pubs" )
sqlString = "Update Authors Set au_lname=@lastname, au_fname=@firstname, phone=@phone" _
& " Where au_id=@authorID"
myCommand = New SQLCommand( sqlString, myConnection )
myCommand.Parameters.Add( New SQLParameter( "@lastname", SqlDbType.VarChar, 40 ))
myCommand.Parameters( "@lastname" ).Value = cTYPE( e.Item.FindControl( "lastname" ), textBox ).Text
myCommand.Parameters.Add( New SQLParameter( "@firstname", SqlDbType.VarChar, 20 ))
myCommand.Parameters( "@firstname" ).Value = cTYPE( e.Item.FindControl( "firstname" ), textBox ).Text
myCommand.Parameters.Add( New SQLParameter( "@phone", SqlDbType.Char, 12 ))
myCommand.Parameters( "@phone" ).Value = cTYPE( e.Item.FindControl( "phone" ), textBox ).Text
myCommand.Parameters.Add( New SQLParameter( "@authorID", SqlDbType.VarChar, 11 ))
myCommand.Parameters( "@authorID" ).Value = myDataList.DataKeys.Item( e.Item.ItemIndex )
myConnection.Open()
myCommand.ExecuteNonQuery
myDataList.DataBind()
myConnection.Close()
myDataList.EditItemIndex = -1
BindData
End Sub
</Script>
<html>
<head><title>Edit Authors</title></head>
<body>
<form Runat="Server">
<asp:DataList
id="myDataList"
cellpadding=10
cellspacing=0
gridlines="both"
RepeatColumns="3"
RepeatDirection="Horizontal"
DataKeyField="au_id"
OnEditCommand="editAuthor"
OnDeleteCommand="deleteAuthor"
OnUpdateCommand="updateAuthor"
OnCancelCommand="cancelEdit"
Runat="Server">
<ItemTemplate>
<asp:LinkButton
Text="Edit"
CommandName="edit"
Runat="Server"/>
<%# Container.DataItem( "au_lname" )%>
</ItemTemplate>
<EditItemTemplate>
<b>Last Name:</b>
<br><asp:TextBox
id="lastname"
text='<%# Container.DataItem( "au_lname" ) %>'
Runat="Server"/>
<p>
<b>First Name:</b>
<br><asp:TextBox
id="firstname"
text='<%# Container.DataItem( "au_fname" ) %>'
Runat="Server"/>
<p>
<b>Phone:</b>
<br><asp:TextBox
id="phone"
text='<%# Container.DataItem( "phone" ) %>'
Runat="Server"/>
<p>
<asp:Button
Text="Update"
CommandName="update"
Runat="Server"/>
<asp:Button
Text="Delete"
CommandName="delete"
Runat="Server"/>
<asp:Button
Text="Cancel"
CommandName="cancel"
Runat="Server"/>
</EditItemTemplate>
</asp:DataList>
</form>
</body>
</html>
Always:),
Hovik Melkomian.