DataGrid Click Event


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
LVL 1
ProgramVBAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DhaestCommented:
What error do you get ?
0
ProgramVBAuthor Commented:
"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
ProgramVBAuthor Commented:
I think the function has to be in vbScript, not vb.net
0
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

DhaestCommented:
0
ProgramVBAuthor Commented:
was looking for an answer to my question not a web link
0
ProgramVBAuthor Commented:
thanks for your help but I need someone who knows ASP.NET
0
DhaestCommented:
>> 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
ProgramVBAuthor Commented:
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
DhaestCommented:
>> 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
ProgramVBAuthor Commented:
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
DhaestCommented:
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
ProgramVBAuthor Commented:
No, I'm using VS2005. asp.net 2.0
0
DhaestCommented:
Do you want the details to be shown in another datagrid or where do you want to show the data ?
0
DhaestCommented:
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
ProgramVBAuthor Commented:
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
DhaestCommented:
Do you want to do this through a buttoncolumn ?
http://www.dotnetjohn.com/articles.aspx?articleid=45
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ProgramVBAuthor Commented:
I'm going to try the buttoncolumn link, will get back to you in a while, thanks
0
ProgramVBAuthor Commented:
Can i do that without the button, maybe the button hidden? So that it happens when the row is clicked.
0
DhaestCommented:
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
ProgramVBAuthor Commented:
Whats the normal way to do it when a user wants to select something from a grid?
0
DhaestCommented:
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
ProgramVBAuthor Commented:
Agreed, but I think the button looks ugly (unless I can customize its image etc)
0
ProgramVBAuthor Commented:

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
DhaestCommented:
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
DhaestCommented:
Or

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

<pages enableEventValidation="false" />
0
ProgramVBAuthor Commented:
its seems that this is not wise for security issues??
0
DhaestCommented:
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
ProgramVBAuthor Commented:
OK, going to try it out.
0
ProgramVBAuthor Commented:
Also you are running the code above on the server, not in the .vb file
0
ProgramVBAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.