or
I have a series of stored procedures created to handle the Select, Insert, Update and Delete needs of the system. Many of the columns are dynamically driven. The delete functionality works perfectly but Update is throwing me some odd issues and I'm not certain where I've made my mistakes. Any direction would be helpful.
The Problem:
When you click Edit the form comes up correctly (See awardgrid-edit.jpg) with even the ItemID on the top indicating I have the appropriate record correlating to the database ID. However when you click the Update button the AJAX does a post back and the Edit field is empty and the button for the [Update] now has no text (See awardgrid-afterupdate.jpg).
The ASPX Page Snippet
<
telerik:radscriptmanager
ID
=
"RadScriptManager1"
Runat
=
"server"
>
</
telerik:radscriptmanager
>
<
uc1:Awards
ID
=
"Awards1"
runat
=
"server"
/>
<
telerik:RadAjaxManager
ID
=
"RadAjaxManager1"
runat
=
"server"
>
<
AjaxSettings
>
<
telerik:AjaxSetting
AjaxControlID
=
"rgProfileItem"
>
<
UpdatedControls
>
<
telerik:AjaxUpdatedControl
ControlID
=
"rgProfileItem"
LoadingPanelID
=
"RadAjaxLoadingPanel1"
/>
</
UpdatedControls
>
</
telerik:AjaxSetting
>
</
AjaxSettings
>
</
telerik:RadAjaxManager
>
<
telerik:RadAjaxLoadingPanel
ID
=
"RadAjaxLoadingPanel1"
Skin
=
"Windows7"
EnableSkinTransparency
=
"true"
runat
=
"server"
/>
<
telerik:RadGrid
ID
=
"rgProfileItem"
runat
=
"server"
AllowFilteringByColumn
=
"True"
AllowPaging
=
"True"
AllowSorting
=
"True"
GridLines
=
"None"
Skin
=
"Windows7"
OnItemDataBound
=
"rgProfileItem_ItemDataBound"
OnInsertCommand
=
"rgProfileItem_InsertCommand"
OnDeleteCommand
=
"rgProfileItem_DeleteCommand"
OnUpdateCommand
=
"rgProfileItem_UpdateCommand"
onneeddatasource
=
"rgProfileItem_NeedDataSource"
AllowAutomaticUpdates
=
"false"
AllowAutomaticInserts
=
"false"
>
<
MasterTableView
AutoGenerateColumns
=
"False"
AllowAutomaticInserts
=
"false"
AllowAutomaticUpdates
=
"false"
DataKeyNames
=
"ItemID"
EditMode
=
"EditForms"
>
<
Columns
>
<
telerik:GridEditCommandColumn
ButtonType
=
"ImageButton"
UniqueName
=
"Edit"
>
<
HeaderStyle
Width
=
"20px"
/>
</
telerik:GridEditCommandColumn
>
<
telerik:GridButtonColumn
ButtonType
=
"ImageButton"
CommandName
=
"Delete"
ImageUrl
=
"~/images/delete-16x16.png"
Text
=
"Delete"
UniqueName
=
"Delete"
>
<
HeaderStyle
Width
=
"20px"
/>
</
telerik:GridButtonColumn
>
<
telerik:GridBoundColumn
DataField
=
"Title"
FilterControlAltText
=
"Filter Title column"
HeaderText
=
"Title"
SortExpression
=
"Title"
UniqueName
=
"Title"
>
<
HeaderStyle
Width
=
"30%"
/>
</
telerik:GridBoundColumn
>
<
telerik:GridDateTimeColumn
DataField
=
"DateReceived"
FilterControlAltText
=
"Filter DateReceived column"
HeaderText
=
"Date Received"
SortExpression
=
"DateReceived"
UniqueName
=
"DateReceived"
DataType
=
"System.DateTime"
>
<
HeaderStyle
Width
=
"150px"
/>
</
telerik:GridDateTimeColumn
>
<
telerik:GridTemplateColumn
DataField
=
"Notes"
FilterControlAltText
=
"Filter Notes column"
Groupable
=
"False"
HeaderText
=
"Notes"
UniqueName
=
"Notes"
>
<
ItemTemplate
>
<
asp:Label
ID
=
"shortNote"
runat
=
"server"
Text='<%# Eval("Notes") %>' />
<
asp:Label
ID
=
"longNote"
runat
=
"server"
Visible
=
"false"
Text='<%# Eval("Notes") %>' />
<
telerik:RadToolTip
ID
=
"RadToolTip1"
runat
=
"server"
Skin
=
"Windows7"
RelativeTo
=
"Element"
Width
=
"400px"
CssClass
=
"infotooltip"
Position
=
"TopCenter"
TargetControlID
=
"shortNote"
>
<%# Eval("Notes") %>
</
telerik:RadToolTip
>
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
</
Columns
>
<
EditFormSettings
InsertCaption
=
"Add new item"
CaptionFormatString
=
"Edit Item: {0}"
CaptionDataField
=
"ItemID"
EditFormType
=
"Template"
PopUpSettings-Modal
=
"true"
PopUpSettings-Width
=
"600px"
>
<
EditColumn
UniqueName
=
"EditCommandColumn1"
FilterControlAltText
=
"Filter EditCommandColumn1 column"
></
EditColumn
>
<
FormTemplate
>
<
asp:Table
ID
=
"pubForm"
CssClass
=
"pubForm"
CellSpacing
=
"0"
CellPadding
=
"2"
runat
=
"server"
>
<
asp:TableFooterRow
>
<
asp:TableCell
ColumnSpan
=
"2"
>
<
asp:Button
ID
=
"Button1"
Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'>
</
asp:Button
>
<
asp:Button
ID
=
"Button2"
Text
=
"Cancel"
runat
=
"server"
CausesValidation
=
"False"
CommandName
=
"Cancel"
>
</
asp:Button
>
</
asp:TableCell
>
</
asp:TableFooterRow
>
</
asp:Table
>
</
FormTemplate
>
</
EditFormSettings
>
</
MasterTableView
>
<
FilterMenu
EnableImageSprites
=
"False"
></
FilterMenu
>
<
HeaderContextMenu
CssClass
=
"GridContextMenu GridContextMenu_Default"
></
HeaderContextMenu
>
</
telerik:RadGrid
>
int maxFieldLength = 100;
int itemTypeID = 2; //ID item for Awards/Honors
SqlConnection cn = new SqlConnection(CONNSTRINGINFO);
protected void Page_Load(object sender, EventArgs e)
{
}
protected void rgProfileItem_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand("sp_getProfileItem", cn);
adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
adapter.SelectCommand.Parameters.AddWithValue("@ProfileItemTypeId", itemTypeID);
if (!string.IsNullOrEmpty(Request.QueryString["uid"]))
{
adapter.SelectCommand.Parameters.AddWithValue("@PersonID", Request.QueryString["uid"]);
}
DataTable myDataTable = new DataTable();
cn.Open();
try
{
adapter.Fill(myDataTable);
}
finally
{
cn.Close();
}
rgProfileItem.DataSource = myDataTable;
}
protected void rgProfileItem_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
{
Label target = (Label)e.Item.FindControl("shortNote");
if (!Object.Equals(target, null))
{
if (target.Text.Length > maxFieldLength)
{
target.Text = target.Text.Substring(0, maxFieldLength) + " ...";
}
}
}
if (e.Item is GridEditFormItem && (e.Item as GridEditableItem).IsInEditMode)
{
GridEditableItem editedItem = (GridEditableItem)e.Item;
string ItemID = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["ItemID"].ToString();
Table editFormTable = (Table)editedItem.FindControl("pubForm");
LoadProfileFields(int.Parse(ItemID), editFormTable);
}
}
protected void rgProfileItem_DeleteCommand(object sender, GridCommandEventArgs e)
{
//Delete Function
cn.Open();
GridDataItem item = (GridDataItem)e.Item;
try
{
SqlCommand comm = new SqlCommand("sp_deleteProfileItem", cn);
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.AddWithValue("@ItemID", item.GetDataKeyValue("ItemID"));
comm.ExecuteNonQuery();
comm.Dispose();
}
catch (Exception ex)
{
lblErrMsg.Text = "<
br
/>" + ex.Message;
}
finally
{
//lblErrMsg.Text = "Deleted " + item.GetDataKeyValue("ItemID");;
cn.Close();
}
}
protected void rgProfileItem_UpdateCommand(object sender, GridCommandEventArgs e)
{
DataTable dtProfileInfo = new DataTable();
//Get the GridEditableItem of the RadGrid
GridEditableItem editedItem = e.Item as GridEditableItem;
string ItemID = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["ItemID"].ToString();
cn.Open();
try
{
string profInfo = ""; //A placeholder for the dynamic fields Profile Information uses
string strProcName = "sp_updateProfileItem";
//Get the DataTable of all Publication Fields
dtProfileInfo = getProfileInfoFields();
foreach (DataRow dr in dtProfileInfo.Rows)
{
//It is important to note that the stored procedure is expecting all rows to be seperated by a | and fields seperated by a =.
//These symbols were used because they are not 'common place' and should not throw erroneous data.
profInfo += dr["FieldID"] + "=" + getDynamicField(stripText(dr["FieldName"].ToString())) + "|";
}
if (profInfo.Length > 0)
{
profInfo = profInfo.Substring(0, profInfo.Length - 1); //Remove the last | symbol
}
SqlCommand comm = new SqlCommand(strProcName, cn);
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.AddWithValue("@ItemType", itemTypeID); //Item Type ID
comm.Parameters.AddWithValue("@ItemInfo", profInfo); //String of dynamic field values
comm.Parameters.AddWithValue("@ProfileItem", ItemID); //ItemID
comm.ExecuteNonQuery();
rgProfileItem.Rebind();
editedItem.Edit = false;
}
catch (Exception ex)
{
rgProfileItem.Controls.Add(new LiteralControl("Unable to update Item. Reason: " + ex.Message));
e.Canceled = true;
}
finally
{
cn.Close();
}
}
protected void rgProfileItem_InsertCommand(object source, GridCommandEventArgs e)
{
}
private void LoadProfileFields(int ItemID,Table formTable)
{
//Based on the Profile Item Type, determine and add what fields are neccessary.
SqlCommand comm = new SqlCommand("sp_getProfileItemFieldsByType", cn);
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.AddWithValue("@ItemTypeID", itemTypeID);
string tmpFieldValue = "";
int rowInsertAt = formTable.Rows.Count - 1;
if (ItemID != null && ItemID > 0)
{
comm.Parameters.Add("@ItemID", ItemID);
}
SqlDataAdapter fieldTypeAdapter = new SqlDataAdapter(comm);
DataTable fieldTypeDataTable = new DataTable();
fieldTypeAdapter.Fill(fieldTypeDataTable);
foreach (DataRow fieldTypeDataRow in fieldTypeDataTable.Rows)
{
tmpFieldValue = "";
try
{
bool isRequired = false;
if (fieldTypeDataRow["Required"].ToString().Trim().ToLower() == "true")
{
isRequired = true;
}
tmpFieldValue = fieldTypeDataRow["FieldValue"].ToString();
rowInsertAt = formTable.Rows.Count - 1;
switch (fieldTypeDataRow["FieldType"].ToString())
{
case "Date":
formTable.Rows.AddAt(rowInsertAt,AddRow_Datebox(fieldTypeDataRow["FieldName"].ToString(), tmpFieldValue, isRequired));
break;
case "Textarea":
formTable.Rows.AddAt(rowInsertAt, AddRow_Textarea(fieldTypeDataRow["FieldName"].ToString(), tmpFieldValue, isRequired));
break;
default:
//By Default everything is a single-line text box.
formTable.Rows.AddAt(rowInsertAt,AddRow_Textbox(fieldTypeDataRow["FieldName"].ToString(), tmpFieldValue, isRequired));
break;
}
}
catch
{
lblErrMsg.Text += "Field Broke!!<
br
>";
}
}
}
private DataTable getProfileInfoFields()
{
SqlCommand comm = new SqlCommand("sp_getProfileItemFieldsByType", cn);
comm.CommandType = CommandType.StoredProcedure;
if (itemTypeID > 0)
{
comm.Parameters.Add("@ItemTypeID", itemTypeID);
}
else
{
return null;
}
SqlDataAdapter adapter = new SqlDataAdapter(comm);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
return dataTable;
}
private TableRow AddRow_Textbox(string rowField, string defaultValue, bool isRequired)
{
TableRow tr = new TableRow();
TableCell tcLabel = new TableCell();
TableCell tcField = new TableCell();
tcLabel.CssClass = "label";
tcLabel.Text = rowField;
RadTextBox rtb = new RadTextBox();
rtb.Skin = "Windows7";
rtb.Width = 230;
rtb.ID = stripText(rowField);
rtb.Text = defaultValue.Trim();
tcField.Controls.Add(rtb);
if (isRequired)
{
RequiredFieldValidator rfv = new RequiredFieldValidator();
rfv.ControlToValidate = rtb.ID;
rfv.ErrorMessage = "You must fill out this field";
rfv.CssClass = "validator";
rfv.ValidationGroup = "PublicationFields";
tcField.Controls.Add(rfv);
tcLabel.Text += "<
span
class=\"required-field\">*</
span
>";
}
tr.Cells.Add(tcLabel);
tr.Cells.Add(tcField);
//int rowInsertAt = pubForm.Rows.Count - 1;
//pubForm.Rows.AddAt(rowInsertAt, tr);
return tr;
}
private TableRow AddRow_Datebox(string rowField, string defaultValue, bool isRequired)
{
TableRow tr = new TableRow();
TableCell tcLabel = new TableCell();
TableCell tcField = new TableCell();
DateTime dr;
tcLabel.CssClass = "label";
tcLabel.Text = rowField;
RadDateInput rdi = new RadDateInput();
rdi.DateFormat = "d";
rdi.Skin = "Windows7";
rdi.Width = 230;
rdi.ID = stripText(rowField);
if (DateTime.TryParse(defaultValue, out dr))
{
rdi.SelectedDate = dr;
}
tcField.Controls.Add(rdi);
if (isRequired)
{
RequiredFieldValidator rfv = new RequiredFieldValidator();
rfv.ControlToValidate = rdi.ID;
rfv.ErrorMessage = "You must fill out this field";
rfv.Display = ValidatorDisplay.Dynamic;
rfv.CssClass = "validator";
rfv.ValidationGroup = "PublicationFields";
tcField.Controls.Add(rfv);
tcLabel.Text += "<
span
class=\"required-field\">*</
span
>";
}
tr.Cells.Add(tcLabel);
tr.Cells.Add(tcField);
//int rowInsertAt = pubForm.Rows.Count - 1;
//pubForm.Rows.AddAt(rowInsertAt, tr);
return tr;
}
private TableRow AddRow_Textarea(string rowField, string defaultValue, bool isRequired)
{
TableRow tr = new TableRow();
TableCell tcLabel = new TableCell();
TableCell tcField = new TableCell();
tcLabel.CssClass = "label";
tcLabel.Text = rowField;
RadEditor rtb = new RadEditor();
rtb.Skin = "Windows7";
rtb.ToolbarMode = EditorToolbarMode.ShowOnFocus;
rtb.SkinID = "DefaultSetOfTools";
rtb.ToolsFile = "~/App_Data/ToolsFile.xml";
rtb.Width = 500;
rtb.Height = 200;
string[] imagePath = {"~/Editor/Img/UserDir/Marketing,~/Editor/Img/UserDir/PublicRelations"};
rtb.ImageManager.ViewPaths = imagePath;
rtb.ImageManager.UploadPaths = imagePath;
rtb.ID = stripText(rowField);
rtb.Content = defaultValue;
tcField.CssClass = "field_onTop";
tcField.Controls.Add(rtb);
if (isRequired)
{
RequiredFieldValidator rfv = new RequiredFieldValidator();
rfv.ControlToValidate = rtb.ID;
rfv.ErrorMessage = "You must fill out this field";
rfv.CssClass = "validator";
rfv.ValidationGroup = "ProfileFields";
tcField.Controls.Add(rfv);
tcLabel.Text += "<
span
class=\"required-field\">*</
span
>";
}
tr.Cells.Add(tcLabel);
tr.Cells.Add(tcField);
//int rowInsertAt = pubForm.Rows.Count - 1;
//pubForm.Rows.AddAt(rowInsertAt, tr);
return tr;
}
private string stripText(string toStrip)
{
string pattern = "[^\\w\\.@-]";
Regex rgx = new Regex(pattern);
return rgx.Replace(toStrip, "");
}
private Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}
foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}
return null;
}
private string getDynamicField(string controlID)
{
string fieldValue = "";
Control fieldControl = FindControlRecursive(Page, controlID);
if (fieldControl != null)
{
string tmpTypeBox = fieldControl.GetType().Name;
switch (tmpTypeBox)
{
case "RadTextBox":
RadTextBox tmpTextControl = (RadTextBox)fieldControl;
fieldValue = tmpTextControl.Text;
break;
case "RadDateInput":
RadDateInput tmpDateControl = (RadDateInput)fieldControl;
if (tmpDateControl.SelectedDate.HasValue)
{
fieldValue = tmpDateControl.DbSelectedDate.ToString().Split(' ')[0];
}
else
{
fieldValue = "";
}
break;
}
}
else
{
lblErrMsg.Text = "Could not find the control ID for " + controlID;
}
return fieldValue;
}
Dim
dataAdapter as new SetTableAdapter.someidentityTableAdapter
Dim
adapterTable as DataTable
adapterTable = dataAdapter.getData()
Radcombo1.DataValueField =
"Value1"
Radcombo1.DataTextField =
"Value2"
Radcombo1.DataSource = adapterTable
Radcombo1.DataBind()
<
telerik:RadGrid
ID
=
"rgAccountRangeSet"
runat
=
"server"
AutoGenerateColumns
=
"False"
GridLines
=
"None"
AllowAutomaticDeletes
=
"True"
AllowAutomaticInserts
=
"True"
AllowAutomaticUpdates
=
"True"
PageSize
=
"25"
AllowPaging
=
"True"
AllowSorting
=
"True"
>
<
PagerStyle
Mode
=
"NumericPages"
/>
<
MasterTableView
AutoGenerateColumns
=
"False"
CommandItemDisplay
=
"Top"
CommandItemSettings-ShowRefreshButton
=
"False"
HorizontalAlign
=
"NotSet"
DataKeyNames
=
"Id"
>
<
CommandItemSettings
AddNewRecordText
=
"Add New Account Range"
AddNewRecordImageUrl
=
"~/images/Keymaster/add.gif"
/>
<
NoRecordsTemplate
>
<
center
><
b
>No records available</
b
></
center
>
</
NoRecordsTemplate
>
<
Columns>
</
Columns
>
<
EditFormSettings
>
<
EditColumn
ButtonType
=
"PushButton"
/>
</
EditFormSettings
>
</
MasterTableView
>
</
telerik:RadGrid
>
With the following code:
Private
Sub
rgAccountRangeSet_InsertCommand(
ByVal
sender
As
Object
,
ByVal
e
As
Telerik.Web.UI.GridCommandEventArgs)
Handles
rgAccountRangeSet.InsertCommand
'code to handle insert items has been removed
rgAccountRangeSet.Rebind()
rgAccountRangeSet.MasterTableView.IsItemInserted =
False
End
Sub