Solved

DataGrid Click Event

Posted on 2010-08-19
30
419 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
  • 17
  • 13
30 Comments
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
What error do you get ?
0
 
LVL 1

Author Comment

by:ProgramVB
Comment Utility
"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
Comment Utility
I think the function has to be in vbScript, not vb.net
0
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
0
 
LVL 1

Author Comment

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

Author Comment

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

Expert Comment

by:Dhaest
Comment Utility
>> 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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
No, I'm using VS2005. asp.net 2.0
0
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 53

Accepted Solution

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

Author Comment

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

Author Comment

by:ProgramVB
Comment Utility
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
Comment Utility
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
Comment Utility
Whats the normal way to do it when a user wants to select something from a grid?
0
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
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
Comment Utility
Agreed, but I think the button looks ugly (unless I can customize its image etc)
0
 
LVL 1

Author Comment

by:ProgramVB
Comment Utility

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
Comment Utility
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
Comment Utility
Or

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

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

Author Comment

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

Expert Comment

by:Dhaest
Comment Utility
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
Comment Utility
OK, going to try it out.
0
 
LVL 1

Author Comment

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

Author Closing Comment

by:ProgramVB
Comment Utility
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

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

743 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

13 Experts available now in Live!

Get 1:1 Help Now