Hello Telerik,
I am working on a project and I have used the Grid popup edit with UserControl as seen on the following link
everything works as expected I click on the edit button and a popup window opens and displays the user control but the data wont bind and I am at a loss to explain why.
How can I resolve this issue??
please find attached the code for xd_ViewContact and xd_EditContact
Cheers.
xd_ViewContact.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="xd_ViewContact.ascx.cs" |
Inherits="xDynamics.Controls.View.xd_ViewContact" %> |
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %> |
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> |
<script type="text/javascript"> |
function RowSelected(row) { |
} |
function RowDblClick(sender, eventArgs) { |
sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical()); |
} |
</script> |
<div> |
<telerik:RadGrid ID="rgCustomerContact" runat="server" AllowPaging="True" |
AllowSorting="True" OnPreRender="rgCustomerContact_PreRender" |
GridLines="None" OnUpdateCommand="rgCustomerContact_UpdateCommand" OnInsertCommand="rgCustomerContact_InsertCommand" |
OnDeleteCommand="rgCustomerContact_DeleteCommand" OnItemCommand="rgCustomerContact_ItemCommand" |
PageSize="9" OnNeedDataSource="BindGrid" AutoGenerateColumns="False"> |
<ClientSettings AllowColumnsReorder="True" EnablePostBackOnRowClick="true" ReorderColumnsOnClient="True"> |
<Selecting AllowRowSelect="True" /> |
<ClientEvents OnRowSelected="RowSelected" /> |
<ClientEvents OnRowDblClick="RowDblClick" /> |
</ClientSettings> |
<MasterTableView DataKeyNames="CustomerContactID" CommandItemDisplay="Top" EditMode="PopUp"> |
<Columns> |
<telerik:GridEditCommandColumn UniqueName="EditCommandColumn" ButtonType="ImageButton" |
EditImageUrl="~/App_Themes/xDynamics/images/Edit.gif"> |
</telerik:GridEditCommandColumn> |
<telerik:GridBoundColumn UniqueName="ContactID" HeaderText="ContactID" DataField="CustomerContactID"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn UniqueName="FirstName" HeaderText="First Name" DataField="CustomerContactID_FirstName"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn UniqueName="Surname" HeaderText="Surname" DataField="CustomerContactID_LastName"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn UniqueName="Phone" HeaderText="Phone" DataField="CustomerContactID_PhoneNumber"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn UniqueName="Mobile" HeaderText="Mobile" DataField="CustomerContactID_MobileNumber"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn UniqueName="Fax" HeaderText="Fax" DataField="CustomerContactID_FaxNumber"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn UniqueName="Email" HeaderText="Email" DataField="CustomerContactID_Email"> |
</telerik:GridBoundColumn> |
</Columns> |
<RowIndicatorColumn> |
<HeaderStyle Width="20px"></HeaderStyle> |
</RowIndicatorColumn> |
<ExpandCollapseColumn> |
<HeaderStyle Width="20px"></HeaderStyle> |
</ExpandCollapseColumn> |
<EditFormSettings UserControlName="~/Applications/xDynamics/Controls/Forms/Edit/xd_EditContact.ascx" |
EditFormType="WebUserControl" InsertCaption="Add new item" CaptionFormatString="Edit CustomerContactID: {0}" |
CaptionDataField="CustomerContactID"> |
<PopUpSettings Width="500px" /> |
<EditColumn UniqueName="EditCommandColumn1" > |
</EditColumn> |
</EditFormSettings> |
</MasterTableView> |
</telerik:RadGrid> |
</div> |
<div> |
<ajaxToolkit:Accordion ID="Accordion1" runat="server" SelectedIndex="0" HeaderCssClass="accordionHeaderDeselected" |
HeaderSelectedCssClass="accordionHeaderSelected" ContentCssClass="accordionContent" |
FadeTransitions="false" FramesPerSecond="40" TransitionDuration="250" Visible="false" |
RequireOpenedPane="false" SuppressHeaderPostbacks="true"> |
<Panes> |
<ajaxToolkit:AccordionPane ID="AccordionPane1" runat="server"> |
<Header> |
<a href="" style="text-decoration: none">Contact Details</a></Header> |
<Content> |
<table id="tblPan1" width="500px"> |
<tr runat="server" width="150px" id="trCustomerTypeID" visible="False"> |
<td> |
CustomerContactID |
</td> |
<td> |
<asp:Label ID="lblCustomerContactID" runat="server"> |
</asp:Label> |
</td> |
</tr> |
<tr runat="server" width="150px" id="tr1" visible="False"> |
<td> |
CustomerID |
</td> |
<td> |
<asp:Label ID="lblCustomerID" runat="server"> |
</asp:Label> |
</td> |
</tr> |
<tr runat="server" width="150px" id="tr2"> |
<td> |
Title |
</td> |
<td> |
<asp:TextBox ID="txtTitle" runat="server" Width="300" ReadOnly="True"> |
</asp:TextBox> |
</td> |
<td> |
Position |
</td> |
<td> |
<asp:TextBox ID="txtPosition" runat="server" Width="300" ReadOnly="True"> |
</asp:TextBox> |
</td> |
</tr> |
<tr runat="server" width="150px" id="tr3"> |
<td> |
First Name |
</td> |
<td> |
<asp:TextBox ID="txtFirstName" runat="server" Width="300" ReadOnly="True"> |
</asp:TextBox> |
</td> |
<td> |
Last Name |
</td> |
<td> |
<asp:TextBox ID="txtLastName" runat="server" Width="300" ReadOnly="True"> |
</asp:TextBox> |
</td> |
</tr> |
<tr runat="server" width="150px" id="tr6"> |
<td width="150px"> |
Phone |
</td> |
<td> |
<asp:TextBox ID="txtPhone" runat="server" Width="300" ReadOnly="True"> |
</asp:TextBox> |
</td> |
<td> |
Mobile |
</td> |
<td> |
<asp:TextBox ID="txtMobile" runat="server" Width="300" ReadOnly="True"> |
</asp:TextBox> |
</td> |
</tr> |
<tr runat="server" width="150px" id="tr8"> |
<td> |
Fax |
</td> |
<td> |
<asp:TextBox ID="txtFax" runat="server" Width="300" ReadOnly="True"> |
</asp:TextBox> |
</td> |
<td> |
</td> |
<td> |
<asp:TextBox ID="txtEmail" runat="server" Width="300" ReadOnly="True"> |
</asp:TextBox> |
</td> |
</tr> |
</table> |
</Content> |
</ajaxToolkit:AccordionPane> |
<ajaxToolkit:AccordionPane ID="AccordionPane2" runat="server"> |
<Header> |
<a href="" style="text-decoration: none">Contact Mailing</a></Header> |
<Content> |
<table id="Table1" width="500px"> |
<tr runat="server" width="150px" id="tr10"> |
<td> |
Address |
</td> |
<td> |
<asp:TextBox ID="txtAddress" runat="server" Width="300" ReadOnly="True"> |
</asp:TextBox> |
</td> |
<td> |
Country |
</td> |
<td> |
<asp:TextBox ID="txtCountry" runat="server" Width="300" ReadOnly="True"> |
</asp:TextBox> |
</td> |
</tr> |
<tr runat="server" width="150px" id="tr12"> |
<td> |
State |
</td> |
<td> |
<asp:TextBox ID="txtState" runat="server" Width="300" ReadOnly="True"> |
</asp:TextBox> |
</td> |
<td> |
Location |
</td> |
<td> |
<asp:TextBox ID="txtLocation" runat="server" Width="300" ReadOnly="True"> |
</asp:TextBox> |
</td> |
</tr> |
<tr runat="server" width="150px" id="tr14"> |
<td> |
Postcode |
</td> |
<td> |
<asp:TextBox ID="txtPostcode" runat="server" Width="300" ReadOnly="True"> |
</asp:TextBox> |
</td> |
</tr> |
<tr> |
<td> |
Active? |
</td> |
<td> |
<asp:CheckBox ID="chkActive" runat="server" Enabled="False"></asp:CheckBox> |
</td> |
<td> |
Training Account |
</td> |
<td> |
<asp:CheckBox ID="chkTrainingAccount" runat="server" Enabled="False" /> |
</td> |
</tr> |
<tr runat="server" id="trOrganiserActive"> |
<td> |
Organiser Active |
</td> |
<td> |
<asp:CheckBox ID="chkOrganiserActive" runat="server" Enabled="False" /> |
</td> |
</tr> |
<tr runat="server" width="150px" id="tr16"> |
<td> |
Edited By |
</td> |
<td> |
<asp:TextBox ID="txtEditedBy" runat="server" Width="300" Enabled="False"> |
</asp:TextBox> |
</td> |
<td> |
Edited Date |
</td> |
<td> |
<asp:TextBox ID="txtEditedDate" runat="server" Width="300" Enabled="False"> |
</asp:TextBox> |
</td> |
</tr> |
</table> |
</Content> |
</ajaxToolkit:AccordionPane> |
</Panes> |
</ajaxToolkit:Accordion> |
</div> |
xd_ViewContact.ascx.cs
using System; |
using System.Collections; |
using System.Collections.Generic; |
using System.Configuration; |
using System.Data; |
using System.Data.SqlClient; |
using System.Linq; |
using System.Web; |
using System.Web.UI; |
using System.Web.UI.WebControls; |
using Telerik.Web.UI; |
namespace xDynamics.Controls.View |
{ |
public partial class xd_ViewContact : UserControl |
{ |
DataAccess data = new DataAccess(); |
protected void Page_Init(object sender, EventArgs e) |
{ |
Accordion1.FindControl("nothing"); |
} |
public void Page_Load(object sender, EventArgs e) |
{ |
} |
public void FunctionPageLoad() |
{ |
if (Geek.CustomerID != null) |
{ |
lblCustomerID.Text = Geek.CustomerID.ToString(); |
} |
Accordion1.Visible = false; |
} |
public void RebindGridFunction() |
{ |
rgCustomerContact.Rebind(); |
} |
protected void rgCustomerContact_ItemCommand(object source, GridCommandEventArgs e) |
{ |
Geek.xd_CustomerContactID = rgCustomerContact.SelectedValue; |
BindCustomerContact(Geek.xd_CustomerContactID.ToString()); |
Accordion1.Visible = true; |
} |
protected void rgCustomerContact_PreRender(object sender, EventArgs e) |
{ |
if (!this.IsPostBack) |
{ |
//this.rgCustomerContact.MasterTableView.Items[1].Edit = true; |
this.rgCustomerContact.MasterTableView.Rebind(); |
} |
} |
protected void BindGrid(object source, GridNeedDataSourceEventArgs e) |
{ |
this.rgCustomerContact.DataSource = this.Contacts; |
this.Contacts.PrimaryKey = new DataColumn[] { this.Contacts.Columns["CustomerContactID"] }; |
} |
public void BindCustomerContact(string CustomerContactID) |
{ |
if (!String.IsNullOrEmpty(Geek.xd_CustomerContactID.ToString())) |
{ |
string sql = string.Format("SELECT data_customer_contact.CustomerContactID, data_customer_contact.CustomerID, data_lookup_tables_1.LookupID_LookupTable AS Title, data_customer_contact.CustomerContactID_FirstName, data_customer_contact.CustomerContactID_LastName, data_customer_contact.CustomerContactID_PositionTitle, data_customer_contact.CustomerContactID_PhoneNumber, data_customer_contact.CustomerContactID_MobileNumber, data_customer_contact.CustomerContactID_FaxNumber, data_customer_contact.CustomerContactID_Email, data_customer_contact.CustomerContactID_Address, data_lookup_tables_3.LookupID_LookupTable AS LookupID_Country, data_lookup_tables_5.LookupID_LookupTable AS LookupID_State, data_lookup_tables_2.LookupID_LookupTable AS LookupID_Location, data_lookup_tables_4.LookupID_LookupTable AS LookupID_Postcode, data_customer_contact.CustomerContactID_Active, IsNull(data_customer_contact.TrainingActive,0) AS TrainingActive, IsNull(data_customer_contact.OrganiserActive,0) AS OrganiserActive, data_customer_contact.CustomerContactID_EditedBy, data_customer_contact.CustomerContactID_EditedDate FROM Xtrain.data_customer_contact LEFT OUTER JOIN Xtrain.data_lookup_tables AS data_lookup_tables_4 ON Xtrain.data_customer_contact.LookupID_Postcode = data_lookup_tables_4.LookupID LEFT OUTER JOIN Xtrain.data_lookup_tables AS data_lookup_tables_2 ON Xtrain.data_customer_contact.LookupID_Location = data_lookup_tables_2.LookupID LEFT OUTER JOIN Xtrain.data_lookup_tables AS data_lookup_tables_5 ON Xtrain.data_customer_contact.LookupID_State = data_lookup_tables_5.LookupID LEFT OUTER JOIN Xtrain.data_lookup_tables AS data_lookup_tables_3 ON Xtrain.data_customer_contact.LookupID_Country = data_lookup_tables_3.LookupID LEFT OUTER JOIN Xtrain.data_lookup_tables AS data_lookup_tables_1 ON Xtrain.data_customer_contact.CustomerContactID_ContactTitle = data_lookup_tables_1.LookupID WHERE (data_customer_contact.CustomerContactID = {0})", CustomerContactID); |
DataTable dt = data.GetDataTable(sql); |
if (dt.Rows.Count > 0) |
{ |
lblCustomerID.Text = dt.Rows[0]["CustomerID"].ToString(); |
if (!String.IsNullOrEmpty(dt.Rows[0]["CustomerContactID"].ToString())) |
{ |
lblCustomerContactID.Text = dt.Rows[0]["CustomerContactID"].ToString(); |
//Geek.xd_CustomerContactID = lblCustomerContactID.Text; |
} |
txtTitle.Text = dt.Rows[0]["Title"].ToString(); |
txtPosition.Text = dt.Rows[0]["CustomerContactID_PositionTitle"].ToString(); |
txtFirstName.Text = dt.Rows[0]["CustomerContactID_FirstName"].ToString(); |
txtLastName.Text = dt.Rows[0]["CustomerContactID_LastName"].ToString(); |
txtPhone.Text = dt.Rows[0]["CustomerContactID_PhoneNumber"].ToString(); |
txtMobile.Text = dt.Rows[0]["CustomerContactID_MobileNumber"].ToString(); |
txtFax.Text = dt.Rows[0]["CustomerContactID_FaxNumber"].ToString(); |
txtEmail.Text = dt.Rows[0]["CustomerContactID_Email"].ToString(); |
txtAddress.Text = dt.Rows[0]["CustomerContactID_Address"].ToString(); |
txtCountry.Text = dt.Rows[0]["LookupID_Country"].ToString(); |
txtState.Text = dt.Rows[0]["LookupID_State"].ToString(); |
txtLocation.Text = dt.Rows[0]["LookupID_Location"].ToString(); |
txtPostcode.Text = dt.Rows[0]["LookupID_Postcode"].ToString(); |
if (!String.IsNullOrEmpty(dt.Rows[0]["CustomerContactID_Active"].ToString())) |
{ |
chkActive.Checked = Convert.ToBoolean(dt.Rows[0]["CustomerContactID_Active"].ToString()); |
} |
if (!String.IsNullOrEmpty(dt.Rows[0]["TrainingActive"].ToString())) |
{ |
chkTrainingAccount.Checked = Convert.ToBoolean(dt.Rows[0]["TrainingActive"]); |
} |
if (!String.IsNullOrEmpty(dt.Rows[0]["OrganiserActive"].ToString())) |
{ |
chkOrganiserActive.Checked = Convert.ToBoolean(dt.Rows[0]["OrganiserActive"]); |
} |
txtEditedBy.Text = dt.Rows[0]["CustomerContactID_EditedBy"].ToString(); |
txtEditedDate.Text = dt.Rows[0]["CustomerContactID_EditedDate"].ToString(); |
} |
dt.Dispose(); |
} |
Geek.xd_CustomerContactID = lblCustomerContactID.Text; |
} |
private static DataTable GetDataTable(string queryString) |
{ |
String ConnString = ConfigurationManager.ConnectionStrings["XDEVConnect"].ConnectionString; |
SqlConnection MySqlConnection = new SqlConnection(ConnString); |
SqlDataAdapter MySqlDataAdapter = new SqlDataAdapter(); |
MySqlDataAdapter.SelectCommand = new SqlCommand(queryString, MySqlConnection); |
DataTable myDataTable = new DataTable(); |
MySqlConnection.Open(); |
try |
{ |
MySqlDataAdapter.Fill(myDataTable); |
} |
finally |
{ |
MySqlConnection.Close(); |
} |
return myDataTable; |
} |
private DataTable Contacts |
{ |
get |
{ |
object obj = this.Session["Contacts"]; |
if ((!(obj == null))) |
{ |
return ((DataTable)(obj)); |
} |
DataTable myContactsTable = new DataTable(); |
myContactsTable = GetDataTable("SELECT data_customer_contact.CustomerContactID, data_customer_contact.CustomerID, data_customer_contact.CustomerContactID_ContactTitle, data_customer_contact.CustomerContactID_FirstName, data_customer_contact.CustomerContactID_LastName, data_customer_contact.CustomerContactID_PositionTitle, data_customer_contact.CustomerContactID_PhoneNumber, data_customer_contact.CustomerContactID_MobileNumber, data_customer_contact.CustomerContactID_FaxNumber, data_customer_contact.CustomerContactID_Email, data_customer_contact.CustomerContactID_Address, data_customer_contact.LookupID_Country, data_customer_contact.LookupID_State, data_customer_contact.LookupID_Location, data_customer_contact.LookupID_Postcode, data_customer_contact.CustomerContactID_Active, data_customer_contact.CustomerContactID_EditedBy, data_customer_contact.CustomerContactID_EditedDate, IsNull(data_customer_contact.TrainingActive,0) AS TrainingActive, IsNull(data_customer_contact.OrganiserActive,0) AS OrganiserActive FROM Xtrain.data_customer_contact WHERE (CustomerID = " + Geek.CustomerID + ")"); |
this.Session["Contacts"] = myContactsTable; |
return myContactsTable; |
} |
} |
protected void rgCustomerContact_UpdateCommand(object source, GridCommandEventArgs e) |
{ |
GridEditableItem editedItem = e.Item as GridEditableItem; |
UserControl ucEditContact = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID); |
//Prepare new row to add it in the DataSource |
DataRow[] changedRows = this.Contacts.Select("CustomerContactID = " + editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["CustomerContactID"]); |
if (changedRows.Length != 1) |
{ |
rgCustomerContact.Controls.Add(new LiteralControl("Unable to locate the Employee for updating.")); |
e.Canceled = true; |
return; |
} |
//Update new values |
Hashtable newValues = new Hashtable(); |
newValues["CustomerID"] = (ucEditContact.FindControl("ddlCompany") as DropDownList).SelectedItem.Value; |
newValues["CustomerContactID_ContactTitle"] = (ucEditContact.FindControl("ddlTitle") as DropDownList).SelectedItem.Value; |
newValues["CustomerContactID_FirstName"] = (ucEditContact.FindControl("txtFirstName") as TextBox).Text; |
newValues["CustomerContactID_LastName"] = (ucEditContact.FindControl("txtLastName") as TextBox).Text; |
newValues["CustomerContactID_PositionTitle"] = (ucEditContact.FindControl("txtPosition") as TextBox).Text; |
newValues["CustomerContactID_PhoneNumber"] = (ucEditContact.FindControl("txtPhone") as TextBox).Text; |
newValues["CustomerContactID_MobileNumber"] = (ucEditContact.FindControl("txtMobile") as TextBox).Text; |
newValues["CustomerContactID_FaxNumber"] = (ucEditContact.FindControl("txtFax") as TextBox).Text; |
newValues["CustomerContactID_Address"] = (ucEditContact.FindControl("txtAddress") as TextBox).Text; |
newValues["CustomerContactID_Notes"] = (ucEditContact.FindControl("txtNotes") as TextBox).Text; |
newValues["CustomerContactID_Email"] = (ucEditContact.FindControl("txtEmail") as TextBox).Text; |
newValues["LookupID_Country"] = (ucEditContact.FindControl("ddlCountry") as DropDownList).SelectedItem.Value; |
newValues["LookupID_State"] = (ucEditContact.FindControl("ddlState") as DropDownList).SelectedItem.Value; |
newValues["LookupID_Location"] = (ucEditContact.FindControl("ddlLocation") as DropDownList).SelectedItem.Value; |
newValues["LookupID_Postcode"] = (ucEditContact.FindControl("ddlPostcode") as DropDownList).SelectedItem.Value; |
newValues["CustomerContactID_Active"] = (ucEditContact.FindControl("chkActive") as CheckBox).Checked; |
newValues["TrainingActive"] = (ucEditContact.FindControl("chkTrainingAccount") as CheckBox).Checked; |
newValues["OrganiserActive"] = (ucEditContact.FindControl("chkOrganiserActive") as CheckBox).Checked; |
newValues["CustomerContactID_EditedBy"] = (ucEditContact.FindControl("txtEditedBy") as TextBox).Text; |
newValues["CustomerContactID_EditedDate"] = (ucEditContact.FindControl("txtEditedDate") as TextBox).Text; |
changedRows[0].BeginEdit(); |
try |
{ |
foreach (DictionaryEntry entry in newValues) |
{ |
changedRows[0][(string)entry.Key] = entry.Value; |
} |
changedRows[0].EndEdit(); |
this.Contacts.AcceptChanges(); |
} |
catch (Exception ex) |
{ |
changedRows[0].CancelEdit(); |
Label lblError = new Label(); |
lblError.Text = "Unable to update Contact. Reason: " + ex.Message; |
lblError.ForeColor = System.Drawing.Color.Red; |
rgCustomerContact.Controls.Add(lblError); |
e.Canceled = true; |
} |
} |
protected void rgCustomerContact_InsertCommand(object source, GridCommandEventArgs e) |
{ |
GridEditableItem editedItem = e.Item as GridEditableItem; |
UserControl ucAddContact = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID); |
//Create new row in the DataSource |
DataRow newRow = this.Contacts.NewRow(); |
//Insert new values |
Hashtable newValues = new Hashtable(); |
newValues["CustomerID"] = (ucAddContact.FindControl("lblCompany") as Label).Text; |
newValues["Title"] = (ucAddContact.FindControl("ddlTitle") as DropDownList).SelectedItem.Value; |
newValues["CustomerContactID_FirstName"] = (ucAddContact.FindControl("txtFirstName") as TextBox).Text; |
newValues["CustomerContactID_LastName"] = (ucAddContact.FindControl("txtLastName") as TextBox).Text; |
newValues["CustomerContactID_PositionTitle"] = (ucAddContact.FindControl("txtPosition") as TextBox).Text; |
newValues["CustomerContactID_PhoneNumber"] = (ucAddContact.FindControl("txtPhone") as TextBox).Text; |
newValues["CustomerContactID_MobileNumber"] = (ucAddContact.FindControl("txtMobile") as TextBox).Text; |
newValues["CustomerContactID_FaxNumber"] = (ucAddContact.FindControl("txtFax") as TextBox).Text; |
newValues["CustomerContactID_Address"] = (ucAddContact.FindControl("txtAddress") as TextBox).Text; |
newValues["CustomerContactID_Notes"] = (ucAddContact.FindControl("txtNotes") as TextBox).Text; |
newValues["CustomerContactID_Email"] = (ucAddContact.FindControl("txtEmail") as TextBox).Text; |
newValues["LookupID_Country"] = (ucAddContact.FindControl("ddlCountry") as DropDownList).SelectedItem.Value; |
newValues["LookupID_State"] = (ucAddContact.FindControl("ddlState") as DropDownList).SelectedItem.Value; |
newValues["LookupID_Location"] = (ucAddContact.FindControl("ddlLocation") as DropDownList).SelectedItem.Value; |
newValues["LookupID_Postcode"] = (ucAddContact.FindControl("ddlPostcode") as DropDownList).SelectedItem.Value; |
newValues["CustomerContactID_Active"] = (ucAddContact.FindControl("chkActive") as CheckBox).Checked; |
newValues["TrainingActive"] = (ucAddContact.FindControl("chkTrainingAccount") as CheckBox).Checked; |
newValues["OrganiserActive"] = (ucAddContact.FindControl("chkOrganiserActive") as CheckBox).Checked; |
newValues["CustomerContactID_EditedBy"] = (ucAddContact.FindControl("txtEditedBy") as TextBox).Text; |
newValues["CustomerContactID_EditedDate"] = (ucAddContact.FindControl("txtEditedDate") as TextBox).Text; |
//make sure that unique primary key value is generated for the inserted row |
newValues["CustomerContactID"] = (int)this.Contacts.Rows[this.Contacts.Rows.Count - 1]["CustomerContactID"] + 1; |
try |
{ |
foreach (DictionaryEntry entry in newValues) |
{ |
newRow[(string)entry.Key] = entry.Value; |
} |
this.Contacts.Rows.Add(newRow); |
this.Contacts.AcceptChanges(); |
} |
catch (Exception ex) |
{ |
Label lblError = new Label(); |
lblError.Text = "Unable to insert Contact. Reason: " + ex.Message; |
lblError.ForeColor = System.Drawing.Color.Red; |
rgCustomerContact.Controls.Add(lblError); |
e.Canceled = true; |
} |
} |
protected void rgCustomerContact_DeleteCommand(object source, GridCommandEventArgs e) |
{ |
string ID = (e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CustomerContactID"].ToString(); |
DataTable contactTable = this.Contacts; |
if (contactTable.Rows.Find(ID) != null) |
{ |
contactTable.Rows.Find(ID).Delete(); |
contactTable.AcceptChanges(); |
} |
} |
} |
} |
xd_EditContact.ascx
<%@ Control AutoEventWireup="true" CodeFile="xd_EditContact.ascx.cs" Inherits="xDynamics.Controls.Edit.xd_EditContact" |
Language="C#" %> |
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %> |
<div> |
<asp:ValidationSummary ID="vsContact" runat="server" ValidationGroup="Contact" ShowMessageBox="True" |
ShowSummary="False" DisplayMode="List" /> |
<ajaxToolkit:Accordion ID="Accordion1" runat="server" SelectedIndex="0" HeaderCssClass="accordionHeaderDeselected" |
HeaderSelectedCssClass="accordionHeaderSelected" ContentCssClass="accordionContent" |
FadeTransitions="false" FramesPerSecond="40" TransitionDuration="250" RequireOpenedPane="false" |
SuppressHeaderPostbacks="true"> |
<Panes> |
<ajaxToolkit:AccordionPane ID="AccordionPane1" runat="server"> |
<Header> |
<a href="" style="text-decoration: none">Details</a></Header> |
<Content> |
<table id="tblPan1" width="500px" class="style1"> |
<tr runat="server" width="150px" id="tr1" visible="true"> |
<td> |
CustomerID |
</td> |
<td> |
<asp:UpdatePanel ID="UpdatePanel6" runat="server"> |
<ContentTemplate> |
<asp:DropDownList ID="ddlCompany" runat="server" AutoPostBack="True" Width="100%" |
CausesValidation="True"> |
</asp:DropDownList> |
</ContentTemplate> |
</asp:UpdatePanel> |
</td> |
</tr> |
<tr runat="server" width="150px" id="tr4"> |
<td> |
CustomerContactID |
</td> |
<td> |
<asp:Label ID="lblCustomerContactID" runat="server" Text='<%# DataBinder.Eval( Container, "DataItem.CustomerContactID") %>'> |
</asp:Label> |
</td> |
</tr> |
<tr runat="server" width="150px" id="tr2"> |
<td> |
Title |
</td> |
<td> |
<asp:UpdatePanel ID="UpdatePanel5" runat="server"> |
<ContentTemplate> |
<asp:DropDownList ID="ddlTitle" runat="server" AutoPostBack="True" Width="100%" |
CausesValidation="True"> |
</asp:DropDownList> |
<asp:RequiredFieldValidator ID="rfvTitle" runat="server" ErrorMessage="*" ControlToValidate="ddlTitle" |
ValidationGroup="Contact"></asp:RequiredFieldValidator> |
</ContentTemplate> |
</asp:UpdatePanel> |
</td> |
</tr> |
<tr runat="server" width="150px" id="tr5"> |
<td> |
Position |
</td> |
<td> |
<asp:TextBox ID="txtPosition" runat="server" Width="300" Text='<%# DataBinder.Eval( Container, "DataItem.CustomerContactID_PositionTitle") %>'> |
</asp:TextBox> |
<asp:RequiredFieldValidator ID="rfvPosition" runat="server" ErrorMessage="*" ValidationGroup="Contact" |
ControlToValidate="txtPosition"></asp:RequiredFieldValidator> |
</td> |
</tr> |
<tr runat="server" width="150px" id="tr3"> |
<td> |
First Name |
</td> |
<td> |
<asp:TextBox ID="txtFirstName" runat="server" Width="300" Text='<%# DataBinder.Eval( Container, "DataItem.CustomerContactID_FirstName") %>'> |
</asp:TextBox> |
<asp:RequiredFieldValidator ID="rfvFirstName" runat="server" ErrorMessage="*" ValidationGroup="Contact" |
ControlToValidate="txtFirstName"></asp:RequiredFieldValidator> |
</td> |
</tr> |
<tr runat="server" width="150px" id="tr7"> |
<td> |
Surname Name |
</td> |
<td> |
<asp:TextBox ID="txtLastName" runat="server" Width="300" Text='<%# DataBinder.Eval( Container, "DataItem.CustomerContactID_LastName") %>'> |
</asp:TextBox> |
<asp:RequiredFieldValidator ID="rfvLastName" runat="server" ErrorMessage="*" ValidationGroup="Contact" |
ControlToValidate="txtLastName"></asp:RequiredFieldValidator> |
</td> |
</tr> |
</table> |
</Content> |
</ajaxToolkit:AccordionPane> |
<ajaxToolkit:AccordionPane ID="AccordionPane3" runat="server"> |
<Header> |
<a href="" style="text-decoration: none">Contact Details</a></Header> |
<Content> |
<table id="Table2" width="500px" class="style1"> |
<tr runat="server" width="150px" id="tr6"> |
<td width="150px"> |
Phone |
</td> |
<td> |
<asp:TextBox ID="txtPhone" runat="server" Width="300" Text='<%# DataBinder.Eval( Container, "DataItem.CustomerContactID_PhoneNumber") %>'> |
</asp:TextBox> |
<asp:RequiredFieldValidator ID="rfvPhone" runat="server" ErrorMessage="*" ValidationGroup="Contact" |
ControlToValidate="txtPhone"></asp:RequiredFieldValidator> |
</td> |
</tr> |
<tr runat="server" width="150px" id="tr9"> |
<td> |
Mobile |
</td> |
<td> |
<asp:TextBox ID="txtMobile" runat="server" Width="300" Text='<%# DataBinder.Eval( Container, "DataItem.CustomerContactID_MobileNumber") %>'> |
</asp:TextBox> |
<asp:RequiredFieldValidator ID="rfvMobile" runat="server" ErrorMessage="*" ValidationGroup="Contact" |
ControlToValidate="txtMobile"></asp:RequiredFieldValidator> |
</td> |
</tr> |
<tr runat="server" width="150px" id="tr8"> |
<td> |
Fax |
</td> |
<td> |
<asp:TextBox ID="txtFax" runat="server" Width="300" Text='<%# DataBinder.Eval( Container, "DataItem.CustomerContactID_FaxNumber") %>'> |
</asp:TextBox> |
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ErrorMessage="*" |
ValidationGroup="Contact" ControlToValidate="txtFax"></asp:RequiredFieldValidator> |
</td> |
</tr> |
<tr runat="server" width="150px" id="tr11"> |
<td> |
</td> |
<td> |
<asp:TextBox ID="txtEmail" runat="server" Width="300" Text='<%# DataBinder.Eval( Container, "DataItem.CustomerContactID_Email") %>'> |
</asp:TextBox> |
<asp:RequiredFieldValidator ID="rfvEmail" runat="server" ErrorMessage="*" ValidationGroup="Contact" |
ControlToValidate="txtEmail"></asp:RequiredFieldValidator> |
</td> |
</tr> |
</table> |
</Content> |
</ajaxToolkit:AccordionPane> |
<ajaxToolkit:AccordionPane ID="AccordionPane2" runat="server"> |
<Header> |
<a href="" style="text-decoration: none">Mailing</a></Header> |
<Content> |
<table id="Table1" width="100%" class="style1"> |
<tr runat="server" width="150px" id="tr10"> |
<td> |
Address |
</td> |
<td> |
<asp:TextBox ID="txtAddress" runat="server" Width="300" Text='<%# DataBinder.Eval( Container, "DataItem.CustomerContactID_Address") %>'> |
</asp:TextBox> |
<asp:RequiredFieldValidator ID="rfvAddress" runat="server" ErrorMessage="*" ValidationGroup="Contact" |
ControlToValidate="txtAddress"></asp:RequiredFieldValidator> |
</td> |
</tr> |
<tr runat="server" width="150px" id="tr12"> |
<td> |
Country |
</td> |
<td> |
<asp:UpdatePanel ID="UpdatePanel1" runat="server"> |
<ContentTemplate> |
<asp:DropDownList ID="ddlCountry" runat="server" Width="100%" AutoPostBack="True" |
OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged"> |
</asp:DropDownList> |
<asp:SqlDataSource ID="sdsCountry" runat="server"></asp:SqlDataSource> |
<asp:RequiredFieldValidator ID="rfvCountry" runat="server" ErrorMessage="*" ControlToValidate="ddlCountry" |
ValidationGroup="Customer"></asp:RequiredFieldValidator> |
</ContentTemplate> |
</asp:UpdatePanel> |
</td> |
</tr> |
<tr runat="server" id="trState"> |
<td> |
State |
</td> |
<td> |
<asp:UpdatePanel ID="UpdatePanel2" runat="server"> |
<ContentTemplate> |
<asp:DropDownList ID="ddlState" runat="server" Width="100%" AutoPostBack="True" |
OnSelectedIndexChanged="ddlState_SelectedIndexChanged"> |
</asp:DropDownList> |
<asp:SqlDataSource ID="sdsState" runat="server"></asp:SqlDataSource> |
<asp:RequiredFieldValidator ID="rfvState" runat="server" ErrorMessage="*" ControlToValidate="ddlState" |
ValidationGroup="Customer"></asp:RequiredFieldValidator> |
</ContentTemplate> |
</asp:UpdatePanel> |
</td> |
</tr> |
<tr runat="server" width="150px" id="tr13"> |
<td> |
Location |
</td> |
<td> |
<asp:UpdatePanel ID="UpdatePanel3" runat="server"> |
<ContentTemplate> |
<asp:DropDownList ID="ddlLocation" runat="server" Width="100%" AutoPostBack="True" |
OnSelectedIndexChanged="ddlLocation_SelectedIndexChanged"> |
</asp:DropDownList> |
<asp:SqlDataSource ID="sdsLocation" runat="server"></asp:SqlDataSource> |
<asp:RequiredFieldValidator ID="rfvLocation" runat="server" ErrorMessage="*" ControlToValidate="ddlLocation" |
ValidationGroup="Customer"></asp:RequiredFieldValidator> |
</ContentTemplate> |
</asp:UpdatePanel> |
</td> |
</tr> |
<tr runat="server" id="trPostcode"> |
<td> |
Postcode |
</td> |
<td> |
<asp:UpdatePanel ID="UpdatePanel4" runat="server"> |
<ContentTemplate> |
<asp:DropDownList ID="ddlPostcode" runat="server" Width="100%"> |
</asp:DropDownList> |
<asp:SqlDataSource ID="sdsPostcode" runat="server"></asp:SqlDataSource> |
<asp:RequiredFieldValidator ID="rfvPostcode" runat="server" ErrorMessage="*" ControlToValidate="ddlPostcode" |
ValidationGroup="Customer"></asp:RequiredFieldValidator> |
</ContentTemplate> |
</asp:UpdatePanel> |
</td> |
</tr> |
</table> |
</Content> |
</ajaxToolkit:AccordionPane> |
<ajaxToolkit:AccordionPane ID="AccordionPane4" runat="server"> |
<Header> |
<a href="" style="text-decoration: none">Contact Details</a></Header> |
<Content> |
<table id="Table3" width="500px" class="style1"> |
<tr> |
<td> |
Active? |
</td> |
<td> |
<asp:CheckBox ID="chkActive" runat="server" Checked='<%# DataBinder.Eval( Container, "DataItem.CustomerContactID_Active")%>'> |
</asp:CheckBox> |
</td> |
</tr> |
<tr runat="server" width="150px" id="tr14"> |
<td> |
Training Account |
</td> |
<td> |
<asp:CheckBox ID="chkTrainingAccount" runat="server" Checked='<%#DataBinder.Eval( Container, "DataItem.TrainingActive")%>' /> |
</td> |
</tr> |
<tr runat="server" id="trOrganiserActive"> |
<td> |
Organiser Active |
</td> |
<td> |
<asp:CheckBox ID="chkOrganiserActive" runat="server" Checked='<%#DataBinder.Eval( Container, "DataItem.OrganiserActive")%>' /> |
</td> |
</tr> |
<tr runat="server" id="trNotes"> |
<td> |
Notes |
</td> |
<td> |
<asp:TextBox ID="txtNotes" runat="server" TextMode="MultiLine" Width="100%" Height="150px" |
Text='<%# DataBinder.Eval( Container, "DataItem.CustomerContactID_Notes") %>'></asp:TextBox> |
<asp:RequiredFieldValidator ID="rfvNotes" runat="server" ErrorMessage="*" ControlToValidate="txtNotes" |
ValidationGroup="Customer"></asp:RequiredFieldValidator> |
</td> |
</tr> |
<tr runat="server" width="150px" id="tr16"> |
<td> |
Edited By |
</td> |
<td> |
<asp:TextBox ID="txtEditedBy" runat="server" Width="300" Enabled="False" Text='<%# DataBinder.Eval( Container, "DataItem.CustomerContactID_EditedBy") %>'> |
</asp:TextBox> |
</td> |
</tr> |
<tr runat="server" width="150px" id="tr15"> |
<td> |
Edited Date |
</td> |
<td> |
<asp:TextBox ID="txtEditedDate" runat="server" Width="300" Enabled="False" Text='<%# DataBinder.Eval( Container, "DataItem.CustomerContactID_EditedDate") %>'> |
</asp:TextBox> |
</td> |
</tr> |
</table> |
</Content> |
</ajaxToolkit:AccordionPane> |
</Panes> |
</ajaxToolkit:Accordion> |
<table id="Table4" width="500px" class="style1"> |
<tr> |
<td align="right" colspan="2"> |
<asp:Button ID="btnUpdate" Text="Update" runat="server" CommandName="Update" Visible='<%# !(DataItem is Telerik.Web.UI.GridInsertionObject) %>'> |
</asp:Button> |
<asp:Button ID="btnInsert" Text="Insert" runat="server" CommandName="PerformInsert" |
Visible='<%# DataItem is Telerik.Web.UI.GridInsertionObject %>'></asp:Button> |
|
<asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" |
CommandName="Cancel"></asp:Button> |
</td> |
</tr> |
</table> |
</div> |
xd_EditContact.ascx.cs
using System; |
using System.Data; |
using System.Collections; |
using System.Data.SqlClient; |
using System.Web.UI; |
namespace xDynamics.Controls.Edit |
{ |
public partial class xd_EditContact : UserControl |
{ |
#region Globals |
DataAccess data = new DataAccess(); |
private object _dataItem = null; |
#endregion |
#region Page Events |
protected void Page_Init(object sender, EventArgs e) |
{ |
Accordion1.FindControl("nothing"); |
} |
public void Page_Load(object sender, EventArgs e) |
{ |
} |
#endregion |
#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); |
} |
/// <summary> |
/// Required method for Designer support - do not modify |
/// the contents of this method with the code editor. |
/// </summary> |
private void InitializeComponent() |
{ |
this.DataBinding += new System.EventHandler(this.BindCompanyList); |
this.DataBinding += new System.EventHandler(this.BindTitleList); |
this.DataBinding += new System.EventHandler(this.BindCountryList); |
} |
#endregion |
#region Public Functions |
public void FunctionPageLoad() |
{ |
string selectedCountries = ddlCountry.SelectedValue; |
string selectedStates = ddlState.SelectedValue; |
string selectedLocations = ddlLocation.SelectedValue; |
string selectedPostcode = ddlPostcode.SelectedValue; |
ddlCountry.SelectedValue = selectedCountries; |
ddlState.SelectedValue = selectedStates; |
ddlLocation.SelectedValue = selectedLocations; |
ddlPostcode.SelectedValue = selectedPostcode; |
} |
public object DataItem |
{ |
get |
{ |
return this._dataItem; |
} |
set |
{ |
this._dataItem = value; |
} |
} |
#endregion |
#region Control Bindings |
protected void BindCompanyList(object sender, EventArgs e) |
{ |
string sql = "SELECT DISTINCT CustomerID, CompanyName FROM Xtrain.data_customer WHERE (AccountActive = 1) ORDER BY CompanyName ASC"; |
DataTable Company = data.GetDataTable(sql); |
ArrayList CompanyList = new ArrayList(Company.Rows); |
if (Company.Rows.Count > 0) |
{ |
ddlCompany.DataSource = Company; |
ddlCompany.DataTextField = "CompanyName"; |
ddlCompany.DataValueField = "CustomerID"; |
ddlCompany.DataBind(); |
ddlCompany.Items.Insert(0, "- Select a Company -"); |
object CompanyValue = DataBinder.Eval(DataItem, "CustomerID"); |
if (CompanyValue == DBNull.Value) |
{ |
CompanyValue = "0"; |
} |
ddlCompany.SelectedIndex = CompanyList.IndexOf(CompanyValue); |
ddlCompany.DataSource = null; |
} |
} |
protected void BindTitleList(object sender, EventArgs e) |
{ |
string sql = "SELECT DISTINCT LookupID_LookupTable, LookupID, LookupParentID FROM Xtrain.data_lookup_tables WHERE (LookupParentID = 11)"; |
DataTable Title = data.GetDataTable(sql); |
ArrayList TitleList = new ArrayList(Title.Rows); |
if (Title.Rows.Count > 0) |
{ |
ddlTitle.DataSource = Title; |
ddlTitle.DataTextField = "LookupID_LookupTable"; |
ddlTitle.DataValueField = "LookupID"; |
ddlTitle.DataBind(); |
ddlTitle.Items.Insert(0, "- Select a Title -"); |
object TitleValue = DataBinder.Eval(DataItem, "CustomerContactID_ContactTitle"); |
if (TitleValue == DBNull.Value) |
{ |
TitleValue = "0"; |
} |
ddlTitle.SelectedIndex = TitleList.IndexOf(TitleValue); |
ddlTitle.DataSource = null; |
} |
} |
protected void BindCountryList(object sender, EventArgs e) |
{ |
string sql = "SELECT DISTINCT LookupID_LookupTable, LookupID, LookupParentID FROM Xtrain.data_lookup_tables WHERE (LookupParentID = 41)"; |
DataTable Country = data.GetDataTable(sql); |
ArrayList CountryList = new ArrayList(Country.Rows); |
if (Country.Rows.Count > 0) |
{ |
ddlCountry.DataSource = Country; |
ddlCountry.DataTextField = "LookupID_LookupTable"; |
ddlCountry.DataValueField = "LookupID"; |
ddlCountry.DataBind(); |
ddlCountry.Items.Insert(0, "- Select a Country -"); |
object CountryValue = DataBinder.Eval(DataItem, "LookupID_Country"); |
if (CountryValue == DBNull.Value) |
{ |
CountryValue = "0"; |
} |
ddlCountry.SelectedIndex = CountryList.IndexOf(CountryValue); |
ddlCountry.DataSource = null; |
} |
} |
protected void BindStateList(object sender, EventArgs e) |
{ |
string sql = string.Format("SELECT DISTINCT LookupID_LookupTable, LookupID, LookupParentID FROM Xtrain.data_lookup_tables WHERE ( LookupID_ParentLookupID ={0})", ddlCountry.SelectedValue); |
DataTable State = data.GetDataTable(sql); |
ArrayList StateList = new ArrayList(State.Rows); |
if (State.Rows.Count > 0) |
{ |
ddlState.DataSource = State; |
ddlState.DataTextField = "LookupID_LookupTable"; |
ddlState.DataValueField = "LookupID"; |
ddlState.DataBind(); |
ddlState.Items.Insert(0, "- Select a State -"); |
object StateValue = DataBinder.Eval(DataItem, "LookupID_State"); |
if (StateValue == DBNull.Value) |
{ |
StateValue = "0"; |
} |
ddlState.SelectedIndex = StateList.IndexOf(StateValue); |
ddlState.DataSource = null; |
} |
} |
protected void BindLocationList(object sender, EventArgs e) |
{ |
string sql = string.Format("SELECT DISTINCT LookupID_LookupTable, LookupID, LookupParentID FROM Xtrain.data_lookup_tables WHERE (LookupParentID = ={0})", ddlState.SelectedValue); |
DataTable Location = data.GetDataTable(sql); |
ArrayList LocationList = new ArrayList(Location.Rows); |
if (Location.Rows.Count > 0) |
{ |
ddlLocation.DataSource = Location; |
ddlLocation.DataTextField = "LookupID_LookupTable"; |
ddlLocation.DataValueField = "LookupID"; |
ddlLocation.DataBind(); |
ddlLocation.Items.Insert(0, "- Select a Location -"); |
object LocationValue = DataBinder.Eval(DataItem, "LookupID_Location"); |
if (LocationValue == DBNull.Value) |
{ |
LocationValue = "0"; |
} |
ddlLocation.SelectedIndex = LocationList.IndexOf(LocationValue); |
ddlLocation.DataSource = null; |
} |
} |
protected void BindPostcodeList(object sender, EventArgs e) |
{ |
string sql = string.Format("SELECT DISTINCT LookupID_LookupTable, LookupID, LookupParentID FROM Xtrain.data_lookup_tables WHERE (LookupParentID = ={0})", ddlLocation.SelectedValue); |
DataTable Postcode = data.GetDataTable(sql); |
ArrayList PostcodeList = new ArrayList(Postcode.Rows); |
if (Postcode.Rows.Count > 0) |
{ |
ddlPostcode.DataSource = Postcode; |
ddlPostcode.DataTextField = "LookupID_LookupTable"; |
ddlPostcode.DataValueField = "LookupID"; |
ddlPostcode.DataBind(); |
ddlPostcode.Items.Insert(0, "- Select a Postcode -"); |
object PostcodeValue = DataBinder.Eval(DataItem, "LookupID_Postcode"); |
if (PostcodeValue == DBNull.Value) |
{ |
PostcodeValue = "0"; |
} |
ddlPostcode.SelectedIndex = PostcodeList.IndexOf(PostcodeValue); |
ddlPostcode.DataSource = null; |
} |
} |
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e) |
{ |
ddlState.DataBind(); |
} |
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e) |
{ |
ddlLocation.DataBind(); |
} |
protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e) |
{ |
ddlPostcode.DataBind(); |
} |
#endregion |
} |
} |