I have issues with buttons in a template field inside the webdatagrid. While clicking on the button it is invoking the javascript method but the page is getting refreshed. How can i avoid the page refresh while click on the button.
Thanks,
Vijay Krishnan
Hello Vijay,
Thank you for posting in the Infragistics community !
I assume that you are using a server side asp button control. To avoid postback you can call return false; in the end of the javascript function that is invoked on button click. You can also use a html <input type="button"> control,which does not trigger a postback on button click.
If these do not help please provide the code for the TemplateDataField and I will suggest accordingly how to resolve your issue.
Hello Hristo,
I have tried whatever you had mentioned here, but still i coud'nt perform the crud functions well.
This is really become a mess. i was about to upgrade the DLL's to ver 14 and changed the controls too.
But really fed up of this webdatagrid, It'll be a great help for me if you can send me a sample webdatagrid with edit and delete button and all this has to be dynamic.
Ultrawebgrid was fine, why did u do this ?, i dont really understand that why you people take these kind of decisions.
Please look into the sample code given below.
protected override void OnPreRender(EventArgs e) { if (!Page.IsClientScriptBlockRegistered("templatedClicked")) { string strCode = @" <script type='text/javascript'> function templatedClicked() { var result = confirm('Are you sure you want to delete this row'); if (result) { //initiate row delete from the client and call the grid's commit var grid = $find('COETableManager_wdgList'); var editing = grid.get_behaviors().get_editingCore();
grid.get_rows().remove(grid.get_rows().get_row(2));
editing.commit();
} else {
//call do postback to load the new selected data in the second grid? __doPostBack(); } return false; } </script>"; Page.RegisterClientScriptBlock("templatedClicked", strCode); }
if (!Page.IsClientScriptBlockRegistered("wdgList_Editing_RowAdded")) { string strCode = @" <script type='text/javascript'> function wdgList_Editing_RowAdded(sender, eventArgs) { var behaviors = sender.get_behaviors(); var addRowBehavior = behaviors.get_editingCore().get_behaviors().get_rowAdding(); var addNewRow = addRowBehavior.get_row(); var activation = behaviors.get_activation(); activation.set_activeCell(addNewRow.get_cell(0)); addRowBehavior.enterEditMode(addNewRow.get_cell(0)); } </script>"; Page.RegisterClientScriptBlock("wdgList_Editing_RowAdded", strCode); } }
private class CustomItemTemplate : ITemplate { public void InstantiateIn(Control container) { Button edit = new Button(); edit.Text = "Edit "; edit.OnClientClick = "BLOCKED SCRIPTreturn GridDepartments_Selection_RowSelectionChanged();";
Button delete = new Button(); delete.Text = "Delete"; delete.OnClientClick = "BLOCKED SCRIPTreturn templatedClicked();";
//HtmlButton edit = new HtmlButton(); //edit.InnerText = "Edit "; //edit.Attributes.Add("OnClientClick", "BLOCKED SCRIPTreturn GridDepartments_Selection_RowSelectionChanged();");
//HtmlButton delete = new HtmlButton(); //delete.InnerText = "Delete"; //delete.Attributes.Add("OnClientClick", "BLOCKED SCRIPTreturn templatedClicked();");
container.Controls.Add(edit); container.Controls.Add(delete); } }
private void BindWebgrid() { COETableEditorBOList boList = COETableEditorBOList.NewList(); boList.TableName = CurrentTable; //init the dal in TableName property define labTableDescription.Text = COETableEditorUtilities.getTableDescription(CurrentTable); ReturnDataTable = COETableEditorBOList.getTableEditorDataTable(CurrentTable); wdgList.Columns.Clear(); DataTable dtGridSource = new DataTable(); int intFlag; int columnIndex = 0; List<Column> lstColumn = COETableEditorUtilities.getColumnList(CurrentTable);
if (wdgList.Columns.Count == 0) { foreach (DataColumn dc in ReturnDataTable.Columns) { DataColumn dcGridSource = new DataColumn(dc.ColumnName, dc.DataType); //dtGridSource.Columns.Add(dcGridSource); BoundDataField bdField = new BoundDataField(true); bdField.DataFieldName = dc.ColumnName; bdField.Key = dc.ColumnName; bdField.Header.Text = dc.ColumnName; bdField.Width = Unit.Pixel(130); wdgList.Columns.Add(bdField);
if (COETableEditorUtilities.getLookupLocation(CurrentTable, lstColumn[columnIndex].FieldName).Length != 0) { if (COETableEditorUtilities.getIsStructureLookupField(CurrentTable, lstColumn[columnIndex].FieldName)) { _includeStructure = true; bdField.Key = "CDXValue_" + columnIndex.ToString(); bdField.Hidden = true; TemplateDataField tcChemDraw = new TemplateDataField(); tcChemDraw.Header.Text = dc.ColumnName; ; tcChemDraw.Key = "ChemDraw_" + columnIndex.ToString(); tcChemDraw.ItemTemplate = new IndexTypeChemDraw(); wdgList.Columns.Add(tcChemDraw); } } else { //Updated on 2008/11/24 to fix the lookup field bug if (COETableEditorUtilities.getIsStructure(CurrentTable, lstColumn[columnIndex].FieldName)) { _includeStructure = true; bdField.Key = "CDXValue_" + columnIndex.ToString(); bdField.Hidden = true; TemplateDataField tcChemDraw = new TemplateDataField(); tcChemDraw.Header.Text = dc.ColumnName; tcChemDraw.Key = "ChemDraw_" + columnIndex.ToString(); tcChemDraw.ItemTemplate = new IndexTypeChemDraw(); wdgList.Columns.Add(tcChemDraw); } }
columnIndex = columnIndex + 1; }
TemplateDataField field1 = new TemplateDataField(); field1.Key = "TemplateColumn1"; field1.Header.Text = "Edit";
this.wdgList.Columns.Add(field1);
TemplateDataField templateColumn1 = (TemplateDataField)this.wdgList.Columns["TemplateColumn1"]; templateColumn1.ItemTemplate = new CustomItemTemplate(); } else { if (wdgList.Columns.Count != 0) { wdgList.Columns.Clear(); } foreach (DataColumn dc in ReturnDataTable.Columns) { DataColumn dcGridSource = new DataColumn(dc.ColumnName, dc.DataType); dtGridSource.Columns.Add(dcGridSource); BoundDataField bdField = new BoundDataField(true); bdField.DataFieldName = dc.ColumnName; bdField.Key = dc.ColumnName; bdField.Header.Text = dc.ColumnName; bdField.Width = Unit.Pixel(130); wdgList.Columns.Add(bdField); }
TemplateDataField templateColumn1 = (TemplateDataField)this.wdgList.Columns["TemplateColumn1"]; templateColumn1.ItemTemplate = new CustomItemTemplate(); } if (dtGridSource.Rows.Count == 0) { if (dtGridSource.Columns.Count == 0) { foreach (DataColumn dc in ReturnDataTable.Columns) { DataColumn dcGridSource = new DataColumn(dc.ColumnName, dc.DataType); dtGridSource.Columns.Add(dcGridSource); } } //for (int intRowCount = 0; intRowCount < DtGrid.Rows.Count; intRowCount++) //{ //dtGridSource.Rows.Add(DtGrid.Rows[intRowCount].ItemArray); //}
dtGridSource.PrimaryKey = new DataColumn[] { dtGridSource.Columns[0] }; wdgList.DataKeyFields = dtGridSource.Columns[0].ColumnName;
//Create Filtering Behaviour this.wdgList.Behaviors.CreateBehavior<Filtering>();
//Create Sorting Behaviour this.wdgList.Behaviors.CreateBehavior<Sorting>();
//Create Row Selectors Behaviour this.wdgList.Behaviors.CreateBehavior<RowSelectors>();
if (wdgList.Behaviors.Filtering.Filtered == true || ((System.Collections.CollectionBase)(wdgList.Behaviors.Sorting.SortedColumns)).Count != 0 || wdgList.Behaviors.RowSelectors.Enabled == true) { wdgList.Behaviors.Clear(); this.wdgList.Behaviors.CreateBehavior<Filtering>(); this.wdgList.Behaviors.CreateBehavior<Sorting>(); }
//Initalize The Client Method For Columns View And Tooltip //wdgList.ClientEvents.Initialize = "initGrid”; //wdgList.ClientEvents.AJAXResponse = "initGrid”; wdgList.Height = Unit.Pixel(500); wdgList.Width = Unit.Pixel(700); wdgList.DefaultColumnWidth = Unit.Pixel(50); //Create Virtual Scrolling Behaviour VirtualScrolling vrtScrolling = this.wdgList.Behaviors.CreateBehavior<VirtualScrolling>(); vrtScrolling.RowCacheFactor = 50;
//Create Row, Column & Cell Selection Behaviour Selection gridSelection = this.wdgList.Behaviors.CreateBehavior<Selection>(); gridSelection.CellClickAction = CellClickAction.Row; gridSelection.CellClickAction = CellClickAction.Cell; gridSelection.RowSelectType = SelectType.Multiple; gridSelection.ColumnSelectType = SelectType.Multiple; gridSelection.CellSelectType = SelectType.Multiple;
//Create Paging Behaviour Paging gridPaging = this.wdgList.Behaviors.CreateBehavior<Paging>(); gridPaging.PagerAppearance = PagerAppearance.Bottom;
gridPaging.PageSize = 15; gridPaging.QuickPages = 2;
//Create Row Selectors Behaviour RowSelectors rowSelectors = this.wdgList.Behaviors.CreateBehavior<RowSelectors>(); rowSelectors.RowNumbering = true;
//Create Activation, Column Moving & Column Resizing Behaviours this.wdgList.Behaviors.CreateBehavior<Activation>(); this.wdgList.Behaviors.CreateBehavior<ColumnMoving>(); this.wdgList.Behaviors.CreateBehavior<ColumnResizing>();
//Create CellEditing Behaviour this.wdgList.Behaviors.CreateBehavior<EditingCore>(); this.wdgList.Behaviors.EditingCore.Behaviors.CreateBehavior<CellEditing>();
//Create RowAdding Behaviour this.wdgList.Behaviors.CreateBehavior<EditingCore>(); this.wdgList.Behaviors.EditingCore.Behaviors.CreateBehavior<RowAdding>();
//Create Deleting Behaviour this.wdgList.Behaviors.CreateBehavior<EditingCore>(); this.wdgList.Behaviors.EditingCore.Behaviors.CreateBehavior<RowDeleting>();
// Create Edit Cell Settings EditingColumnSetting settingEditColumn = new EditingColumnSetting(); settingEditColumn.ColumnKey = dtGridSource.Columns[0].ColumnName; settingEditColumn.ReadOnly = true; settingEditColumn.EditorID = dtGridSource.Columns[0].ColumnName;
// Create Add Row Settings RowAddingColumnSetting settingAddColumn = new RowAddingColumnSetting(); settingAddColumn.ColumnKey = dtGridSource.Columns[0].ColumnName; settingAddColumn.ReadOnly = true; settingAddColumn.EditorID = dtGridSource.Columns[0].ColumnName;
// Edit Cell, Add Row & Delete Row settings this.wdgList.Behaviors.EditingCore.Behaviors.CellEditing.EditModeActions.EnableOnActive = true; this.wdgList.Behaviors.EditingCore.Behaviors.RowAdding.EditModeActions.EnableOnActive = true; this.wdgList.Behaviors.EditingCore.Behaviors.RowDeleting.Enabled = true;
this.wdgList.Behaviors.EditingCore.Behaviors.CellEditing.ColumnSettings.Add(settingEditColumn); this.wdgList.Behaviors.EditingCore.Behaviors.RowAdding.ColumnSettings.Add(settingAddColumn);
wdgList.DataSource = ReturnDataTable; wdgList.DataBind();
} }
Hello,
Thank you for the sample shared !
Please try the following:
...
delete.OnClientClick = "templatedClicked()"
<script type='text/javascript'> function templatedClicked() {var result = confirm('Are you sure you want to delete this row');if (result) {
//initiate row delete from the client and call the grid's commitvar grid = $find('COETableManager_wdgList');var editing = grid.get_behaviors().get_editingCore();
//call do postback to load the new selected data in the second grid?__doPostBack();}return false;}</script>";
Please have in mind that return false on the last line will do prevent the postback that the button is about to trigger. However editing.commit(); will trigger a partial postback that will update the grid only. and __doPostBack(); will also cause a postback as you know.
Please let me know if this helps or if you have any further questions on the matter.
I'm just following up to see if you need any further assistance with this issue. If so please let me know.