Solved

A Button Event on my DataGrid when clicked does not occur if I use DataSet as DataSource, So How Do I make the click event occur for the button?

Posted on 2003-10-30
13
381 Views
Last Modified: 2008-03-17
The Button is a Edit/Update/Cancel button on the Column properties of the datagrid. So which Event do I use or How can I make that button to do something if its clicked?

I am using VB.NET and the project is a webapp.
0
Comment
Question by:Moizsaif123
  • 5
  • 3
  • 2
  • +2
13 Comments
 
LVL 3

Expert Comment

by:the-edge
ID: 9653950
add the code to do something to the Button Click event
0
 
LVL 2

Expert Comment

by:CuSo4
ID: 9653978
If this can help you, it's a standard way of using the Edit/Update/Cancel buttons of a datagrid:

First you have to write the methods that will actually handles the Editing/Cancelling:

Sub Grid_Edit(Sender As Object, e As DataGridCommandEventArgs)
     'Some other code
     DataGrid1.EditItemIndex = e.Item.ItemIndex
     DataGrid1.DataBind
End Sub

Sub Grid_CancelEdit(Sender As Object, e As DataGridCommandEventArgs)
     'Some other code
     DataGrid1.EditItemIndex = -1
     DataGrid1.DataBind
End Sub

Next you have to join these methods to your DataGrid, by using the attributes "oneditcommand" and "oncancelcommand":


<asp:datagrid id="DataGrid1" runat="server" oneditcommand="Grid_Edit" oncancelcommand="Grid_CancelEdit"/>

You can also use the attributes "onupdatecommand" and "ondeletecommand"

Here you have some code in C#:

                   http://www.superdotnet.com/Article.aspx?ArticleID=48
0
 
LVL 4

Author Comment

by:Moizsaif123
ID: 9654169
HeyCuSo4, This is what I used

Sub Grid_Edit(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand
DataGrid1.Visible = False
DataGrid1.EditItemIndex = e.Item.ItemIndex
DataGrid1.DataBind()
End Sub

The DataGrid1.Visible = False is used just to see if the button_click works, an I put the attributes , oneditcommand="DataGrid1"
but it did not.....what could I be doing wrong?

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 2

Expert Comment

by:CuSo4
ID: 9654188
hmm, it's oneditcommand="Grid_Edit" ...
0
 
LVL 2

Expert Comment

by:CuSo4
ID: 9654205
And I think there might also be a conflict in the last code, since you are saying the same thing two times: "Handles Datagrid1.EditCommand" and oneditcommand="Grid_Edit" are equivalent...
0
 
LVL 2

Accepted Solution

by:
CuSo4 earned 150 total points
ID: 9654287
I quickely tested it and this code works at 100% in VB.NEY:
In your aspx file:

                       <asp:DataGrid id="DataGrid1" runat="server" OnEditCommand="Grid_Edit"></asp:DataGrid>

In your code-behind file:

                        Sub Grid_Edit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
                               DataGrid1.Visible = False
                               DataGrid1.EditItemIndex = e.Item.ItemIndex
                               DataGrid1.DataBind()
                       End Sub
0
 
LVL 12

Expert Comment

by:roverm
ID: 9656532
Good article about the datagrid and it also explains what you want to know in chapter 6:

http://aspnet.4guysfromrolla.com/articles/040502-1.aspx

D'Mzz!
RoverM
0
 
LVL 4

Author Comment

by:Moizsaif123
ID: 9657852
HeyCuSo4, I used the same code yu gave me, stil nothing happnd

<asp:DataGrid id="DataGrid1" runat="server" OnEditCommand="Grid_Edit"></asp:DataGrid>

Sub Grid_Edit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
DataGrid1.Visible = False
DataGrid1.EditItemIndex = e.Item.ItemIndex
DataGrid1.DataBind()
End Sub

I have a question, Are you using DataSet as yur DataSource or DataReader
2)Do I place the Sub Grid_Edit...... in page load or anywhere in the code?


0
 
LVL 4

Author Comment

by:Moizsaif123
ID: 9657947
Hey CuSo4, Your code is correct, there is also the same event I can use in Vb itself, the editcommand event, the problem was in my page load for some reason.....all the code neccessary to populate the grid was supposed to be in


if not ispostback then

endif

I dont know wy.....but the event triggers with it....


0
 
LVL 2

Expert Comment

by:CuSo4
ID: 9658000
Yes, I think it's because during a postback the "Grid_Edit()" method is always executed after the Page_Load(). And if you include the method to populate the datagrid every-time a postback occures, the Grid_Edit event is cancelled everytime, because the code thinks you have a new Datagrid with new data that has nothing to do with the previous one... When you include the code to populate the datagrid in a "if not postback" code block, it's only executed the first time you load the page. When a postback occures, the datasource stays the same (is stored in Viewstate I think), and the Grid_Edit event can be triggered...
0
 
LVL 4

Expert Comment

by:meet_zorrer
ID: 9666255
Hi !
So this is the soluction of all of your problems now. but first i tell you something that you might or might not know.

Very First of All the problem lies in [ Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ] event, you did not post the code that what is the method you are using to get data in the datagrid, but as you said you just create a initial work to test so i guessed that you must have put code in PAGE_LOAD event something like this:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dt As New DataTable("TestTable")
            dt.Columns.Add("test1", Type.GetType("System.Decimal", True, True))
            dt.Columns.Add("test2", Type.GetType("System.Decimal", True, True))
            Dim i As Integer
            For i = 0 To 5
                'Dim dr As New DataRow()
                'dr.Item()
                Dim o(1) As Object
                o(0) = i
                o(1) = i
                dt.Rows.Add(o)
            Next
            DataGrid1.AutoGenerateColumns = True
            DataGrid1.DataSource = dt.DefaultView
            DataGrid1.DataMember = "TestTable"
            DataGrid1.DataBind()
    End Sub

means just get some temporary data and bind the grid to test data grid events. or else you must have done some sort of differently like creating a dataset and binding it to datagrid, but what ever the way the problem lies in PAGE_LOAD event where datagrid is DATA BINDED.

So when ever a page get to do any thing in ASP.NET, the web page gets a post back to send server notification this is called [Server Round Trip]. So in Page load event you have to write a line like this:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Not Page.IsPostBack Then

             '....... the rest of the code to fill any datasets or datatables .........

        End If
             
End Sub

So by adding this line in the DataGrid it saves the refilling of DataSet/DataTable.
Also the technical reason behind is this what your problem is when you click some button on the data grid the SERVER ROUND TRIP starts and datagird contents to be sent back to the server,
along with information that what is the cause of this round trip or say who told the page to go to server.

but as there is no [ IF ] to stop the refilling of data again , the datagrid gets new data evertime, even when the [DataGrid1_ItemCommand] event is to be called. That's why the DataGrid1 Control loses the information about the reason of the web page's post back or server round trip.
and the event [DataGrid1_ItemCommand] is not fired.

You can try your code with or with out of
[ If not page.isPostback then ...
  End if
] statement and you will get your answer.

That's [A BIG] it.

Regards
Meet

PS:
System 1.7 Intel, 128 MB RAM, VS 2002, .NET framework v1.0.3705, and about 20 GB Hard Disk :D
0
 
LVL 4

Expert Comment

by:meet_zorrer
ID: 9669034
Hay you posted before that you are not able to run the code that you accepted answer here so i posted my answer,
so accept it or i will report to the comunity services

Regards,
Meet.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question