Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. TemplateColumn - Question

TemplateColumn - Question

Scheduled Pinned Locked Moved ASP.NET
questiondatabasehelp
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    voxmail
    wrote on last edited by
    #1

    Hi Everybody! I have some trouble with accessing the properties of the items places in the template column of a DataGrid. The DataGrid is Databound. I want to reach the database key of the displayed data. If I Databind the CommandArgument of a LinkButton property with the desired ID from the database, how can I get and use it after that? Any idea? 10x to those who are going to help me! Best Regards

    E 1 Reply Last reply
    0
    • V voxmail

      Hi Everybody! I have some trouble with accessing the properties of the items places in the template column of a DataGrid. The DataGrid is Databound. I want to reach the database key of the displayed data. If I Databind the CommandArgument of a LinkButton property with the desired ID from the database, how can I get and use it after that? Any idea? 10x to those who are going to help me! Best Regards

      E Offline
      E Offline
      emorales
      wrote on last edited by
      #2

      HI, In the code where you wrote datagrid.datasource = data add another line: datagrid.datakeysfield=columnkey In the datagrid_itemcommand event write the following: sub datagrid_itemcommand(sender, e) dim itemkey as integer = datagrid.datakeys(e.item.itemindex) select case e.itemcommand case "LinkButton 's commandargument" Your code! case "another control's commandargument" case "another control's commandargument" ens case end sub Let me know if this is do you want to do emorales mcdba, mcad, mcsd

      V 1 Reply Last reply
      0
      • E emorales

        HI, In the code where you wrote datagrid.datasource = data add another line: datagrid.datakeysfield=columnkey In the datagrid_itemcommand event write the following: sub datagrid_itemcommand(sender, e) dim itemkey as integer = datagrid.datakeys(e.item.itemindex) select case e.itemcommand case "LinkButton 's commandargument" Your code! case "another control's commandargument" case "another control's commandargument" ens case end sub Let me know if this is do you want to do emorales mcdba, mcad, mcsd

        V Offline
        V Offline
        voxmail
        wrote on last edited by
        #3

        Thanks a lot for the help. Can you, please, tell me how exactly to get the linkButton's property, because it is different when it is placed in a DataGrid. Explain the string in the quotes: "LinkButton 's commandargument". I tried with LinkButton.Command Argiment, but it doesn't work (of course), because it is not in the page but in the DataGrid. What I have to do to access any ot it's properties? Even the Text Property?

        E 1 Reply Last reply
        0
        • V voxmail

          Thanks a lot for the help. Can you, please, tell me how exactly to get the linkButton's property, because it is different when it is placed in a DataGrid. Explain the string in the quotes: "LinkButton 's commandargument". I tried with LinkButton.Command Argiment, but it doesn't work (of course), because it is not in the page but in the DataGrid. What I have to do to access any ot it's properties? Even the Text Property?

          E Offline
          E Offline
          emorales
          wrote on last edited by
          #4

          Sorry, I forgot explain how to get the linkbutton's properties within datagrid. First we do a findcontrol in the cell where is it located of the item., Then you will do a ctype for the type of the control that you will access. Finally you will access to the control's properties. example: ctype(e.item.cells(0).findcontrol("controlID"), LinkButton).CommandArgument ctype(e.item.cells(0).findcontrol("controlID"), LinkButton).Text ctype(e.item.cells(0).findcontrol("controlID"), LinkButton).Enable . . . 1. cells(0): Change the 0 for the cell number where the control is located. 2. FindControl("ControlID"): Change the ControlID for the LinkButton.id property that you gave to the control. 3. Ctype(object,datatype): You have to do a ctype to access the properties of the control. Happy Programming, emorales mcdba, mcad, mcsd

          V 1 Reply Last reply
          0
          • E emorales

            Sorry, I forgot explain how to get the linkbutton's properties within datagrid. First we do a findcontrol in the cell where is it located of the item., Then you will do a ctype for the type of the control that you will access. Finally you will access to the control's properties. example: ctype(e.item.cells(0).findcontrol("controlID"), LinkButton).CommandArgument ctype(e.item.cells(0).findcontrol("controlID"), LinkButton).Text ctype(e.item.cells(0).findcontrol("controlID"), LinkButton).Enable . . . 1. cells(0): Change the 0 for the cell number where the control is located. 2. FindControl("ControlID"): Change the ControlID for the LinkButton.id property that you gave to the control. 3. Ctype(object,datatype): You have to do a ctype to access the properties of the control. Happy Programming, emorales mcdba, mcad, mcsd

            V Offline
            V Offline
            voxmail
            wrote on last edited by
            #5

            Thank you again! Can I ask you one more question? I just vrote the following method in C# void datagrid_itemcommand(object sender, DataGridCommandEventArgs e) { Label1.Text=((LinkButton)(e.Item.Cells(0).FindControl("lnkFullText"))).Text); } This error occured: Compiler Error Message: CS0118: 'System.Web.UI.WebControls.TableRow.Cells' denotes a 'property' where a 'method' was expected What should I change?

            E 1 Reply Last reply
            0
            • V voxmail

              Thank you again! Can I ask you one more question? I just vrote the following method in C# void datagrid_itemcommand(object sender, DataGridCommandEventArgs e) { Label1.Text=((LinkButton)(e.Item.Cells(0).FindControl("lnkFullText"))).Text); } This error occured: Compiler Error Message: CS0118: 'System.Web.UI.WebControls.TableRow.Cells' denotes a 'property' where a 'method' was expected What should I change?

              E Offline
              E Offline
              emorales
              wrote on last edited by
              #6

              You welcome, I think you have an extra parentesis. void datagrid_itemcommand(object sender, DataGridCommandEventArgs e) { Label1.Text=( (LinkButton)(e.Item.Cells(0).FindControl("lnkFullText") ) ) .Text); 1 2 2 3 4 4 5 5 3 ? 1 Delete the ? parentesis. The correct sentence is: Label1.Text=( (LinkButton)(e.Item.Cells(0).FindControl("lnkFullText") ) .Text); } Check that, and let me know. Remember, you can access any child controls in any control using "control".findcontrol("childcontrol") using a datatype conversion. emorales mcdba, mcad, mcsd

              V 2 Replies Last reply
              0
              • E emorales

                You welcome, I think you have an extra parentesis. void datagrid_itemcommand(object sender, DataGridCommandEventArgs e) { Label1.Text=( (LinkButton)(e.Item.Cells(0).FindControl("lnkFullText") ) ) .Text); 1 2 2 3 4 4 5 5 3 ? 1 Delete the ? parentesis. The correct sentence is: Label1.Text=( (LinkButton)(e.Item.Cells(0).FindControl("lnkFullText") ) .Text); } Check that, and let me know. Remember, you can access any child controls in any control using "control".findcontrol("childcontrol") using a datatype conversion. emorales mcdba, mcad, mcsd

                V Offline
                V Offline
                voxmail
                wrote on last edited by
                #7

                Unfortunately, the error still exists :( CS0118: 'System.Web.UI.WebControls.TableRow.Cells' denotes a 'property' where a 'method' was expected

                1 Reply Last reply
                0
                • E emorales

                  You welcome, I think you have an extra parentesis. void datagrid_itemcommand(object sender, DataGridCommandEventArgs e) { Label1.Text=( (LinkButton)(e.Item.Cells(0).FindControl("lnkFullText") ) ) .Text); 1 2 2 3 4 4 5 5 3 ? 1 Delete the ? parentesis. The correct sentence is: Label1.Text=( (LinkButton)(e.Item.Cells(0).FindControl("lnkFullText") ) .Text); } Check that, and let me know. Remember, you can access any child controls in any control using "control".findcontrol("childcontrol") using a datatype conversion. emorales mcdba, mcad, mcsd

                  V Offline
                  V Offline
                  voxmail
                  wrote on last edited by
                  #8

                  Actually, the mistake is in the parentesis :) They have to be squared. Label1.Text=( (LinkButton)(e.Item.Cells[0].FindControl("lnkFullText"))) .Text; I think this is ok, but 'DataGridCommandEventArgs', 'object' and 'CommandEventArgs' doesn't contai definition for 'Item'. What I have to use? Special thanks.

                  A E 2 Replies Last reply
                  0
                  • V voxmail

                    Actually, the mistake is in the parentesis :) They have to be squared. Label1.Text=( (LinkButton)(e.Item.Cells[0].FindControl("lnkFullText"))) .Text; I think this is ok, but 'DataGridCommandEventArgs', 'object' and 'CommandEventArgs' doesn't contai definition for 'Item'. What I have to use? Special thanks.

                    A Offline
                    A Offline
                    Anonymous
                    wrote on last edited by
                    #9

                    Hi, Wow, C# is different! Anyway, here is an example for you. I confused with the properties. There is two properties taht you have to understand. First is CommandName This property is the name of the command, that means here you will write the action that you want to do. Second is CommandArgument This is a property to set any value that you want access in the moment that ItemCommand event raise. Where you can set this property? In the datagird.ItemDataBound. The following code is a web form that I programed for you. Is running very well and is a good example. Add a web form in your project and then copy and paste the code in the behind code. Remember change the class name for the web form name that you created for this. using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace test { /// /// Summary description for WebForm1. /// public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls.DataGrid DataGrid1; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here ArrayList arrTest =new ArrayList(); arrTest.Add("Test 1"); arrTest.Add("Test 2"); arrTest.Add("Test 3"); this.DataGrid1.DataSource=arrTest; this.DataGrid1.DataBind(); } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void DataGrid1_ItemCommand(object sender, DataGridCommandEventArgs e ) { LinkButton lnk = ((LinkButton) (e.Item.Cells[0].FindControl("ddd"))); if (((String) e.CommandName) == "test") { this.Label1.Text= e.Item.Cells[2].Text; } else {

                    1 Reply Last reply
                    0
                    • V voxmail

                      Actually, the mistake is in the parentesis :) They have to be squared. Label1.Text=( (LinkButton)(e.Item.Cells[0].FindControl("lnkFullText"))) .Text; I think this is ok, but 'DataGridCommandEventArgs', 'object' and 'CommandEventArgs' doesn't contai definition for 'Item'. What I have to use? Special thanks.

                      E Offline
                      E Offline
                      emorales
                      wrote on last edited by
                      #10

                      Sorry the past reply is wrong. This is the correct Reply. Hi, Wow, C# is different! Anyway, here is an example for you. I confused with the properties (CommandArgument and CommandName. There is two properties that you have to understand. First is CommandName This property is the name of the command, that means here you will write the action that you want to do. Second is CommandArgument This is a property to set any value that you want access in the moment that ItemCommand event raise. Where you can set this property? In the datagird.ItemDataBound. You will notice that did not use the FindControl in the Datagrid.ItemCommand. I haved to look my own code to verify and create this example. The following code is a web form that I programed for you. Is running very well and is a good example. Add a web form in your project and then copy and paste the code in the web form and behind code. ------------------------------- Web Form ------------------------------- <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="test.WebForm1" %> WebForm1

                      Label

                      LinkButton

                      ----------------------------------------------------- Behind Code ----------------------------------------------------- using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlC

                      V 1 Reply Last reply
                      0
                      • E emorales

                        Sorry the past reply is wrong. This is the correct Reply. Hi, Wow, C# is different! Anyway, here is an example for you. I confused with the properties (CommandArgument and CommandName. There is two properties that you have to understand. First is CommandName This property is the name of the command, that means here you will write the action that you want to do. Second is CommandArgument This is a property to set any value that you want access in the moment that ItemCommand event raise. Where you can set this property? In the datagird.ItemDataBound. You will notice that did not use the FindControl in the Datagrid.ItemCommand. I haved to look my own code to verify and create this example. The following code is a web form that I programed for you. Is running very well and is a good example. Add a web form in your project and then copy and paste the code in the web form and behind code. ------------------------------- Web Form ------------------------------- <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="test.WebForm1" %> WebForm1

                        Label

                        LinkButton

                        ----------------------------------------------------- Behind Code ----------------------------------------------------- using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlC

                        V Offline
                        V Offline
                        voxmail
                        wrote on last edited by
                        #11

                        Thank U!!! It Works! :))) Best Regards!

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups