473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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>
May 14 '06 #1
0 1119

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jim Heavey | last post by:
Hello, I have a situation which is occuring and I am not quites sure how to overcome the problem. I have a web page which displays a datagrid, The user can click on a items within the row(s)...
7
by: vinay | last post by:
I have created a Master-Detail DataGrid The detail grid is embedded inside the Mastergrid with each row. Adding the detail grid in ItemDatabound event. I am exporting the Grid to a Excel....
2
by: pmanno | last post by:
If I have a page with a DataGrid that is bound to a DataTable that is populated by a query to a database and I want to enable sorting and paging, do I have to add the DataTable to the cache or will...
1
by: Ken Varn | last post by:
I have a problem where my DataGrid would not maintain the ViewState of my databound rows. I finally narrowed down the problem. If my first column is a template column, the view state for the...
4
by: Antonio Carpentieri | last post by:
Hi all, in my previous post I've wrong typed some tems.. this is the corrected post. in a aspx page I have a repeater like this: <asp:repeater id=repeaterResults runat="server"...
3
by: ajfish | last post by:
Hi, I have a web form with smart navigation turned on. the html has an onload event which calls a javascript function and the contents of this function are dynamically generated on the server...
0
by: Freddie | last post by:
hi, i need a datagrid w/ 2 headers, the datagrid is bound if( ! IsPostBack), Ken Cox kindly shared some code that addes a new header in the ItemDataBound method, so i moved it to Page_Load, and...
9
by: rn5a | last post by:
A Form has a DataGrid which displays records from a SQL Server 2005 DB table. Users can modify the records using this DataGrid for which I am using EditCommandColumn in the DataGrid. This is the...
0
by: New Bee | last post by:
This may or may not be a simple problem to solve, but to me its seesm complex, so I will try and be as succinct as possible. The Scenario ------------- I am using a DataGrid to display data...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.