TemplateColumn - Question
-
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
-
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
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
-
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
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?
-
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?
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
-
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
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?
-
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?
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
-
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
-
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
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.
-
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.
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 {
-
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.
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
-
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