Advertisement

03.20.2008 at 08:00PM PDT, ID: 23259043
[x]
Attachment Details

cant set commandargument with gridview

Asked by jagguy in Programming for ASP.NET, .NET, .NET Framework 2.0

Tags: asp.net 2.0 ,with vb.net

i am using visual web developer 2008 under winxp home.
I am learning  to use asp.net2.0  with vb code. First i want to use a gridview to fill with data which i can do. Then i convert a datafield into a linkbutton so i can click on it and get  row details to do further processing but i cant get that far yet.


Iniially I am trying to get the rowindex of a row when i click on a linkbutton on a gridview. The problem is i want to set the commandargumet to the rowindex but i cant do this. I copy code to do this from  the msdn site and i get an error. Now i am frustrated as no one seems to know this?

I can connect to a access DB and display values in a gridview .
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
<asp:GridView ID="GridView1" runat="server"
         DataKeyNames="lname" AutoGenerateColumns="False" >
       <Columns>
         <asp:TemplateField >
        <ItemTemplate >
        <asp:LinkButton id="one" commandname="two" runat ="server"
          Text ='<%# Bind("id")%>' >
        </asp:LinkButton>
        </ItemTemplate>
        </asp:TemplateField>
...
 
 
' this is code to assign command argument to a row index on creation of a row.
 
Protected Sub GridView1_RowCreated1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
        If e.Row.RowType = DataControlRowType.DataRow Then
 
            Dim addButton As LinkButton = CType(e.Row.Cells(0).Controls(0), LinkButton) 'error
 Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.LinkButton'.
           
 addButton.CommandArgument = e.Row.RowIndex.ToString()
 
 
        End If
 
 
'here if i get the rowindex i just assign to a textbox to test but i dont get this far.
 
 Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        
        If (e.CommandName = "three") Then
            TextBox1.Text = Convert.ToInt32(e.CommandArgument)
 
    
    End Sub
[+][-]03.20.2008 at 08:11PM PDT, ID: 21177867

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.20.2008 at 08:23PM PDT, ID: 21177926

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.20.2008 at 08:38PM PDT, ID: 21177973

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.20.2008 at 08:40PM PDT, ID: 21177979

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.20.2008 at 08:43PM PDT, ID: 21177989

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.20.2008 at 08:46PM PDT, ID: 21178002

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.20.2008 at 08:54PM PDT, ID: 21178030

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.20.2008 at 08:55PM PDT, ID: 21178034

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.20.2008 at 08:59PM PDT, ID: 21178046

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.20.2008 at 09:07PM PDT, ID: 21178071

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.20.2008 at 09:16PM PDT, ID: 21178104

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.20.2008 at 09:17PM PDT, ID: 21178107

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.20.2008 at 09:32PM PDT, ID: 21178167

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.20.2008 at 09:34PM PDT, ID: 21178173

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.20.2008 at 11:39PM PDT, ID: 21178484

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.20.2008 at 11:45PM PDT, ID: 21178498

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.21.2008 at 02:05AM PDT, ID: 21178768

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.21.2008 at 03:42AM PDT, ID: 21179014

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Programming for ASP.NET, .NET, .NET Framework 2.0
Tags: asp.net 2.0 ,with vb.net
Sign Up Now!
Solution Provided By: MaxOvrdrv2
Participating Experts: 1
Solution Grade: A
 
 
 
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
  • Automotive
  • 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
  • Displays / Monitors
  • Handhelds / PDAs
  • Components
  • Peripherals
  • Laptops/Notebooks
  • Servers
  • Misc
  • Apple
  • Embedded Hardware
  • Networking Hardware
  • Storage
  • Desktops
  • New Users
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMware
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Virtualization
  • 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
  • Web Computing
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Consulting
  • 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
  • Automation
  • 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
  • Web Services
  • 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
  • Web Computing
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Lounge
  • Business Travel
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
  • Automotive
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
03.20.2008 at 08:11PM PDT, ID: 21177867
i might be the only one up this late... and i'm not used to working with Grids... but from what i can see of the error message, when a grid is rendered, it renders it's rows or cells as literal objects... therefore, the only solution i can see would be that you populate a table with the data manually instead of using the grid, and when you get to the table column you want to link, you add a control to the table instead.
 
03.20.2008 at 08:23PM PDT, ID: 21177926
Hi,

i am still a newbie with asp.net but i know this can be done with a grid .
the msdn even gave code about how to do this as i have given, and many people claimed to have done this on other forums but getting the code is taking too long.

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx


I want to use a gridview to fill with data and be able to select fields for further processing. Now i am stuck on simply getting command argument I dont want simply do something else at this stage.



 
03.20.2008 at 08:38PM PDT, ID: 21177973
ahhh... here's the problem... you need to convert the cells contents into a linkbutton... you're trying to do the whole row...

Protected Sub GridView1_RowCreated1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
        If e.Row.RowType = DataControlRowType.DataRow Then
 
            Dim addButton As LinkButton = CType(e.Row.Cells(0).Controls(0), LinkButton)

the (0) there is supposed to be replaced with the selected field's index... not just 0...
 
03.20.2008 at 08:40PM PDT, ID: 21177979
on top of that... if you only want to ADD, and not convert, you can do this instead:

If e.Row.RowType = DataControlRowType.DataRow Then
 
            Dim addButton As LinkButton
           e.Row.Cells(index).Controls(index).add(addButton)
 
03.20.2008 at 08:43PM PDT, ID: 21177989
or maybe i'm way off on what you're trying to do... sorry if i am... i'm still not getting the whole picture i think.
 
03.20.2008 at 08:46PM PDT, ID: 21178002

Hi,
I have already got the linkbutton working with the datafields contents. I can click on it .
This below just assigns the rowindex to the commandargument as explained on the website I posted.

so each command argument gets a rowindex eg0,1,2..endrow for each row

Protected Sub GridView1_RowCreated1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
        If e.Row.RowType = DataControlRowType.DataRow Then
 
            Dim addButton As LinkButton = CType(e.Row.Cells(0).Controls(0), LinkButton) 'error but all this code is copied from msdn?

so i am not sure if that is what you mean and you might have to tell me what to type as well, as i am a little new to this.
 
03.20.2008 at 08:54PM PDT, ID: 21178030
so i have a gridview with data. I click on a column with data converted to a lnkbutton.
the link button then runs the rowcommand  when clicked.

with  the rowcommand i want the rowindex of what linkbutton i clicked eg if i click on 2nd linkbutton i should get the rowindex of 2 (0 rowis probably the header). now i get the rowindex from the command argument which is easy enough BUT i need at gridview start up to assign this myself.

So what i am doing is assigning each commandargument for my templatefield a rowindex when creating the gridview . the msdn site i posted is doing this and all code i have posted is about this.

Protected Sub GridView1_RowCreated1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
        If e.Row.RowType = DataControlRowType.DataRow Then
 
            Dim addButton As LinkButton = CType(e.Row.Cells(0).Controls(0), LinkButton) 'error but all this
 
03.20.2008 at 08:55PM PDT, ID: 21178034
that's ok so am i... as mentioned, i don't work often with grids... but we'll figure it out...

ok so you assign no problem... you also create the button no problem right... so you're having problems doing what? getting to the code-behind when clicked?
 
03.20.2008 at 08:59PM PDT, ID: 21178046
oh ok... so yeah... you have to replace the 0's in the parentheses with the index that you assigned to the button...

like this:

dim ind as int32
ind=e.toInt32

now ind= the rowindex that's selected...
 
03.20.2008 at 09:07PM PDT, ID: 21178071

i can fill the gridview yes with linkbuttons as a column
i click on a linkbutton to get the rowindex in the rowcommand but there is a problem. I get the rowindex from the command argument which i havent set it up.

this needs a rowindex for each row it appears in.

  <asp:TemplateField >
        <ItemTemplate >
        <asp:LinkButton id="one" commandname="two" runat ="server"
          Text ='<%# Bind("id")%>' >
        </asp:LinkButton>
        </ItemTemplate>
        </asp:TemplateField>

to set the rowindex for the command argument i do this when i create the gridview with this even. It autmatically does this when i run the program. this below code is what you do accordig to msdn to assign command argument to a rowindex on creating of the gridview.

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx

Protected Sub GridView1_RowCreated1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
        If e.Row.RowType = DataControlRowType.DataRow Then
 
'*********            
Dim addButton As LinkButton = CType(e.Row.Cells(0).Controls(0), LinkButton) 'problem here
 Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.LinkButton'.
