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
378 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Visual Studio installation 5 39
Word Template Mail merge with vb.net 4 40
Expando 4 35
Check if number is currency 15 29
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now