?
Solved

DataGrid Click Event

Posted on 2010-08-19
30
Medium Priority
?
428 Views
Last Modified: 2012-08-13

Can anyone tell me why then function ShowDetails();") is never called, instead I get ERROR ON PAGE when i click on the grid row

      Protected Sub dg1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dg1.ItemCreated

        If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
            e.Item.Attributes.Add("onclick", "ShowDetails();")
        End If

    End Sub


    Public Sub ShowDetails()


    End Sub
0
Comment
Question by:ProgramVB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 17
  • 13
30 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 33475361
What error do you get ?
0
 
LVL 1

Author Comment

by:ProgramVB
ID: 33475403
"Error on Page" bottom left hand corner.  I think my syntax is wrong in the line:

 e.Item.Attributes.Add("onClick", "ShowDetails();")


Should I add the RETURN as below?

 e.Item.Attributes.Add("onClick", "return ShowDetails();")
0
 
LVL 1

Author Comment

by:ProgramVB
ID: 33475462
I think the function has to be in vbScript, not vb.net
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:Dhaest
ID: 33475557
0
 
LVL 1

Author Comment

by:ProgramVB
ID: 33475906
was looking for an answer to my question not a web link
0
 
LVL 1

Author Comment

by:ProgramVB
ID: 33476571
thanks for your help but I need someone who knows ASP.NET
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33482442
>> thanks for your help but I need someone who knows ASP.NET

I'm sorry, but if you make such a statement, I refuse to help you further !!!
0
 
LVL 1

Author Comment

by:ProgramVB
ID: 33482857
Listen dude, if you think helping me is cutting/pasting a link and posting that is 'helping' me then you are mistaken.  I asked a question to which I expected an answer or at a minimum some guidance. I could easily go onto the web and get links like the one you sent. I dont have time to trawl to links, I need an answer from an 'Expert'. So, if you are not skilled in a particular area, there's no point in confusing people with links rather than quality answers/guidance. Cheers
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33482916
>> if you are not skilled in a particular area ...

If I'm not skilled in asp.net, why do I have a ms certificate and how could I answer for  857,484 points in asp.net area than ?

By the way, sometimes a link gives you more explanation than just posting the possible solution ! Remind that the most experts here are doing this in their spare time (for nothing). If they can do this for a client, they will get paid !


By the way: what version of .net are you using ? Because I usually work with dataGridView (was trying to reproduce the error here)
0
 
LVL 1

Author Comment

by:ProgramVB
ID: 33482951
OK, a line in the sand.

OK, what I'm trying to achieve is when the user clicks on the gride, i capture a hidden ID field in the grid and pass that to a function.  I think I maybe going about it the wrong way in that I can only call a clientside function, not a function in my .vb file. Should I be putting the function into the asmx file instead of the.vb file? i.e. Is a click event this a Server side function? As you can see im just learning asp.net :(
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33482987
Ok I understand, I'll take a look at it.

I assume you are using .NET 3.5. Do you want the details to be shown in another datagrid or where do you want to show the data ?


0
 
LVL 1

Author Comment

by:ProgramVB
ID: 33483078
No, I'm using VS2005. asp.net 2.0
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33483100
Do you want the details to be shown in another datagrid or where do you want to show the data ?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33483155
Here are some starting points (it all depends on how you want to show it on your screen)

1. You can also consider showing the details in the same datagrid, when clicking on it

A complete article and examples of this can be found at
Master Slave Relationship in Datagrid in VB.NET while expanding and collapsing rows using client side Javascript.
http://www.progtalk.com/viewarticle.aspx?articleid=51

2. Using a detailsView-component
Flexible Custom Data Views
http://msdn.microsoft.com/nl-be/magazine/cc163694(en-us).aspx

Tutorial 10: Master/Detail Using a Selectable Master GridView with a Details DetailView
http://msdn.microsoft.com/en-us/library/aa581796.aspx


3. Click on details, call serverfunction
An Extensive Examination of the DataGrid Web Control: Part 3
http://www.4guysfromrolla.com/articles/042402-1.aspx


(ps: I learned a lot of the complete article of the last link)
0
 
LVL 1

Author Comment

by:ProgramVB
ID: 33483209
The Grid contains a list of reports, when the user clicks on a row, I want the run that report based on the hidden rowID.
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 33483259
Do you want to do this through a buttoncolumn ?
http://www.dotnetjohn.com/articles.aspx?articleid=45
0
 
LVL 1

Author Comment

by:ProgramVB
ID: 33483316
I'm going to try the buttoncolumn link, will get back to you in a while, thanks
0
 