'**********
           
 
03.20.2008 at 09:16PM PDT, ID: 21178104
ok... here's the thing... that error you're getting is because it's trying to change row0cell0... think about it...

second, if you're creating your linkbuttons already in the template, then you don't need to convert or add the actual control... you just need to change it's commandargument property... not re-created it...

here try this:
Protected Sub GridView1_RowCreated1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
        If e.Row.RowType = DataControlRowType.DataRow Then
 
'*********            
if e.row.cells(0).controls(0).getType()=linkbutton then
  Dim addButton As LinkButton = e.Row.Cells(0).Controls(0)
'now this means that here, addbutton(local var) gets assigned ONLY if it's a linkbutton it's checking...
'do your other stuff here... add the command argument to it...
end if


 
03.20.2008 at 09:17PM PDT, ID: 21178107
the problem is assigning each linkbutton a rowindex in the column it appears in the gridview. so i can access it later whenever i click on it.
 
03.20.2008 at 09:32PM PDT, ID: 21178167
i know... but how did you want to assign it? as part of the linkbutton's ID, as part of it's EventArguments, as part of it's CommandArguments... ?

as well, WHEN are you trying to assign the rowindex property to the linkbutton? you're trying to do it, it seems, by overriding the CreateRow function of the gridview... so you're trying to assign the argument to the linkbutton as the row is being created... correct?

it's really quite simple... something like this should do the trick:

onCreateRow

e.Row.Cells(0)(NOTE: 0 here is only valid if you're adding the link button to the very first cell in your row!).Controls.FindByType(System.Web.UI.LinkButton) (NOTE: or something like that).CommandArguments.Add("Key as string", e.Row.Index)

do you understand what i mean by the code above?
 
03.20.2008 at 09:34PM PDT, ID: 21178173
here... generate the page (without trying to assign the linkbuttons indexes!) with the linkbuttons on each row, then view the source of the resulting page, and paste me the source for 3 linkbuttons that have been created... thx
 
03.20.2008 at 11:39PM PDT, ID: 21178484
this will create all linkbuttons no problems with data fro a DB.
If I click on the linkbutton i will raise the rowcommand event.
i want the row index to be stored in the command argument , and have given that code a few times already.


   <asp:GridView ID="GridView1" runat="server"
         DataKeyNames="lname" AutoGenerateColumns="False" >
       <Columns>
         <asp:TemplateField >
        <ItemTemplate >
        <asp:LinkButton id="one" commandname="two" runat ="server"
          Text ='<%# Bind("id")%>' >
        </asp:LinkButton>
        </ItemTemplate>
        </asp:TemplateField>

q)     this doesnt work but i really need the actual code because i wont be able to fill in the gaps.
          e.Row.Cells(0).Controls.Findcontrol(LinkButton).CommandArguments.Add("lname", e.Row.Index)
   
 
03.20.2008 at 11:45PM PDT, ID: 21178498
yes i am using the GridView_RowCreated1 event to assign the indexes to each linkbutton on each row.

this is how msdn describes how you do this.

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx
look at the blow from the website given

  Sub CustomersGridView_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

    ' The GridViewCommandEventArgs class does not contain a
    ' property that indicates which row's command button was
    ' clicked. To identify which row's button was clicked, use
    ' the button's CommandArgument property by setting it to the
    ' row's index.

 If e.Row.RowType = DataControlRowType.DataRow Then

      ' Retrieve the LinkButton control from the first column.
      Dim addButton As LinkButton = CType(e.Row.Cells(0).Controls(0), LinkButton)

      ' Set the LinkButton's CommandArgument property with the
      ' row's index.
      addButton.CommandArgument = e.Row.RowIndex.ToString()
 
03.21.2008 at 02:05AM PDT, ID: 21178768
this works but i cant use CType(e.Row.Cells(0).Controls(0), LinkButton) as the 0 isnt the 1st column

 Protected Sub GridView1_RowCreated1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim my As LinkButton = CType(e.Row.FindControl("one"), LinkButton)

            ' If (Not my Is Nothing) Then
            my.CommandArgument = e.Row.RowIndex.ToString()
            TextBox1.Text = e.Row.RowIndex.ToString()
            'End If

        End If
   
 
03.21.2008 at 03:42AM PDT, ID: 21179014
well then if it works? ;)
Accepted Solution
 
 
20080716-EE-VQP-32 / EE_QW_2_20070628