Showing posts with label Telerik. Show all posts
Showing posts with label Telerik. Show all posts

Sunday, November 20, 2016

Kendo Grid Tips: Adding New Row to Kendo Gird with Enter Key Press

There were requirements to add new row to editable gird when user click on last editable cell. Using addRow() function of kendo gird we can add new row to data gird. However to use that function we need to identify the correct key stroke and correct cell to invoke addRow() function. Following code snippet shows how to add new row to grid if user press enter key on 4th column.

We can change javascript key value to modify following script to trigger on any key (e.g. if change (code == 9) you can add new row with Tab key)

    $(document).ready(function () {
        $("#MyGrid").on("keyup", "tr", function (e) {
            var code = (e.keyCode ? e.keyCode : e.which);
            var td = $(e.target).closest('td');


            if (code == 13) {
                var tdIndex = td.index();


                if (tdIndex == 3) {
                    var grid = $("#MyGrid").data("kendoGrid");
                    grid.addRow();
                }
            }
        });
    }); 

Saturday, March 24, 2012

C# - Change attributes of all controls in ASP page

Sometime it requires disabling all controls in the web page using code-behind. Can use following logic to disable all controls in the page without go through each and every control.
private void MakePageControlsReadOnly(Control pageControl)
{
foreach (Control childControl in pageControl.Controls)
{
if (childControl.GetType() == typeof(Control type you want to disable))
{
((Control type you want to disable)childControl).Enabled = false;
}
if (childControl.Controls.Count > 0)
{
MakePageControlsReadOnly(childControl);
}
}
}

This not only for page level this pageControl variable can be a control which contains child controls like table contains link buttons. This generic method can change to change any attribute of the control rather than enable/disable.

Change visibility of child control.

((Control type you want to disable)childControl).Visible = false;

Adding attributes for all link buttons in the table cell.

((TableCell)childControl).Attributes.Add("OnClick", "return false;");


This control type can be extend to other control types like Rad Controls.

((RadGrid)childControl).Attributes.Add("OnClick", "return false;");

Complete example

private void MakePageControlsReadOnly(Control pageControl)
{
foreach (Control childControl in pageControl.Controls)
{
if (childControl.GetType() == typeof(TextBox))
{
((TextBox)childControl).Enabled = false;
}
else if (childControl.GetType() == typeof(CheckBox))
{
((CheckBox)childControl).Enabled = false;
}
else if (childControl.GetType() == typeof(DropDownList))
{
((DropDownList)childControl).Enabled = false;
}
else if (childControl.GetType() == typeof(RadDatePicker))
{
((RadDatePicker)childControl).Enabled = false;
}
else if (childControl.GetType() == typeof(Button))
{
((Button)childControl).Enabled = false;
}
else if (ChildControlsCreated.GetType() == typeof(RadTimePicker))
{
((RadTimePicker)childControl).Enabled = false;
}
else if (ChildControlsCreated.GetType() == typeof(LinkButton))
{
((LinkButton)childControl).Enabled = false;
}
else if ((childControl).GetType() == typeof(TableCell))
{
((TableCell)childControl).Attributes.Add("OnClick", "return false;");
}
else if ((childControl).GetType() == typeof(RadGrid))
{
((RadGrid)childControl).Attributes.Add("OnClick", "return false;");
}

if (childControl.Controls.Count > 0)
{
MakePageControlsReadOnly(childControl);
}
}
}

How invoke this method in page level.

MakePageControlsReadOnly ((Control)this);