Scott Harford
Top achievements
Rank 1
Scott Harford
asked on 25 Feb 2009, 03:50 PM
Apparently, nesting ASP.NET or Telerik controls within a PanelBar Item template will cause said control(2) to loose their server side recognition. You may not access the control in the Code Behind page using the conventional methods.
Yes, I have seen documentation, posts, that describe using the FindControl method, but that only provides proof of the problem.
I have seen the article within Online Documentation ('Accessing Controls Inside Templates'), and it provides an example using a nested RadColorPicker. Additionaly, if provides example VB code for the ColorChanged event handler, ass follows:
Protected Sub RadColorPicker1_ColorChanged(ByVal sender As Object, _
ByVal e As EventArgs) Handles RadColorPicker1.ColorChanged
Dim picker As RadColorPicker = CType(sender, RadColorPicker)
Dim label As Label = CType(picker.Parent.FindControl("Label1"), Label)
label.ForeColor = picker.SelectedColor
End Sub
In my case however, this doesn't work. Since the control is not recognized in the code behind page, I cannot write a handler for any of the control's events. The statement 'Handles ControlName.EventName' will not compile.
I am at a loss to understand why anyone would want to disable the runat="Server" functionality of a .NET control.
Perhaps you could explain it to me.
Regards,
SGH
Yes, I have seen documentation, posts, that describe using the FindControl method, but that only provides proof of the problem.
I have seen the article within Online Documentation ('Accessing Controls Inside Templates'), and it provides an example using a nested RadColorPicker. Additionaly, if provides example VB code for the ColorChanged event handler, ass follows:
Protected Sub RadColorPicker1_ColorChanged(ByVal sender As Object, _
ByVal e As EventArgs) Handles RadColorPicker1.ColorChanged
Dim picker As RadColorPicker = CType(sender, RadColorPicker)
Dim label As Label = CType(picker.Parent.FindControl("Label1"), Label)
label.ForeColor = picker.SelectedColor
End Sub
In my case however, this doesn't work. Since the control is not recognized in the code behind page, I cannot write a handler for any of the control's events. The statement 'Handles ControlName.EventName' will not compile.
I am at a loss to understand why anyone would want to disable the runat="Server" functionality of a .NET control.
Perhaps you could explain it to me.
Regards,
SGH
13 Answers, 1 is accepted
0
Hi Scott Harford,
RadPanelBar utilizes the item template paradigm (just as the DataGrid, Repeater and many other built-in controls). As a result the controls are not available in codebehind (just as with the DataGrid, repater and so on). To subscribe to some event you can use the AutoEventWireUp feature or the AddHandler method. If you choose the second option you should use the FindControl method of the specific panel item to locate the target control:
I hope this helps,
Albert
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
RadPanelBar utilizes the item template paradigm (just as the DataGrid, Repeater and many other built-in controls). As a result the controls are not available in codebehind (just as with the DataGrid, repater and so on). To subscribe to some event you can use the AutoEventWireUp feature or the AddHandler method. If you choose the second option you should use the FindControl method of the specific panel item to locate the target control:
Dim button As Button = RadPanelBar1.Items(0).Items(0).FindControl("Button1") |
AddHandler button.Click, AddressOf Me.Button1_Click |
I hope this helps,
Albert
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Scott Harford
Top achievements
Rank 1
answered on 25 Feb 2009, 05:52 PM
Albert,
Thank you for the reply. While your answer explains why the control behaves the way it does, the logic of doing this it what is so perplexing. A Grid or Repeater is not typically used as a container to present nested User Controls.
If you wish to have the PanelBar act as a container control, it should behave like one. That is, capable of supporting nested buttons, treeviews, user controls, and even Grids, without killing the server side functionality. I would think anyway.
In its current state, the PanelBar should only be used as a fancy list control. Really.
Regards,
Scott
Thank you for the reply. While your answer explains why the control behaves the way it does, the logic of doing this it what is so perplexing. A Grid or Repeater is not typically used as a container to present nested User Controls.
If you wish to have the PanelBar act as a container control, it should behave like one. That is, capable of supporting nested buttons, treeviews, user controls, and even Grids, without killing the server side functionality. I would think anyway.
In its current state, the PanelBar should only be used as a fancy list control. Really.
Regards,
Scott
0
Stephen
Top achievements
Rank 1
answered on 24 Nov 2009, 06:17 PM
I am trying to do this same thing and having an issue. Can you tell me where the above code should go? I put it in the Page_Load but my button click is still not recognized and the button click method does not fire.
I have a button nested in a RadPanelBar and I have a click method in the code behind (just like the demo example) but I can't get the method to fire.
I have a button nested in a RadPanelBar and I have a click method in the code behind (just like the demo example) but I can't get the method to fire.
0
Hi Stephen,
Please find attached a sample code snippet that shows the needed approach.
Greetings,
Paul
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Please find attached a sample code snippet that shows the needed approach.
Greetings,
Paul
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Stephen
Top achievements
Rank 1
answered on 01 Dec 2009, 05:42 PM
Hello,
Thank you very much for the reply. After much testing between your code and mine I discovered that what seems to be causing my button_click to not fire is the fact that on my second RadPanelItem I have a RadDatePicker control. Do you have any idea why having this control on the page in a second panel would cause that button_click to not fire?
So my page code looks something like the block below. There are other controls in that second Panel and I also have a third Panel with controls. If I remove the RadDatePicker everything else works...
Thank you very much for the reply. After much testing between your code and mine I discovered that what seems to be causing my button_click to not fire is the fact that on my second RadPanelItem I have a RadDatePicker control. Do you have any idea why having this control on the page in a second panel would cause that button_click to not fire?
So my page code looks something like the block below. There are other controls in that second Panel and I also have a third Panel with controls. If I remove the RadDatePicker everything else works...
<telerik:RadPanelBar ID="RadPanelBar1" runat="server" ExpandMode="SingleExpandedItem" Width="100%"> |
<Items> |
<telerik:RadPanelItem Expanded="True" Text="Step 1: Registrant Information" runat="server" Selected="true"> |
<Items> |
<telerik:RadPanelItem Value="Step1" runat="server"> |
<ItemTemplate> |
....controls here and button |
</ItemTemplate> |
</telerik:RadPanelItem> |
</Items> |
</telerik:RadPanelItem> |
<telerik:RadPanelItem Enabled="False" Text="Step 2: Travel" runat="server"> |
<Items> |
<telerik:RadPanelItem Value="Step2" runat="server"> |
<ItemTemplate> |
<telerik:RadDatePicker ID="radArrivalDate" runat="server" TabIndex="116"> |
<DateInput runat="server" DateFormat="M/d/yyyy" DisplayDateFormat="M/d/yyyy"></DateInput> |
</telerik:RadDatePicker> |
</ItemTemplate> |
</telerik:RadPanelItem> |
</Items> |
</telerik:RadPanelItem> |
</Items> |
</telerik:RadPanelBar> |
0
Hi Stephen,
I am not able to reproduce the fault using RadDatePicker. You can find my modified project as an attachment. Can you modify it in a way that reproduces the fault?
Regards,
Genady Sergeev
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
I am not able to reproduce the fault using RadDatePicker. You can find my modified project as an attachment. Can you modify it in a way that reproduces the fault?
Regards,
Genady Sergeev
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Stephen
Top achievements
Rank 1
answered on 18 Feb 2010, 04:04 PM
So mysteriously my problems disappeared and that situation works for me now.
But I have a new problem...now within a RadPanelBar I have a GridView and I have controls within the GridView that I need to access in the codebehind for validation and other purposes. So I am able to define the GridView in the way discussed above, but when I attempt to loop through the GridView rows, the controls within are not coming back with the correct info.
Here is my page code:
Here is the codebehind:
The controls chkItem and txtQuantity are the ones I am trying having issues with. The checkbox is checked and the textbox has data in it but everytime it comes back that the checkbox is not checked and the textbox is empty.
Thanks,
Steve
But I have a new problem...now within a RadPanelBar I have a GridView and I have controls within the GridView that I need to access in the codebehind for validation and other purposes. So I am able to define the GridView in the way discussed above, but when I attempt to loop through the GridView rows, the controls within are not coming back with the correct info.
Here is my page code:
<telerik:RadPanelItem Enabled="False" Text="Add Ons" Value="Step2Parent" runat="server" Visible="false"> |
<Items> |
<telerik:RadPanelItem Value="Step2" runat="server"> |
<ItemTemplate> |
<asp:GridView ID="gvAddOns" runat="server" AutoGenerateColumns="False" Width="90%" CellPadding="8" GridLines="None" DataKeyNames="ID"> |
<Columns> |
<asp:TemplateField> |
<ItemTemplate> |
<asp:CheckBox ID="chkItem" runat="server" /> |
</ItemTemplate> |
<HeaderStyle Width="30px" /> |
</asp:TemplateField> |
<asp:BoundField DataField="Description" HeaderText="Item" HeaderStyle-Width="40%" HeaderStyle-HorizontalAlign="left" /> |
<asp:TemplateField HeaderText="Quantity"> |
<ItemTemplate> |
<asp:TextBox ID="txtQuantity" runat="server" Width="50px" MaxLength="4" /> |
</ItemTemplate> |
<HeaderStyle Width="50px" /> |
</asp:TemplateField> |
</Columns> |
</asp:GridView> |
</ItemTemplate> |
</telerik:RadPanelItem> |
</Items> |
</telerik:RadPanelItem> |
Here is the codebehind:
Dim gvAddOns As GridView = CType(RadPanelBar1.FindItemByValue("Step2").FindControl("gvAddOns"), GridView) |
For Each row As GridViewRow In gvAddOns.Rows |
Dim chkItem As CheckBox = CType(row.FindControl("chkItem"), CheckBox) |
Dim txtQuantity As TextBox = CType(row.FindControl("txtQuantity"), TextBox) |
If chkItem IsNot Nothing AndAlso chkItem.Checked Then |
bAddOnsChecked = True |
....... |
End If |
Next |
The controls chkItem and txtQuantity are the ones I am trying having issues with. The checkbox is checked and the textbox has data in it but everytime it comes back that the checkbox is not checked and the textbox is empty.
Thanks,
Steve
0
Hi Stephen,
In which event are you accessing these controls? Perhaps they are not yet databound. Can you try to hook on the ItemDataBound event of your RadGrid and try to access the controls there. Example:
Greetings,
Genady Sergeev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
In which event are you accessing these controls? Perhaps they are not yet databound. Can you try to hook on the ItemDataBound event of your RadGrid and try to access the controls there. Example:
Private Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As GridItemEventArgs)
If TypeOf e.Item Is GridDataItem Then
'Your find control logic goes here.
End If
End Sub
Greetings,
Genady Sergeev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Stephen
Top achievements
Rank 1
answered on 03 Mar 2010, 06:42 PM
Hello,
The grid has already been populated. This server side validation function is being called on a button click to save.
I am inserting the full page code and codebehind. The code in question is in the CustomValidation function in the codebehind.
Thanks,
Steve
The grid has already been populated. This server side validation function is being called on a button click to save.
I am inserting the full page code and codebehind. The code in question is in the CustomValidation function in the codebehind.
<%@ Page Language="vb" AutoEventWireup="false" MasterPageFile="~/includes/masterpages/standard_leftnav.Master" CodeBehind="addons.aspx.vb" Inherits="WorkshopsWorld.addons" |
title="Registration | Add Ons" %> |
<%@ Register TagPrefix="uc" TagName="LeftNav" Src="~/includes/navs/main.ascx" %> |
<asp:Content ID="Content1" ContentPlaceHolderID="NavContentPlaceHolder" runat="server"> |
<div id="nav"> |
<uc:LeftNav id="LeftNav" runat="server" /> |
<p><img border="0" src="/images/spacer.gif" height="1" alt=""></p> |
</div> |
</asp:Content> |
<asp:Content ID="Content2" ContentPlaceHolderID="MainContentPlaceHolder" runat="server"> |
<asp:HiddenField ID="hidRegistrationTypeID" runat="server" /> |
<asp:HiddenField ID="hidRegistrationID" runat="server" /> |
<p><b><asp:Label ID="lblStatus" runat="server" ForeColor="Red" Visible="False" /></b></p> |
<h4>Registration Add-Ons</h4> |
<asp:ObjectDataSource ID="odsCatalogList" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="dsCatalogList" TypeName="WorkshopsLibrary.CRegistration"> |
<SelectParameters> |
<asp:SessionParameter Name="sUserID" SessionField="UserID" Type="String" /> |
<asp:ControlParameter Name="sRegistrationTypeID" ControlID="hidRegistrationTypeID" Type="String" /> |
<asp:Parameter Name="bRequired" DefaultValue="False" Type="Boolean" /> |
<asp:QueryStringParameter Name="sWorkshopID" QueryStringField="wid" Type="String" /> |
</SelectParameters> |
</asp:ObjectDataSource> |
<p><b>Choose Optional Add-Ons:</b></p> |
<asp:GridView ID="gvAddOns" runat="server" AutoGenerateColumns="False" DataSourceID="odsCatalogList" Width="100%" GridLines="None" DataKeyNames="ID"> |
<Columns> |
<asp:TemplateField> |
<ItemTemplate> |
<asp:CheckBox ID="chkItem" runat="server" /> |
</ItemTemplate> |
<HeaderStyle Width="30px" /> |
</asp:TemplateField> |
<asp:BoundField DataField="Description" HeaderText="Item" HeaderStyle-Width="40%" HeaderStyle-HorizontalAlign="left" /> |
<asp:TemplateField HeaderText="Quantity"> |
<ItemTemplate> |
<asp:TextBox ID="txtQuantity" runat="server" Width="50px" MaxLength="4" /> |
</ItemTemplate> |
<HeaderStyle Width="50px" /> |
</asp:TemplateField> |
</Columns> |
</asp:GridView> |
<p><asp:Button ID="cmdSave" runat="server" Text="Continue" /></p> |
</asp:Content> |
Imports WorkshopsLibrary |
Partial Public Class addons |
Inherits System.Web.UI.Page |
Protected sUserID As String |
Protected sWorkshopID As String |
Protected bNewOrder As Boolean = False |
Protected bSomethingChecked As Boolean = False |
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load |
Dim cPageRoutines As New CPageRoutines |
Dim cRegistration As New CRegistration |
cPageRoutines.VerifyWorkshop("wid") |
cPageRoutines.VerifyPageNotVisited("AddOnsPageNotVisited") |
sWorkshopID = HttpContext.Current.Request.QueryString("wid") |
If (HttpContext.Current.Session("UserID") Is Nothing) Then |
sUserID = "" |
Else |
sUserID = HttpContext.Current.Session("UserID").ToString |
End If |
If (HttpContext.Current.Session(sWorkshopID & "-RegistrationID") Is Nothing) Then |
Response.Redirect("search.aspx?wid=" & sWorkshopID) |
Else |
hidRegistrationID.Value = HttpContext.Current.Session(sWorkshopID & "-RegistrationID").ToString |
End If |
cRegistration.LoadRegistrationTypeID(sUserID, hidRegistrationID.Value) |
hidRegistrationTypeID.Value = cRegistration.RegistrationTypeID.ToString |
If (HttpContext.Current.Session(sWorkshopID & "-OrderID") Is Nothing) Then |
bNewOrder = True |
End If |
End Sub |
Private Sub cmdSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSave.Click |
If Page.IsValid Then |
If CustomValidation() Then |
If bSomethingChecked Then |
SaveAddOns() |
Else |
Response.Redirect("payment.aspx?wid=" & sWorkshopID) |
End If |
Else |
lblStatus.Visible = True |
End If |
End If |
End Sub |
Private Function CustomValidation() As Boolean |
Dim bValid As Boolean = True |
Dim bMismatchItem As Boolean = False |
Dim bMismatchQuantity As Boolean = False |
For Each row As GridViewRow In gvAddOns.Rows |
Dim chkItem As CheckBox = CType(row.FindControl("chkItem"), CheckBox) |
Dim txtQuantity As TextBox = CType(row.FindControl("txtQuantity"), TextBox) |
If chkItem IsNot Nothing AndAlso chkItem.Checked Then |
bSomethingChecked = True |
If txtQuantity.Text = "" Then |
bMismatchItem = True |
End If |
Else |
If txtQuantity.Text <> "" Then |
bMismatchQuantity = True |
End If |
End If |
Next |
If bMismatchItem Then |
bValid = False |
lblStatus.Text = "Error: You must enter the quantity for the item you selected." |
End If |
If bMismatchQuantity Then |
bValid = False |
lblStatus.Text = "Error: You must check the item for which you have entered a quantity." |
End If |
Return bValid |
End Function |
Private Sub SaveAddOns() |
Dim cRegistration As New CRegistration |
Dim iRet As Integer |
Dim bRet As Boolean = False |
If bNewOrder Then |
iRet = cRegistration.AddOrder(sUserID, hidRegistrationID.Value, sWorkshopID) |
HttpContext.Current.Session(sWorkshopID & "-OrderID") = CStr(iRet) |
End If |
For Each row As GridViewRow In gvAddOns.Rows |
Dim chkItem As CheckBox = CType(row.FindControl("chkItem"), CheckBox) |
Dim txtQuantity As TextBox = CType(row.FindControl("txtQuantity"), TextBox) |
If chkItem IsNot Nothing AndAlso chkItem.Checked Then |
bRet = cRegistration.AddUpOrderItem(sUserID, _ |
HttpContext.Current.Session(sWorkshopID & "-OrderID").ToString, _ |
gvAddOns.DataKeys(row.RowIndex).Value.ToString, _ |
txtQuantity.Text) |
If Not bRet Then |
Exit For |
End If |
End If |
Next |
If bRet Then |
HttpContext.Current.Session("AddOnsPageNotVisited") = Nothing |
Response.Redirect("payment.aspx?wid=" & sWorkshopID) |
Else |
lblStatus.Text = "Save failed!" |
lblStatus.Visible = True |
End If |
End Sub |
End Class |
Thanks,
Steve
0
Hello Stephen,
As far as I can see you use template columns, however, you haven't bound the txtQuantity textbox to its related data item. This can be achieved the following way:
Then you need to call GridView.DataBind in order to evaluate the databinding expressions.
The problem that you experience is in no way related to RadPanelBar neither to any RadControls. I suggest that you check on the internet for more information on how to properly bind your GridView. You may start from here.
Kind regards,
Genady Sergeev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
As far as I can see you use template columns, however, you haven't bound the txtQuantity textbox to its related data item. This can be achieved the following way:
<asp:TemplateField HeaderText=
"Quantity"
>
<ItemTemplate>
<asp:TextBox ID=
"txtQuantity"
runat=
"server"
Width=
"50px"
MaxLength=
"4"
Text=
'<%# DataBinder.Eval(Container.DataItem, "Quantity") %>' />
</ItemTemplate>
</asp:TemplateField>
Then you need to call GridView.DataBind in order to evaluate the databinding expressions.
The problem that you experience is in no way related to RadPanelBar neither to any RadControls. I suggest that you check on the internet for more information on how to properly bind your GridView. You may start from here.
Kind regards,
Genady Sergeev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Stephen
Top achievements
Rank 1
answered on 09 Mar 2010, 03:32 PM
Hello,
I don't think my explanantion was clear. The "Quantity" textbox is not being populated by anything in the grid. The BoundField "Description" is being populated with data in the grid, the "Quantity" field is empty so the user can fill in the quantity and then submit. It is working almost like a catalog/shopping cart. The grid is being populated with a bunch of items with checkboxes and textboxes next to them. Once the grid is populated and the page is loaded, the user sees the items, checks the boxes of the items they want and fills in the appropriate textboxes for quantities. They then submit.
The exact code that I pasted was working perfectly on the page before I put it into a RadPanelBar.
I don't think my explanantion was clear. The "Quantity" textbox is not being populated by anything in the grid. The BoundField "Description" is being populated with data in the grid, the "Quantity" field is empty so the user can fill in the quantity and then submit. It is working almost like a catalog/shopping cart. The grid is being populated with a bunch of items with checkboxes and textboxes next to them. Once the grid is populated and the page is loaded, the user sees the items, checks the boxes of the items they want and fills in the appropriate textboxes for quantities. They then submit.
The exact code that I pasted was working perfectly on the page before I put it into a RadPanelBar.
0
Hi Stephen,
I have created sample project based on your code. I have RadPanelBar and GridView as an ItemTemplate. Inside the GridView there is a textbox where one can enter text. After a postback I am able to retrieve the text. Can you please modify my sample project in a way that it reproduces the issue?
Kind regards,
Genady Sergeev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
I have created sample project based on your code. I have RadPanelBar and GridView as an ItemTemplate. Inside the GridView there is a textbox where one can enter text. After a postback I am able to retrieve the text. Can you please modify my sample project in a way that it reproduces the issue?
Kind regards,
Genady Sergeev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Stephen
Top achievements
Rank 1
answered on 16 Mar 2010, 02:07 PM
Ok I feel stupid now. I figured out my problem...in my page I am binding the gridview to the datasource on the codebehind because based on certain conditions I may or may not show that RadPanelItem. So I bind the gridview to the datasource and then display that RadPanelItem if it meets the conditions. My problem is that I do the binding to the datasource in the Page_Load but I didn't have the code in a "Not IsPostBack" If block...so once I checked the checkbox, put something in the textbox and then submitted, when the page loaded again it reloaded the grid and cleared out my entries. Once I put the binding of the gridview into the "Not IsPostBack" it all worked correctly.
Thank you for your time, patience, and help!
Steve
Thank you for your time, patience, and help!
Steve