Advertisement

01.23.2008 at 08:35AM PST, ID: 23104876
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

ImageButton in Gridview Template Column

Tags: Microsoft, VS 2005, ASP.NET, 2.0
Hello All,
    I have a Gridview control (dgDocuments) and in it I have a template column with two images in it. An up arrow and a down arrow. When either of these is clicked it runs the onclick event of the ImageButton. This works fine. My problem is that I want to ascertain the ID of the line on which the Image was clicked. I have tried every version of dgDocuments.SelectedRow.Cells(0).Text etc but I am getting Object reference not set to an instance of an object. Does anyone have any ideas on the correct syntax for this??? Here is my OnClick event:-

Protected Sub imgUpArrow_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        Dim parameterReturn As SqlParameter
        Dim ReturnVal As Integer

        Try
            Open_Connection()
            cmd = New SqlCommand("sp_Update_Reference_Documentation_Up", conn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@Id", dgDocuments.SelectedRow.Cells(0).Text)
            cmd.Parameters.AddWithValue("@Sort_Order", dgDocuments.SelectedRow.Cells(2).Text)
            cmd.Parameters.AddWithValue("@UserId", Session("UserId"))
            cmd.Parameters.AddWithValue("@Document_Id", lblDocument_Id.Text)
            parameterReturn = New SqlParameter("@ReturnValue", SqlDbType.Int)
            parameterReturn.Direction = ParameterDirection.ReturnValue
            cmd.Parameters.Add(parameterReturn)
            cmd.ExecuteNonQuery()
            If ReturnVal = cmd.Parameters("@ReturnValue").Value = 1 Then
                FillDocuments()
            Else
                ' do nothing
            End If
        Catch ex As Exception

        End Try
    End Sub

Thanks in advance,
mcdermon
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: mcdermon
Solution Provided By: srivatsavaye
Participating Experts: 1
Solution Grade: B
Views: 291
Translate:
Loading Advertisement...
01.23.2008 at 09:06AM PST, ID: 20725127

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.24.2008 at 02:17AM PST, ID: 20731974

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.24.2008 at 06:45AM PST, ID: 20733395

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.24.2008 at 07:01AM PST, ID: 20733563

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.24.2008 at 07:50AM PST, ID: 20734049

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.24.2008 at 07:53AM PST, ID: 20734079

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.24.2008 at 08:50AM PST, ID: 20734673

Rank: Guru

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.25.2008 at 03:53AM PST, ID: 20741684

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.25.2008 at 06:01AM PST, ID: 20742485

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.25.2008 at 08:00AM PST, ID: 20743548

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
01.23.2008 at 09:06AM PST, ID: 20725127

Rank: Guru

When you are working with template columns you need to use the rowcommand event of the gridview
Move you code to your rowcommand event
In your HTML for ImageButtons there are properties called CommandArgument and CommandSource
set CommandArgument = '<%# Eval("ID") %>' and CommandSource= "UpButton"

Now in your rowcommand
say
if e.CommandSource = "upButton"
''write your code here
'You can retrieve the ID using this code
dim intID as integer = Convert.toint32(e.CommandArgument)
End if

Hope this helps
Accepted Solution
 
01.24.2008 at 02:17AM PST, ID: 20731974
Thanks so much for your reply - I tried this and it worked for the ID - however, is there a way of picking up two values as I also need the "sort_order" which you can see in the code of my first post. I could run a stored proc to get this based on the value in the table for that particular ID, but is there an easier way??
Thanks,
mcdermon
 
01.24.2008 at 06:45AM PST, ID: 20733395
Sorry but it isn't working after all. It seems to pick up the row number of the line where I click the button instead of the RefID. For example:-

RefID     Document_Details            Sort_Order       UserId
12          Document1                       1                       mcdermon
14          Document2                       2                       mcdermon
13          Document3                       3                       mcdermon


is my table. And this is the order it is displayed in. But when I click on the second row my commandargument comes back as 1, even though I have used this:-

CommandArgument = '<%# Eval("RefID") %>' which is in a column in the Gridview. Also if I click on the third row it comes back as 2, so I can only assume that it is picking up the row in the grid not the RefID.

Can you help me with this?

Thanks,
mcdermon
 
01.24.2008 at 07:01AM PST, ID: 20733563

Rank: Guru

That's not possible. Could you post your gridview HTML and rowcommand event here
 
01.24.2008 at 07:50AM PST, ID: 20734049
Row Command Event:-
Private Sub dgDocuments_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles dgDocuments.RowCommand
        Dim parameterOutput As SqlParameter
        Dim parameterReturn As SqlParameter
        Dim ReturnVal As Integer
        Dim Sort_Order As String
        Dim LineId As Integer = Convert.ToInt32(e.CommandArgument)

        Try
            Open_Connection()
            cmd = New SqlCommand("sp_Get_Sort_Order_Value", conn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@RefId", LineId)
            parameterOutput = New SqlParameter("@Order", SqlDbType.Int)
            parameterOutput.Direction = ParameterDirection.Output
            cmd.Parameters.Add(parameterOutput)
            cmd.ExecuteNonQuery()
            Sort_Order = parameterOutput.Value

            If e.CommandName = "imgUpArrow" Then
                cmd = New SqlCommand("sp_Update_Reference_Documentation_Up", conn)
            Else
                cmd = New SqlCommand("sp_Update_Reference_Documentation_Down", conn)
            End If

            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@RefId", LineId)
            cmd.Parameters.AddWithValue("@Sort_Order", Sort_Order)
            cmd.Parameters.AddWithValue("@UserId", Session("UserId"))
            cmd.Parameters.AddWithValue("@Document_Id", lblDocument_Id.Text)
            parameterReturn = New SqlParameter("@ReturnValue", SqlDbType.Int)
            parameterReturn.Direction = ParameterDirection.ReturnValue
            cmd.Parameters.Add(parameterReturn)
            cmd.ExecuteNonQuery()
            If ReturnVal = cmd.Parameters("@ReturnValue").Value = 1 Then
                FillDocuments()
            Else
                ' do nothing
            End If
        Catch ex As Exception

        End Try
    End Sub

Gridview:-
<asp:GridView ID="dgDocuments" runat="server" ShowHeader="False" Width="100%" AutoGenerateColumns="False" CssClass ="Gridview" style="border-right: #10626b 1px solid; border-top: #10626b 1px solid; border-left: #10626b 1px solid; border-bottom: #10626b 1px solid"  >
        <Columns>
            <asp:BoundField DataField="RefID">
                <ItemStyle CssClass="HiddenColumn" Width="0%" Height="25px"/>
            </asp:BoundField>
            <asp:BoundField DataField="Document_Details" HeaderText="Document Details">
                <ItemStyle Width="65%" Height="25px"/>
            </asp:BoundField>
            <asp:BoundField DataField="Sort_Order" HeaderText="Sort Order">
                <ItemStyle Width="20%" Height="25px"/>
            </asp:BoundField>
            <asp:TemplateField>
                <ItemTemplate>
                    &nbsp;&nbsp;<asp:ImageButton CommandName="imgUpArrow" CommandArgument='13'  ID="imgUpArrow" runat="server" ImageUrl="~/Pictures/Arrow_Up.png" />
                    &nbsp;
                    <asp:ImageButton CommandName="imgDownArrow" CommandArgument='13'  ID="imgDownArrow" runat="server" ImageUrl="~/Pictures/Arrow_Down.png" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
 
01.24.2008 at 07:53AM PST, ID: 20734079
Opps - I had hardcoded the RefId on the gridview to test it. This is my actual Gridview code:-
<asp:GridView ID="dgDocuments" runat="server" ShowHeader="False" Width="100%" AutoGenerateColumns="False" CssClass ="Gridview" style="border-right: #10626b 1px solid; border-top: #10626b 1px solid; border-left: #10626b 1px solid; border-bottom: #10626b 1px solid"  >
        <Columns>
            <asp:BoundField DataField="RefID">
                <ItemStyle CssClass="HiddenColumn" Width="0%" Height="25px"/>
            </asp:BoundField>
            <asp:BoundField DataField="Document_Details" HeaderText="Document Details">
                <ItemStyle Width="65%" Height="25px"/>
            </asp:BoundField>
            <asp:BoundField DataField="Sort_Order" HeaderText="Sort Order">
                <ItemStyle Width="20%" Height="25px"/>
            </asp:BoundField>
            <asp:TemplateField>
                <ItemTemplate>
                    &nbsp;&nbsp;<asp:ImageButton CommandName="imgUpArrow" CommandArgument='<%# Eval("RefId") %>'  ID="imgUpArrow" runat="server" ImageUrl="~/Pictures/Arrow_Up.png" />
                    &nbsp;
                    <asp:ImageButton CommandName="imgDownArrow" CommandArgument='<%# Eval("RefId") %>'  ID="imgDownArrow" runat="server" ImageUrl="~/Pictures/Arrow_Down.png" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
 
01.24.2008 at 08:50AM PST, ID: 20734673

Rank: Guru

I see nothing wrong with the code here. I would do things here.
1.Check my stored procedure to see if it is returning the correct field
2. use just one image button and test it
 
01.25.2008 at 03:53AM PST, ID: 20741684
Hello,
   It really will not work. If I run a function on the command argument (CommandArgument='<%#Get_RefId(Eval("RefId"))%>') then

Function Get_RefId(ByVal RefID As String)

        lblRefID.Text = RefID
        Return 1

End Function

it DOES pick up the value correctly. But I cannot use this as it run this every time the row is created. Any more ideas. I have checked everything!!
 
01.25.2008 at 06:01AM PST, ID: 20742485
I think part of the problem may be that the "Select" happens before the "imgUpArrow" on the rowcommand. This is probably because I have this:-
Private Sub dgDocuments_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles dgDocuments.RowCreated
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#10626B';this.style.color='White';this.style.cursor='hand'")
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#E7F7F7';this.style.color='#10626B'")
            e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(sender, "Select$" + e.Row.RowIndex.ToString))
        End If
    End Sub
on the rowcreated. But I need this in order to allow users to select the line without using a 'Select' button. Any ideas?
 
01.25.2008 at 08:00AM PST, ID: 20743548
Hello, I eventually got this sorted using this:-

Protected Sub imgUpArrow_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        Change_Order(sender)
    End Sub

    Protected Sub imgDownArrow_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        Change_Order(sender)
    End Sub

    Sub Change_Order(ByVal sender As System.Object)
        Dim thisbutton As ImageButton
        Dim thisrow As GridViewRow
        Dim hiddenorder As HiddenField
        Dim parameterReturn As SqlParameter

        thisbutton = sender
        thisrow = thisbutton.Parent.Parent
        hiddenorder = thisrow.FindControl("Sort_Order")

        Try
            Open_Connection()
            If thisbutton.CommandName = "imgUpArrow" Then
                cmd = New SqlCommand("sp_Update_Reference_Documentation_Up", conn)
            Else
                cmd = New SqlCommand("sp_Update_Reference_Documentation_Down", conn)
            End If
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@RefId", CInt(thisbutton.CommandArgument))
            cmd.Parameters.AddWithValue("@Sort_Order", CInt(hiddenorder.Value))
            cmd.Parameters.AddWithValue("@UserId", Session("UserId"))
            cmd.Parameters.AddWithValue("@Document_Id", lblDocument_Id.Text)
            parameterReturn = New SqlParameter("@ReturnValue", SqlDbType.Int)
            parameterReturn.Direction = ParameterDirection.ReturnValue
            cmd.Parameters.Add(parameterReturn)
            cmd.ExecuteNonQuery()

            If parameterReturn.Value = 1 Then
                FillDocuments()
            Else
                ' do nothing
            End If
            '    End If
            'End If
        Catch ex As Exception

        End Try
    End Sub

Thanks for your help though!
mcdermon
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628