Forum Moderators: open

Message Too Old, No Replies

datalists and buttons in asp.net

How to tie back to underlying data?

         

RossWal

12:56 am on Sep 24, 2002 (gmt 0)

10+ Year Member



I have a datalist with a 'close' button on each dataitem. Upon a click of the ''close' button, I need to update the correct row in the database (actually just an XML file), but I have not been able to figure out how to reference the correct row in the data based on ,which instance of the button was clicked.

Any help, links to articles, etc. would be very welcome.

Thanks much,
Ross

tomasz

2:33 pm on Sep 24, 2002 (gmt 0)

10+ Year Member



In your datagrid add DataKeyField 'DataKeyField="UserID"' your unique field

and then you can reference it in your code using

Private Sub DataGrid_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.UpdateCommand

dim sUserId as string=DataGrid.DataKeys(CInt(e.Item.ItemIndex))
'do update where sUser....

End Sub

I hope it will help

RossWal

5:22 pm on Sep 24, 2002 (gmt 0)

10+ Year Member



Thanks tomasz!

It appears my update event is not firing for some reason. Here is the datalist definition:

<asp:datalist id="dlTickets" datakeyfield="id" runat="server" width="60%" repeatcolumns="1" itemstyle-font-size="10pt" OnItemDataBound="DataGrid_ItemCreated">

Here is the button definition:

<itemtemplate>
<asp:button Text="Close" CommandName="Update" id="btnClose" runat="server" visible="true"></asp:button>
</itemtemplate>

And here is the code that doesn't run:

Sub dlTickets_Update(ByVal Sender As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dlTickets.UpdateCommand
Response.Write("Achoo!")
End Sub

Any ideas? What am I missing?

Thanks again!

tomasz

7:41 pm on Sep 24, 2002 (gmt 0)

10+ Year Member



I am sorry example I supplied uses code behind technique so if your code is on the page you don't need to use handle statement

Sub MyGrid_Update(sender As Object, e As DataGridCommandEventArgs)
response.write("Hello World")
End Sub
make sure to add OnUpdateComand event to your datagrid

OnUpdateCommand="MyGrid_Update"

tomasz

7:46 pm on Sep 24, 2002 (gmt 0)

10+ Year Member



here is good example

[samples.gotdotnet.com...]

RossWal

8:30 pm on Sep 24, 2002 (gmt 0)

10+ Year Member



Thanks for taking the time to help with this. I am using a code behind page (VS.NET). From your comments, I'm thinking you see something in my code that is not applicable to a code behind situation?

I added the onUpdateCommand event after my last post, obviously to no avail since I'm still pulling my hair out here... and I have frightfully little to spare ;)

There is a generated line at the top of the html page that says AutoEventWireup="false". I'm assuming that's OK.

Also, I added this to the top of the code behind class since the editor didn't insert it when I added my button to the itemtemplate list:

Protected WithEvents btnClose As System.Web.UI.WebControls.Button

Not sure where to go from here, I may abandon the button and attempt a linkbutton, but I fear I'll have the same issue. Ooops there goes another hair...

tomasz

8:44 pm on Sep 24, 2002 (gmt 0)

10+ Year Member



If you are using code behind replace
Sub dlTickets_Update(... with Sub dlTickets_UpdateCommand(..

Private Sub ShoppingCart_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles ShoppingCart.UpdateCommand
'hello world
End Sub

and
<itemtemplate>
<asp:button Text="Close" CommandName="Update" id="btnClose" runat="server" visible="true"></asp:button>
</itemtemplate>

replace with
<asp:ButtonColumn Text="Close" HeaderText="Button Text" CommandName="Update" />

RossWal

9:22 pm on Sep 24, 2002 (gmt 0)

10+ Year Member



Jeeze, I guess I'm just dense! Here is what I have:

Sub dlTickets_UpdateCommand(ByVal Sender As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dlTickets.UpdateCommand
Response.Write("hello world")
End Sub

<asp:datalist id="dlTickets" runat="server" width="60%" repeatcolumns="1" itemstyle-font-size="10pt" OnItemDataBound="DataGrid_ItemCreated" datakeyfield="id">
<itemstyle font-size="10pt"></itemstyle>
<itemtemplate>
<asp:button Text="Close" CommandName="update" id="btnClose" runat="server" visible="true">
</asp:button><br>
</itemtemplate>
</asp:datalist>

Also tried with this in datalist definition


OnUpdateCommand="dlTickets_UpdateCommand"

I'm obviously missing something, but what? Well at least I'll be saving on barbering ;)

tomasz

4:09 pm on Sep 25, 2002 (gmt 0)

10+ Year Member



here is aspx file
=================
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="test.aspx.vb" Inherits="AOA.test"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>test</title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datalist id="dlTickets" runat="server" width="60%" repeatcolumns="1" itemstyle-font-size="10pt" datakeyfield="id">
<ItemStyle Font-Size="10pt"></ItemStyle>
<ItemTemplate>
<asp:button id="btnClose1" runat="server" visible="true" CommandName="Delete" Text="Close"></asp:button>
</ItemTemplate>
</asp:datalist>
</form>
</body>
</HTML>
=====================
code behind
=============
Public Class test
Inherits System.Web.UI.Page
Protected WithEvents dlTickets As System.Web.UI.WebControls.DataList

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

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
If Not Page.IsPostBack Then
dlTickets.DataSource = CreateDataSource()
dlTickets.DataBind()
End If

End Sub

Private Sub dlTickets_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dlTickets.DeleteCommand
Dim sID As String = dlTickets.DataKeys(e.Item.ItemIndex)
Response.Write(sID)

End Sub
Function CreateDataSource() As ICollection

Dim dt As DataTable
Dim dr As DataRow
Dim i As Integer

'create a DataTable
dt = New DataTable()
dt.Columns.Add(New DataColumn("id", GetType(Integer)))
dt.Columns.Add(New DataColumn("StringValue", GetType(String)))

'Make some rows and put some sample data in
For i = 1 To 9
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
'add the row to the datatable
dt.Rows.Add(dr)
Next

'return a DataView to the DataTable
CreateDataSource = New DataView(dt)

End Function

Private Sub dlTickets_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlTickets.ItemCreated

End Sub
End Class

RossWal

5:34 pm on Sep 25, 2002 (gmt 0)

10+ Year Member



Thanks a million. If I can't get *this* to work I'll simply wander off into the desert & spend my days as a hermit....