LVL 1

Author Comment

by:ProgramVB
ID: 33483416
Can i do that without the button, maybe the button hidden? So that it happens when the row is clicked.
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33483502
I'm afraid not. If you want to do it, then we come back to your original problem, where you need to call a server-side function from javascript (this is possible thanks ajax, but if you are a beginner, this is very complex. See http://sappidireddy.wordpress.com/2008/03/31/how-to-call-server-side-function-from-client-side-code-using-pagemethods-in-aspnet-ajax/)

There happens a lot more behind those buttons than you expect (in fact, some kind of mechanism like link above)
0
 
LVL 1

Author Comment

by:ProgramVB
ID: 33483524
Whats the normal way to do it when a user wants to select something from a grid?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33483541
It's all depending of the grid you are working with, the design of the gui, the users, ...

If you perform it just on a click on the datagrid, don't you have the risk that if a user want's to mark some text to copy it to a word-document,... for other purposes, the report gets printed (or whatever you want to do behind the click).

Honestly, I would prefer a button, so that a user really needs to select the right action
0
 
LVL 1

Author Comment

by:ProgramVB
ID: 33483573
Agreed, but I think the button looks ugly (unless I can customize its image etc)
0
 
LVL 1

Author Comment

by:ProgramVB
ID: 33483581

Now Im getting an error since I've added this to the asmx file:

OnItemCommand="Button_Click">




Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33483654
To fix, modify the web.config file located in the /admin/ folder.

Change this line:

<pages validateRequest="false" />

To:

<pages validateRequest="false" enableEventValidation="false" />

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33483658
Or

Try adding this into the <system.web> section of your web.config file:

<pages enableEventValidation="false" />
0
 
LVL 1

Author Comment

by:ProgramVB
ID: 33483672
its seems that this is not wise for security issues??
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33483676
I created a new project here, added a datagrid and the event and it worked for me without changing anything in my project
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    Protected Sub ItemsGrid_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
        Label1.Text = Label1.Text & " " & e.Item.ItemIndex
        e.Item.Attributes.Add("onclick", "ItemsGrid_ItemCommand")
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not IsPostBack Then
            ' Need to load this data only once.
            ItemsGrid.DataSource = CreateDataSource()
            ItemsGrid.DataBind()
        End If
    End Sub
    
    Dim Cart As DataTable
    Dim CartView As DataView

    Function CreateDataSource() As ICollection
        Dim dt As New DataTable()
        Dim dr As DataRow

        dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
        dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
        dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))

        Dim i As Integer
        For i = 0 To 9
            dr = dt.NewRow()

            dr(0) = i
            dr(1) = "Item " & i.ToString()
            dr(2) = 1.23 * (i + 1)

            dt.Rows.Add(dr)
        Next i

        Dim dv As New DataView(dt)
        Return dv
    End Function

    Protected Sub ItemsGrid_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)

    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">
    <title>DataGrid ItemCreated Example</title>
</head>
<body>

   <form id="form1" runat="server">

      <h3>DataGrid ItemCreated Example</h3>

      <asp:DataGrid id="ItemsGrid" runat="server"
           BorderColor="Black"
           BorderWidth="1px"
           CellPadding="3"
           ShowFooter="True" onitemcreated="ItemsGrid_ItemCreated" 
          onitemcommand="ItemsGrid_ItemCommand">

          <Columns>
              <asp:ButtonColumn ButtonType="PushButton" CommandName="Select" Text="Select">
              </asp:ButtonColumn>
          </Columns>

         <HeaderStyle BackColor="#00aaaa">
         </HeaderStyle>

         <FooterStyle BackColor="#00aaaa">
         </FooterStyle>

      </asp:DataGrid>

      <br />

      <asp:Label id="Label1" 
           Text="Order of items created: " 
           runat="server"/>

      <br />

      <asp:Label id="Label2" 
           Text="Note: The -1's refer to the header and footer." 
           runat="server"/>

   </form>

</body>
</html>

Open in new window

0
 
LVL 1

Author Comment

by:ProgramVB
ID: 33483732
OK, going to try it out.
0
 
LVL 1

Author Comment

by:ProgramVB
ID: 33483759
Also you are running the code above on the server, not in the .vb file
0
 
LVL 1

Author Closing Comment

by:ProgramVB
ID: 33484437
I went with the button solution but changed it to a linkbutton - looks better and also there seems to be issues with the ItemCommand not firing if it's a button. Anyway thanks for your help - got there in the end :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

800 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