OnItemCommand doesn't work if datagrid is generated/bound during a PostBack

L

LeAnne

In my ASPX page, I populate the GRID when the user selects an item in the
dropdown list.
Thus, the data is fetched and the data grid DataBind() method is executed in
the PostBack (i.e. IsPostBack = TRUE on the server side).

At this time the data grid has some information and is now displayed.
How ever, the OnItemCommand that I've specified in the data grid doesn't
seem to get executed at ALL.

I.e. i've placed a simple button control in a column and set the
OnItemCommand for the grid to a public sub in the code page, but when i
click this button in a colum/row nothing happens.
I mean the page does get posted back to the server, but the server sub is
NOT executed.

What am i doing wrong here?

Try the below code:
- Note, If you change the IF IsPostBack THEN to IF NOT IsPostBack THEN,
then the code will work.
- What i'm trying to achieve is the lable text should change.
- If the data grid is bound in the first run, then the OnItemCommand will
work on the server side.
- If on the other hand the data grid is bound during a PostBack, then the
OnItemCommand will NOT work.

<!-------------------Code Block---------------------------------->
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<html>
<script runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
If IsPostBack Then
Dim loConn As New SqlConnection("data source=(local);initial
catalog=NorthWind;user id=sa;password=;")
Dim loSQLCmd As New SqlCommand
With loSQLCmd
.CommandText = "SELECT CustomerId, ContactName FROM
Customers"
.Connection = loConn
End With

Dim loDa As New SqlDataAdapter(loSQLCmd)
Dim loTable As New DataTable
loDa.Fill(loTable)

With Me.DataGrid3
.DataSource = loTable
.DataBind()
End With
End If
End Sub

Sub ShowInfo (byval sender as object, e as datagridcommandeventargs)
me.label2.text = "hello"
End Sub
</script>

<body>

<form runat="server">
<asp:DataGrid id="DataGrid3" style="Z-INDEX: 101; LEFT: 32px; POSITION:
absolute; TOP: 64px" AutoGenerateColumns="False"
OnItemCommand="ShowInfo" runat="server">
<Columns>
<asp:BoundColumn DataField="CustomerID"></asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Button Text="Test" Runat="server" CommandName="Testing"
ID="Button1"></asp:Button>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

<asp:Label id="Label2" style="Z-INDEX: 102; LEFT: 112px; POSITION:
absolute; TOP: 32px" runat="server">Label</asp:Label>

<asp:Button id="Button2" style="Z-INDEX: 103; LEFT: 344px; POSITION:
absolute; TOP: 96px" runat="server"
Text="Go to server"></asp:Button>
</form>

</body>
</html>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top