I am having a problem with the data being cleared out of the controls in the edit form template when a button is clicked on the template. (In the actual application, other events can also clear the objects on the form.) I have created two views on the form to show that the problem occurs with both asp type controls and telerik type controls (see Example 1.) The window on the left side has a telerik:radcontrolbox, asp:listbox, and an asp: button. The window on the right side has a asp:dropdownlist, asp:listbox, and an asp: button. It doesn’t matter which button is clicked (except for Cancel), all of the listbox, radcontrolbox, and dropdownlist controls are cleared out (see Example 2.) The controls on the top of the form are not in the edit form template and are not affected by this problem. I have supplied the ASPX and ASPX.VB below for your review and solution.
To reproduce the problem, click the “Edit” button to display the edit form template and then click the “Add” button or the “Update” button (which was setup to error instead of update during the test phase.) After clicking the “Add” button the form refreshes and the list boxes, the dropdown list, and the radcontrolbox no longer have values in them.
I have researched this problem for a week and have not been able to come up with a solution. I have seen countless answers on how to solve similar issues but none have given an answer that solved the problem in my application. I have tried everything I have read without a solution so I hope that if someone can help me, they will supply me the changes I need to make to my code and not provide me with a link that I’ve probably already been to.
============ |
Publish.aspx |
============ |
<%@ Page Language="VB" MasterPageFile="~/MasterPage.Master" AutoEventWireup="false" CodeFile="Publish.aspx.vb" Inherits="Publish" %> |
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> |
<%@ MasterType VirtualPath="~/MasterPage.Master" %> |
<asp:Content ID="Content1" ContentPlaceHolderID="cphBody" Runat="Server"> |
<asp:ScriptManager ID="ScriptManager1" runat="server" /> |
<script language="JavaScript" type="text/javascript" src="include/commonGrid.js"></script> |
<table cellspacing="0" cellpadding="0" width="600" border="0"> |
<tr> |
<td style="FONT:bold 14pt tahoma">Publish Templates</td> |
</tr> |
</table> |
<br /> |
<div id="divPage" runat="server"> |
<span class="defaultText">Filter by organization:</span> <asp:DropDownList id="ddlOrganization" runat="server" CssClass="dropDownList" DataTextField="Name" DataValueField="OrganizationId" |
AutoPostBack="True" OnSelectedIndexChanged="ddlOrganization_SelectedIndexChanged" /> <asp:CheckBox ID="cbxActiveOnly" runat="server" AutoPostBack="True" Text="Show active only" Checked="true" /> |
<br /> <br /> |
</div> |
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server"> |
<AjaxSettings> |
<telerik:AjaxSetting AjaxControlID="RadGrid1"> |
<UpdatedControls> |
<telerik:AjaxUpdatedControl ControlID="RadGrid1" /> |
</UpdatedControls> |
</telerik:AjaxSetting> |
</AjaxSettings> |
</telerik:RadAjaxManager> |
<telerik:RadGrid ID="RadGrid1" runat="server" |
AllowAutomaticInserts="True" PageSize="10" OnItemDataBound="RadGrid1_ItemDataBound" OnItemCommand="RadGrid1_ItemCommand" |
AllowSorting="true" AllowAutomaticUpdates="True" AllowPaging="True" AutoGenerateColumns="False" |
DataSourceID="SqlDataSource1" OnItemUpdated="RadGrid1_ItemUpdated" OnDataBound="RadGrid1_DataBound" |
OnItemInserted="RadGrid1_ItemInserted" ShowStatusBar="true" OnPreRender="RadGrid1_PreRender"> |
<PagerStyle Mode="NextPrevAndNumeric" /> |
<MasterTableView CommandItemDisplay="TopAndBottom" EditMode="EditForms" |
DataKeyNames="PublishTemplateId" DataSourceID="SqlDataSource1" EnableNoRecordsTemplate="true"> |
<NoRecordsTemplate>There are no templates to display.</NoRecordsTemplate> |
<Columns> |
<telerik:GridEditCommandColumn ButtonType="PushButton" /> |
<telerik:GridBoundColumn UniqueName="FileName" HeaderText="FileName" DataField="FileName" ReadOnly="True"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn UniqueName="Description" HeaderText="Description" DataField="Description" ReadOnly="True"> |
</telerik:GridBoundColumn> |
</Columns> |
<EditFormSettings EditFormType="Template"> |
<FormTemplate> |
<asp:HiddenField ID="hPublishTemplateID" Value='<%# Bind( "PublishTemplateID" ) %>' runat="server" /> |
<table><tr><td> |
<!--column 1--> |
<table id="Table1" cellspacing="2" cellpadding="1" width="420" border="2" rules="none" |
style="border-collapse: collapse; background: white;"> |
<tr> |
<td valign="top" align="right">Work Types: </td> |
<td valign="top" align="center"><asp:ListBox Rows="5" runat="server" ID="workTypeListBox1" Height="100px" Width="200px" /></td> |
<td valign="top" align="left"><telerik:RadComboBox ID="ddlWorkTypes1" runat="server" CssClass="dropDownList" DataTextField="Name" DataValueField="WorkTypesId" AutoPostBack="true" OnSelectedIndexChanged="ddlWorkTypes_SelectedIndexChanged" /> |
<br /><asp:Button ID="btnAddWorkType1" Text="Add" runat="server" CausesValidation="False" CommandName="AddWorkType1" OnClick="btnAddWorkType_Clicked" /></td> |
</tr> |
<tr> |
<td colspan="3" align="right"> |
<asp:Button ID="btnUpdate" Text='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "Insert", "Update") %>' |
runat="server" CommandName='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "PerformInsert", "Update")%>' /> |
|
<asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" |
CommandName="Cancel" /> |
</td> |
</tr> |
</table> |
</td><td> |
<!--column 2--> |
<table id="Table2" cellspacing="2" cellpadding="1" width="420" border="2" rules="none" |
style="border-collapse: collapse; background: white;"> |
<tr> |
<td valign="top" align="right">Work Types: </td> |
<td valign="top" align="center"><asp:ListBox Rows="5" runat="server" ID="workTypeListBox2" Height="100px" Width="200px" /></td> |
<td valign="top" align="left"><asp:DropDownList ID="ddlWorkTypes2" runat="server" CssClass="dropDownList" DataTextField="Name" DataValueField="WorkTypesId" AutoPostBack="true" OnSelectedIndexChanged="ddlWorkTypes_SelectedIndexChanged" /> |
<br /><asp:Button ID="btnAddWorkType2" Text="Add" runat="server" CausesValidation="False" CommandName="AddWorkType2" OnClick="btnAddWorkType_Clicked" /></td> |
</tr> |
<tr> |
<td colspan="3" align="right"> |
<asp:Button ID="Button2" Text='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "Insert", "Update") %>' |
runat="server" CommandName='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "PerformInsert", "Update")%>' /> |
|
<asp:Button ID="Button3" Text="Cancel" runat="server" CausesValidation="False" |
CommandName="Cancel" /> |
</td> |
</tr> |
</table> |
</td></tr></table> |
</FormTemplate> |
<EditColumn UniqueName="EditCommandColumn1"> |
</EditColumn> |
</EditFormSettings> |
</MasterTableView> |
</telerik:RadGrid> |
<asp:SqlDataSource ID="SqlDataSource1" runat="server" |
ConnectionString="<%$ ConnectionStrings:DBReadConnection %>" |
UpdateCommandType="StoredProcedure" UpdateCommand="Publish_Update" |
InsertCommandType="StoredProcedure" InsertCommand="Publish_Update" |
SelectCommandType="StoredProcedure" SelectCommand="Publish_Select"> |
<SelectParameters> |
<asp:ControlParameter Name="ai_i_OrganizationId" ControlID="ddlOrganization" PropertyName="SelectedValue" DefaultValue="-1" /> |
</SelectParameters> |
<UpdateParameters> |
<asp:Parameter Name="ai_i_OrganizationId" Type="Int32" /> |
<asp:Parameter Name="ai_vc_Description" Type="String" /> |
<asp:Parameter Name="ai_vc_FileName" Type="String" /> |
</UpdateParameters> |
<InsertParameters> |
<asp:Parameter Name="ai_i_OrganizationId" Type="Int32" /> |
<asp:Parameter Name="ai_vc_Description" Type="String" /> |
<asp:Parameter Name="ai_vc_FileName" Type="String" /> |
</InsertParameters> |
</asp:SqlDataSource> |
</asp:Content> |
=============== |
Publish.aspx.vb |
=============== |
Imports System.Web.UI.WebControls |
Imports System.Data |
Imports Telerik.Web.UI |
Imports System.Drawing |
Partial Class Publish |
Inherits System.Web.UI.Page |
#Region " Global Page Variables " |
Public Shared gint_OrganizationId As Integer = -1 |
Public Shared gstr_buttonClickedID As String = "" |
Protected Shared gddlID As String = "" |
Protected Shared gddlSelectedText As String = "" |
Protected Shared gddlSelectedValue As String = "" |
Protected Shared gddlSelectedIndex As Integer = -1 |
#End Region |
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) |
If Not Page.IsPostBack Then |
BindOrganizationList() |
End If |
End Sub |
Private Sub AddWorkType(ByRef workTypeListBox As ListBox, ByVal ddlID As String) |
'add a work type to the list box |
If gddlSelectedIndex > -1 And gddlID = ddlID Then |
workTypeListBox.Items.Add(gddlSelectedText) |
gddlID = "" 'reset state |
gddlSelectedIndex = -1 'reset state |
End If |
End Sub |
Public Sub BindOrganizationList() |
'build organization dropdown list based on user type |
Dim ds As New DataSet |
Try |
Dim clsOrganizationDB As New OrganizationDB(CommonFunctions.UserId) |
ds = clsOrganizationDB.GetOrganizationDropDownList() |
ddlOrganization.DataSource = ds |
ddlOrganization.DataBind() |
Try |
ddlOrganization.SelectedIndex = 0 |
gint_OrganizationId = ddlOrganization.SelectedValue |
Catch |
gint_OrganizationId = 0 |
End Try |
Catch sqlex As SqlClient.SqlException |
SetMessage("BindOrganizationList() Failed: " & sqlex.ToString) |
Catch ex As Exception |
SetMessage("BindOrganizationList() Failed: " & ex.ToString) |
End Try |
End Sub |
Private Sub BindWorkTypeList(ByRef ddlObj As Object, ByVal i_active As Integer, ByVal boxType As String) |
'build work type radcombobox list based on organization |
Try |
Dim ddlWorkTypes As Object |
ddlWorkTypes = ddlObj |
If boxType = "rcb" Then 'RadComboBox |
ddlWorkTypes = DirectCast(ddlWorkTypes, RadComboBox) |
Else |
'dropdown list |
ddlWorkTypes = DirectCast(ddlWorkTypes, DropDownList) |
End If |
ddlWorkTypes.Items.Clear() 'clear current list |
Dim storedProc As String = "SEL_WorkTypes" |
Dim dbConn As New SqlClient.SqlConnection(CommonFunctions.GetREADConnectionString()) |
dbConn.Open() |
Dim sqlCmd As SqlClient.SqlCommand = dbConn.CreateCommand() |
sqlCmd.CommandType = CommandType.StoredProcedure |
sqlCmd.CommandText = storedProc |
sqlCmd.Parameters.Add("ai_i_OrganizationId", SqlDbType.Int).Value = gint_OrganizationId |
sqlCmd.Parameters.Add("ai_i_IsActive", SqlDbType.Int).Value = i_active |
sqlCmd.Connection = dbConn |
Dim sa As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(sqlCmd) |
Dim dt = New DataTable |
sa.Fill(dt) |
Dim dv As DataView = New DataView(dt) |
dv.Sort = "Description" |
'build list here |
ddlWorkTypes.DataSource = dv |
ddlWorkTypes.DataTextField = "Description" |
ddlWorkTypes.DataValueField = "TranscriptionWorkTypeID" |
ddlWorkTypes.DataBind() |
If boxType = "rcb" Then 'RadComboBox |
ddlWorkTypes.Items.Insert(0, New RadComboBoxItem("Select Work Type", "")) |
Else |
'dropdown list |
ddlWorkTypes.Items.Insert(0, New ListItem("Select Work Type", "")) |
End If |
dbConn.Close() |
Catch sqlex As SqlClient.SqlException |
SetMessage("BindWorkTypeList() Failed: " & sqlex.ToString) |
Catch ex As Exception |
SetMessage("BindWorkTypeList() Failed: " & ex.ToString) |
End Try |
End Sub |
Private Sub BindWorkTypeListBox(ByRef workTypeListBox As ListBox, ByVal i_PublishTemplateId As Integer) |
'build work type listbox based on xref with publish template |
'hidden listbox contains IDs for updating database |
Try |
workTypeListBox.Items.Clear() 'clear current list |
Dim item As New ListItem() |
Dim storedProc As String = "Publish_SelectWorkType" |
Dim dbConn As New SqlClient.SqlConnection(CommonFunctions.GetREADConnectionString()) |
dbConn.Open() |
Dim sqlCmd As SqlClient.SqlCommand = dbConn.CreateCommand() |
sqlCmd.CommandType = CommandType.StoredProcedure |
sqlCmd.CommandText = storedProc |
sqlCmd.Parameters.Add("ai_i_PublishTemplateId", SqlDbType.Int).Value = i_PublishTemplateId |
sqlCmd.Connection = dbConn |
Dim sdr As SqlClient.SqlDataReader = sqlCmd.ExecuteReader() |
'initialize listboxes |
While workTypeListBox.SelectedIndex <> -1 |
workTypeListBox.Items.Remove(workTypeListBox.SelectedItem.Value) |
End While |
'load listboxes |
While sdr.Read |
workTypeListBox.Items.Add(CStr(sdr.Item("Description"))) |
End While |
dbConn.Close() |
workTypeListBox.DataBind() |
Catch sqlex As SqlClient.SqlException |
SetMessage("BindWorkTypeListBox() Failed: " & sqlex.ToString) |
Catch ex As Exception |
SetMessage("BindWorkTypeListBox() Failed: " & ex.ToString) |
End Try |
End Sub |
Protected Sub ddlOrganization_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) |
gint_OrganizationId = ddlOrganization.SelectedValue 'update global value |
RadGrid1.Rebind() |
RadGrid1.MasterTableView.ClearEditItems() 'force items out of edit mode so that update/cancel buttons are hidden on initial load |
End Sub |
Private Sub DisplayMessage(ByVal text As String) |
RadGrid1.Controls.Add(New LiteralControl(String.Format("<span style='font-weight:bold;color:red'>{0}</span>", text))) |
End Sub |
Private gridMessage As String = Nothing |
Protected Sub RadGrid1_DataBound(ByVal sender As Object, ByVal e As EventArgs) |
If Not String.IsNullOrEmpty(gridMessage) Then |
DisplayMessage(gridMessage) |
End If |
End Sub |
Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) |
'a button has been clicked |
gstr_buttonClickedID = e.CommandName |
If gstr_buttonClickedID = "AddWorkType1" Or gstr_buttonClickedID = "AddWorkType2" Then |
RadGrid1.DataSource = Nothing |
'call the Rebind() method after nullifying the data source |
RadGrid1.Rebind() 'force itemdatabound event on events that don't cause them |
End If |
End Sub |
Protected Sub btnAddWorkType_Clicked(ByVal sender As Object, ByVal e As EventArgs) |
'just a place keeper so that the itemcommand can fire and then the |
'itemdatabound will fire next and the worktype will be added |
End Sub |
Protected Sub ddlWorkTypes_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) |
'dropdown list changed |
Dim sName As String = sender.GetType.Name |
Dim ddl As Object |
If sName = "RaComboBox" Then |
ddl = DirectCast(ddl, RadComboBox) |
Else |
ddl = DirectCast(ddl, DropDownList) |
End If |
ddl = sender |
If ddl.SelectedIndex > -1 Then |
'save the results because they will be lost when the "add" button is clicked |
gddlID = ddl.ID |
gddlSelectedText = ddl.SelectedItem.Text.ToString |
gddlSelectedValue = ddl.SelectedItem.Value.ToString |
gddlSelectedIndex = ddl.SelectedIndex |
Else |
gddlID = "" |
gddlSelectedIndex = -1 |
End If |
End Sub |
Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As GridItemEventArgs) |
'active when a button is clicked on the form |
'objects are located here when a grid editable item is in edit mode otherwise you can't find these objects behind the radgrd |
If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then |
'edit type button has been clicked |
'setup fields for conditional postback and validation |
Dim editItem As GridEditableItem = DirectCast(e.Item, GridEditableItem) |
'do nothing unless an event has been initiated by user |
If gstr_buttonClickedID <> "" Then |
'setup objects for different button clicks |
Dim workTypeListBox1 As ListBox = DirectCast(e.Item.FindControl("workTypeListBox1"), ListBox) |
Dim workTypeListBox2 As ListBox = DirectCast(e.Item.FindControl("workTypeListBox2"), ListBox) |
Dim ddlWorkTypes1 As RadComboBox = DirectCast(e.Item.FindControl("ddlWorkTypes1"), RadComboBox) |
Dim ddlWorkTypes2 As DropDownList = DirectCast(e.Item.FindControl("ddlWorkTypes2"), DropDownList) |
'<asp:DropDownList id="ddlWorkTypes" runat="server" CssClass="dropDownList" DataTextField="Name" DataValueField="WorkTypesId" |
' AutoPostBack="True" OnSelectedIndexChanged="ddlWorkTypes_SelectedIndexChanged" /> |
If gstr_buttonClickedID = "Edit" Then |
'bind list boxes in edit template |
Dim hPublishTemplateID As HiddenField = DirectCast(e.Item.FindControl("hPublishTemplateID"), HiddenField) |
Dim publishTempID As Integer = CInt(hPublishTemplateID.Value) |
BindWorkTypeListBox(workTypeListBox1, publishTempID) |
BindWorkTypeListBox(workTypeListBox2, publishTempID) |
'bind ddl's in edit template |
Dim i_active = 1 'only get active work types |
BindWorkTypeList(ddlWorkTypes1, i_active, "rcb") 'setup ddlWorkTypes dropdown list |
BindWorkTypeList(ddlWorkTypes2, i_active, "ddl") 'setup ddlWorkTypes dropdown list |
ElseIf gstr_buttonClickedID = "AddWorkType1" Then |
'add a work type to the listbox |
AddWorkType(workTypeListBox1, "ddlWorkTypes1") |
ElseIf gstr_buttonClickedID = "AddWorkType2" Then |
'add a work type to the listbox |
AddWorkType(workTypeListBox2, "ddlWorkTypes2") |
End If |
gstr_buttonClickedID = "" 'reset |
End If |
End If |
End Sub |
Protected Sub RadGrid1_ItemInserted(ByVal source As Object, ByVal e As Telerik.Web.UI.GridInsertedEventArgs) |
'template insert |
If Not e.Exception Is Nothing Then |
e.ExceptionHandled = True |
e.KeepInInsertMode = True |
SetMessage("Template cannot be inserted. Reason: " + e.Exception.Message) |
Else |
SetMessage("New template is inserted!") |
End If |
End Sub |
Protected Sub RadGrid1_ItemUpdated(ByVal source As Object, ByVal e As Telerik.Web.UI.GridUpdatedEventArgs) |
'template update |
If Not e.Exception Is Nothing Then |
e.KeepInEditMode = True |
e.ExceptionHandled = True |
SetMessage("Template cannot be updated. Reason: " + e.Exception.Message) |
Else |
Dim item As GridEditableItem = DirectCast(e.Item, GridEditableItem) |
Dim id As String = item.GetDataKeyValue("PublishTemplateId").ToString() |
SetMessage("Template with ID " + id + "is updated!") |
End If |
End Sub |
Protected Sub RadGrid1_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) |
End Sub |
Protected Sub RadGrid1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) |
If (Not Page.IsPostBack) Then |
Dim i As Integer |
For i = 0 To RadGrid1.PageSize - 1 |
RadGrid1.EditIndexes.Add(i) |
Next i |
BindOrganizationList() |
Me.RadGrid1.MasterTableView.Rebind() |
RadGrid1.Rebind() |
End If |
End Sub |
Private Sub SetMessage(ByVal message As String) |
gridMessage = message.Replace(vbCrLf, "<br />") |
End Sub |
End Class |