15 Answers, 1 is accepted
Check out the following code snippet.
CS:
| protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) |
| { |
| if (e.Item is GridCommandItem) |
| { |
| GridCommandItem item = (GridCommandItem)e.Item; |
| LinkButton lnkbtn = (LinkButton)item.FindControl("LinkButton1"); |
| lnkbtn.Enabled = false; |
| } |
| } |
Thanks
Princy.
<MasterTableView>
<<DetailTables>>
<detailtables>
<CommandItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="InitInsert" Enabled="false" CssClass="new">Add New Course</asp:LinkButton>
</CommandItemTemplate>
My Grids structure is defined as above. Pls suggest .
--Srinath
Set the Name property for the Detail Table and try accessing the linkButton in the CommandItemTemplate as shown below.
ASPX:
| <DetailTables> |
| <telerik:GridTableView runat="server" DataSourceID="SqlDataSource1" Name="Detail2" CommandItemDisplay="top" > |
| <CommandItemTemplate> |
| <asp:LinkButton ID="LinkButton2" Text="Click" runat="server"></asp:LinkButton> |
| </CommandItemTemplate> |
CS:
| protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) |
| { |
| if ((e.Item is GridCommandItem) && (e.Item.OwnerTableView.Name == "Detail2")) |
| { |
| GridCommandItem cmdItem = (GridCommandItem)e.Item; |
| LinkButton lnkbtn = (LinkButton)cmdItem.FindControl("LinkButton2"); |
| lnkbtn.ForeColor = System.Drawing.Color.Red; |
| } |
| } |
Hope this helps..
Shinu.
Unable to cast object of type 'Telerik.Web.UI.GridDataItem' to type 'Telerik.Web.UI.GridCommandItem'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.InvalidCastException: Unable to cast object of type 'Telerik.Web.UI.GridDataItem' to type 'Telerik.Web.UI.GridCommandItem'.
Source Error:
Line 355: {
|
Source File: \admin\Edit.ascx.cs Line: 357
It is throwing the above error.
Even I have RadGrid_DetailTableDataBind Event
Can I access from there? Pls suggest.
Are you checking the above given code in the ItemdataBound event? Have you set the ItemDataBound Event handler in the aspx?
Shinu.
Thanks,
--Srinath
Sending your ASPX/ASCX will be more good.
Shinu.
<PortalControls:GridControl
ID="CurriculumGrid"
runat="server"
EnableEmbeddedSkins="false"
Skin="Default"
PageSize="20"
AutoGenerateColumns="False"
AllowSorting="false"
AllowMultiRowSelection="False"
ShowStatusBar="false"
AllowPaging="True"
OnDetailTableDataBind="CurriculumGrid_DetailTableDataBind"
OnNeedDataSource="CurriculumGrid_NeedDataSource"
GridLines="None"
OnItemDataBound="CurriculumGrid_ItemDataBound"
AllowAutomaticInserts="false"
AllowAutomaticUpdates="false"
AllowAutomaticDeletes="false"
OnItemCommand="CurriculumGrid_ItemCommand"
OnCancelCommand="CurriculumGrid_CancelCommand"
OnEditCommand="CurriculumGrid_EditCommand"
OnInsertCommand="CurriculumGrid_InsertCommand"
OnDeleteCommand="CurriculumGrid_DeleteCommand"
OnItemDeleted="CurriculumGrid_ItemDeleted"
OnItemInserted="CurriculumGrid_ItemInserted"
OnItemUpdated="CurriculumGrid_ItemUpdated"
OnUpdateCommand="CurriculumGrid_UpdateCommand">
<PagerStyle Mode="NumericPages"></PagerStyle>
<MasterTableView DataKeyNames="ClassificationID" AllowMultiColumnSorting="false" Width="100%"
CommandItemDisplay="Top" Name="Classification">
<CommandItemTemplate>
<asp:LinkButton ID="AddNew" runat="server" CommandName="InitInsert" CssClass="new">Add New Classification</asp:LinkButton>
</CommandItemTemplate>
<NoRecordsTemplate>
<p>
<asp:Label runat="server" ID="Label1">No Classifications</asp:Label></p>
</NoRecordsTemplate>
<DetailTables>
<telerik:GridTableView DataKeyNames="CourseGroupID" Name="Curriculum" Width="100%" AllowPaging="false"
CommandItemDisplay="Top" runat="server">
<parenttablerelation>
<telerik:GridRelationFields DetailKeyField="ClassificationID" MasterKeyField="ClassificationID" />
</parenttablerelation>
<commanditemtemplate>
<asp:LinkButton ID="AddNew" runat="server" CommandName="InitInsert" CssClass="new">Add New Curricula</asp:LinkButton>
</commanditemtemplate>
<norecordstemplate>
<p><asp:Label runat="server" ID="Label1">No Curricula has been assiged to this Classification</asp:Label></p>
</norecordstemplate>
<detailtables>
<telerik:GridTableView DataKeyNames="CourseGroupProductGroupID,ProductGroupID" Name="Courses" Width="100%"
AllowPaging="false" CommandItemDisplay="Top" runat="server">
<CommandItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="InitInsert" Enabled="false" CssClass="new">Add New Course</asp:LinkButton>
</CommandItemTemplate>
<ParentTableRelation>
<telerik:GridRelationFields DetailKeyField="CourseGroupID" MasterKeyField="CourseGroupID" />
</ParentTableRelation>
<NoRecordsTemplate>
<p><asp:Label runat="server" ID="Label2">No Courses have been assigned to this curricula.</asp:Label></p>
</NoRecordsTemplate>
<Columns>
<telerik:GridBoundColumn UniqueName="CybCourseCode" SortExpression="CybCourseCode" HeaderText="Code" DataField="CybCourseCode" />
<telerik:GridBoundColumn UniqueName="Title" SortExpression="Title" HeaderText="Course Title" DataField="Title" />
<telerik:GridBoundColumn UniqueName="ProductGroupDisplayOrder" visible="true" SortExpression="DisplayOrder" HeaderText="Order" DataField="DisplayOrder" />
<telerik:GridBoundColumn UniqueName="ProductGroupID" SortExpression="ProductGroupID" HeaderText="Code" DataField="ProductGroupID" visible="false" />
<telerik:GridTemplateColumn UniqueName="OtherOptions">
<ItemTemplate>
<div>
<label title="Check box if course should be assigned to students who are assigned this curriculum."><asp:Checkbox ID="AssignWithCourseGroup" runat="server" AutoPostBack="true" OnCheckedChanged="AssignWithCourseGroup_CheckedChanged" Text="Assign W/ Curriculum" Visible="true" ></asp:Checkbox></label><br />
<asp:Checkbox ID="IncludeOnReports" runat="server" AutoPostBack="true" OnCheckedChanged="IncludeOnReports_CheckedChanged" Text="Include in Reports" Visible="true"></asp:Checkbox><br />
<asp:Checkbox ID="DisplayOnMyCourses" runat="server" AutoPostBack="true" OnCheckedChanged="DisplayOnMyCourses_CheckedChanged" Text="Display on My Courses" Visible="true"></asp:Checkbox>
</div>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn UniqueName="CourseActionColumn">
<ItemTemplate>
<div>
<asp:LinkButton ID="lbCourseMoveUp" runat="server" CssClass="moveUp" CommandName="Up" Text="Move Up" Visible="true"></asp:LinkButton>
<asp:LinkButton ID="lbCourseDelete" runat="server" Enabled="true" CssClass="delete" CommandName="Delete" Text="Delete" Visible="true" ></asp:LinkButton>
<asp:LinkButton ID="lbCourseMoveDown" runat="server" CssClass="moveDown" CommandName="Down" Text="Move Down" Visible="true"></asp:LinkButton>
</div>
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
<EditFormSettings editformtype="Template">
<EditColumn uniquename="Title"></EditColumn>
<FormTemplate>
<asp:Label id="lbCourse" runat="server" Text='Select Course to add:' Font-Bold="true" /><br />
<asp:dropdownlist ID="ddProductList" runat="server" Width="500px" DataTextField="Title" DataValueField="ProductGroupID" DataSource="<%# ddProductList_DataSource() %>"></asp:dropdownlist>
<label title="Check box if course should be assigned to students who are assigned this curriculum."><asp:Label id="lblAssign" runat="server" Text='Assign w/ Curriculum:' Font-Bold="true" /></label>
<asp:Checkbox ID="chkAssignWithCourseGroup" runat="server" /><br />
<asp:Label id="lblReports" runat="server" Text='Include On Reports:' Font-Bold="true" />
<asp:Checkbox ID="chkIncludeOnReports" runat="server" /><br />
<asp:Label id="lblMyCourses" runat="server" Text='Display On My Courses:' Font-Bold="true" />
<asp:Checkbox ID="chkDisplayOnMyCourses" runat="server" /><br />
<asp:Label id="lbCourseMessage" runat="server" Text="" ForeColor="Red" /><br />
<asp:Button ID="btnUpdate1" Text='<%# (Container as GridItem).OwnerTableView.IsItemInserted ? "Insert" : "Update" %>'
runat="server" CommandName='<%# (Container as GridItem).OwnerTableView.IsItemInserted ? "PerformInsert" : "Update" %>'>
</asp:Button>
<asp:Button ID="btnCancel1" Text="Cancel" runat="server" CausesValidation="False"
CommandName="Cancel"></asp:Button>
</FormTemplate>
</EditFormSettings>
<SortExpressions>
<telerik:GridSortExpression FieldName="DisplayOrder" SortOrder="Ascending"></telerik:GridSortExpression>
</SortExpressions>
</telerik:GridTableView>
</detailtables>
<columns>
<telerik:GridBoundColumn UniqueName="CurriculumName" SortExpression="CurriculumName" HeaderStyle-HorizontalAlign="Left" HeaderText="Curriculum Name" DataField="CurriculumName" />
<telerik:GridBoundColumn UniqueName="CurriculumDisplayOrder" visible="true" SortExpression="DisplayOrder" HeaderText="Order" DataField="DisplayOrder" />
<telerik:GridBoundColumn UniqueName="CourseGroupID" SortExpression="CourseGroupID" HeaderText="Code" DataField="CourseGroupID" visible="false" />
<telerik:GridTemplateColumn UniqueName="CurriculumActionColumn">
<ItemTemplate>
<div>
<asp:LinkButton ID="lbCurriculumMoveUp" runat="server" CssClass="moveUp" CommandName="Up" Text="Move Up" Visible="true"></asp:LinkButton>
<asp:LinkButton ID="lbCurriculumEdit" runat="server" CssClass="edit" CommandName="Edit" Text="Edit" Visible="true"></asp:LinkButton>
<asp:LinkButton ID="lbCurriculumDelete" runat="server" CssClass="delete" CommandName="Delete" Text="Delete" Visible="true" ></asp:LinkButton>
<asp:LinkButton ID="lbCurriculumMoveDown" runat="server" CssClass="moveDown" CommandName="Down" Text="Move Down" Visible="true"></asp:LinkButton>
</div>
</ItemTemplate>
</telerik:GridTemplateColumn>
</columns>
<editformsettings editformtype="Template">
<EditColumn uniquename="CurriculumName"></EditColumn>
<FormTemplate>
<asp:panel id="pnlddCurriculum" runat="server" >
<asp:Label id="lbCurriculum" runat="server" Text='Select Curriculum to add:' Font-Bold="true" /><br />
<asp:dropdownlist ID="ddCourseGroupID" runat="server" DataTextField="Name" DataValueField="CourseGroupID" DataSource="<%# ddCourseList_DataSource() %>"></asp:dropdownlist>
<asp:Label id="lbOR" runat="server" Text='<br /><br />- OR -<br /><br />' Font-Bold="true" />
</asp:panel>
<asp:Label id="lbCurriculum2" runat="server" Text='<%# (Container as GridItem).OwnerTableView.IsItemInserted ? "Type in a new curriculum name:" : "Curriculum Name:" %>' Font-Bold="true" /><br />
<asp:TextBox ID="txtCurriculum" runat="server" Text='<%# Bind( "CurriculumName" ) %>' MaxLength="250" Width="400px" />
<%--<asp:CustomValidator ID="cvCurriculum" runat="server" ErrorMessage="Invalid input" ControlToValidate="txtCurriculum" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>--%>
<asp:Label id="lblMessage" runat="server" Text="" ForeColor="Red" /><br />
<asp:panel id="pnlAllowCurriculum" runat="server">
<asp:Label id="lblConfirmation" runat="server" Text='Allow Curricula to be Assigned to Students:' Font-Bold="true" />
<asp:radiobutton GroupName="confirmation" ID="rbYes" Text="Yes" Checked="true" runat="server" />
<asp:radiobutton GroupName="confirmation" ID="rbNo" Text="No" runat="server" />
<asp:Label ID="lblAllowAssignToStudents" Text='<%# Bind( "AllowAssignToStudents" ) %>' Visible="false" runat="server" />
<br />
</asp:panel>
<asp:Button ID="btnUpdate" Text='<%# (Container as GridItem).OwnerTableView.IsItemInserted ? "Insert" : "Update" %>'
runat="server" CommandName='<%# (Container as GridItem).OwnerTableView.IsItemInserted ? "PerformInsert" : "Update" %>'>
</asp:Button>
<asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel"></asp:Button>
<%--<asp:Label id="lblMessage" Visible="false" runat="server" Text='Please enter a value or select from a value from list' ForeColor="red" Font-Bold="true" /><br />--%>
</FormTemplate>
</editformsettings>
<sortexpressions>
<telerik:GridSortExpression FieldName="DisplayOrder"></telerik:GridSortExpression>
</sortexpressions>
</telerik:GridTableView>
</DetailTables>
<Columns>
<telerik:GridBoundColumn UniqueName="Classification" SortExpression="ClassificationName"
HeaderText="Classification Name" DataField="ClassificationName" />
<telerik:GridBoundColumn UniqueName="ClassificationDisplayOrder" visible="true" SortExpression="DisplayOrder"
HeaderText="Order" DataField="DisplayOrder" />
<telerik:GridBoundColumn UniqueName="ClassificationID" visible="false" SortExpression="ClassificationID"
HeaderText="Code" DataField="ClassificationID" />
<telerik:GridTemplateColumn UniqueName="ClassificationActionColumn">
<itemtemplate>
<div>
<asp:LinkButton ID="lbMoveUp" runat="server" CssClass="moveUp" CommandName="Up" Text="Move Up" Visible="true"></asp:LinkButton>
<asp:LinkButton ID="lbClassificationEdit" runat="server" CssClass="edit" CommandName="Edit" Text="Edit" Visible="true"></asp:LinkButton>
<asp:LinkButton ID="lbClassificationDelete" runat="server" CssClass="delete" CommandName="Delete" Text="Delete" Visible="true"></asp:LinkButton>
<asp:LinkButton ID="lbMoveDown" runat="server" CssClass="moveDown" CommandName="Down" Text="Move Down" Visible="true"></asp:LinkButton>
</div>
</itemtemplate>
</telerik:GridTemplateColumn>
</Columns>
<EditFormSettings EditFormType="Template">
<EditColumn UniqueName="Classification">
</EditColumn>
<FormTemplate>
<asp:Label ID="lbClassification" runat="server" Text='<%# (Container as GridItem).OwnerTableView.IsItemInserted ? "Enter new classification name:" : "Classification Name:" %>'
Font-Bold="true" /><br />
<asp:TextBox ID="txtClassification" runat="server" Text='<%# Bind( "ClassificationName" ) %>'
MaxLength="500" Width="400px" />
<asp:Label id="lblClassificationMessage" runat="server" Text="" ForeColor="Red" />
<%--<asp:RequiredFieldValidator ID="valClassification" runat="server" ErrorMessage="<br>Please enter a classification name."
ControlToValidate="txtClassification" Display="Dynamic"></asp:RequiredFieldValidator>--%>
<asp:TextBox ID="txtClassificationID" runat="server" Text='<%# Bind( "ClassificationID" ) %>'
Visible="false" />
<br />
<asp:Button ID="btnUpdate2" Text='<%# (Container as GridItem).OwnerTableView.IsItemInserted ? "Insert" : "Update" %>'
runat="server" CommandName='<%# (Container as GridItem).OwnerTableView.IsItemInserted ? "PerformInsert" : "Update" %>'>
</asp:Button>
<asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
CommandName="Cancel"></asp:Button>
</FormTemplate>
</EditFormSettings>
<SortExpressions>
<telerik:GridSortExpression FieldName="DisplayOrder">
</telerik:GridSortExpression>
</SortExpressions>
<PagerTemplate>
</PagerTemplate>
</MasterTableView>
<PagerStyle Position="Bottom" />
</PortalControls:GridControl>
CurriculumGrid_ItemDataBound
CurriculumGrid_DetailTableDataBind
Please let me know how to access that link in the above mentioned events.
Thanks,
--Srinath
Is your Grid in the ASCX page?
Shinu
Thanks,
--Srinath
Have you set the proper NameSpace at the top of the ascx.cs Page? Try setting it as shown below.
ascx.cs:
| using Telerik.Web.UI; |
Thanks
Princy.
I am using that. My code in ascx.cs is working fine with out errors, All the events are firing with out any road blocks.. I just need to access the below mentioned link in one of the below mentioned events. Its bit urgent Princy. Could pls help me out.
Thanks,
--Srinath
I tried this on my end and it is working as expected. Here is the code:
ASCX:
| <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %> |
| <%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" |
| Namespace="System.Web.UI" TagPrefix="asp" %> |
| <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> |
| <br /> |
| <br /> |
| <br /> |
| <asp:ScriptManager id="ScriptManager1" runat="server"> |
| </asp:ScriptManager><br /> |
| <br /> |
| <br /> |
| <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" |
| GridLines="None" OnItemDataBound="RadGrid1_ItemDataBound" OnNeedDataSource="RadGrid1_NeedDataSource"> |
| <MasterTableView DataKeyNames="ProductID" DataSourceID="SqlDataSource1"> |
| <RowIndicatorColumn Visible="False"> |
| <HeaderStyle Width="20px" /> |
| </RowIndicatorColumn> |
| <ExpandCollapseColumn Resizable="False" Visible="False"> |
| <HeaderStyle Width="20px" /> |
| </ExpandCollapseColumn> |
| <Columns> |
| <telerik:GridBoundColumn DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" |
| UniqueName="ProductName"> |
| </telerik:GridBoundColumn> |
| <telerik:GridBoundColumn DataField="ProductID" DataType="System.Int32" HeaderText="ProductID" |
| ReadOnly="True" SortExpression="ProductID" UniqueName="ProductID"> |
| </telerik:GridBoundColumn> |
| <telerik:GridBoundColumn DataField="SupplierID" DataType="System.Int32" HeaderText="SupplierID" |
| SortExpression="SupplierID" UniqueName="SupplierID"> |
| </telerik:GridBoundColumn> |
| </Columns> |
| <DetailTables> |
| <telerik:GridTableView runat="server" DataSourceID="SqlDataSource1" > |
| <Columns> |
| <telerik:GridBoundColumn DataField="ProductName" UniqueName="ProductName" HeaderText="ProductName" SortExpression="ProductName" ></telerik:GridBoundColumn> |
| </Columns> |
| <DetailTables> |
| <telerik:GridTableView runat="server" DataSourceID="SqlDataSource1" Name="Detail2" CommandItemDisplay="top" > |
| <CommandItemTemplate> |
| <asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton> |
| </CommandItemTemplate> |
| <Columns> |
| <telerik:GridBoundColumn DataField="ProductName" HeaderText="ProductName" UniqueName="ProductName" SortExpression="ProductName" ></telerik:GridBoundColumn> |
| </Columns> |
| </telerik:GridTableView> |
| </DetailTables> |
| </telerik:GridTableView> |
| </DetailTables> |
| <EditFormSettings> |
| <PopUpSettings ScrollBars="None" /> |
| </EditFormSettings> |
| </MasterTableView> |
| </telerik:RadGrid> |
| <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" |
| SelectCommand="SELECT [ProductName], [ProductID], [SupplierID] FROM [Products]"> |
| </asp:SqlDataSource> |
ASCX.CS:
| using System; |
| using System.Data; |
| using System.Configuration; |
| using System.Collections; |
| using System.Web; |
| using System.Web.Security; |
| using System.Web.UI; |
| using System.Web.UI.WebControls; |
| using System.Web.UI.WebControls.WebParts; |
| using System.Web.UI.HtmlControls; |
| using Telerik.Web.UI; |
| public partial class WebUserControl : System.Web.UI.UserControl |
| { |
| protected void Page_Load(object sender, EventArgs e) |
| { |
| } |
| protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) |
| { |
| if ((e.Item is GridCommandItem) && (e.Item.OwnerTableView.Name == "Detail2")) |
| { |
| GridCommandItem cmditem = (GridCommandItem)e.Item; |
| LinkButton lnkbtn = (LinkButton)cmditem.FindControl("LinkButton1"); |
| lnkbtn.Enabled = false; |
| } |
| } |
| } |
Thanks
Shinu.
--Srinath
You can also go through the following help document link.
Traversing detail tables/items in Telerik RadGrid
Shinu.