This is a migrated thread and some comments may be shown as answers.

Attach Handler to Auto-Generated Editor

4 Answers 64 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Marbry
Top achievements
Rank 1
Marbry asked on 22 Dec 2011, 07:06 PM
Hello, I have a grid (gridEdit) where I am trying to attach a client side handler to certain auto generated edit elements.  I seem to have a valid reference and no errors are being thrown, but the handler never gets called.

Code behind:
protected void gridEdit_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem form = (GridEditableItem)e.Item;
        for (int i = 0; i < _charFieldLength.Count; i++)
        {
            TextBox dataField = (TextBox)form[_charFieldLength[i].FieldName].Controls[0];
            dataField.MaxLength = _charFieldLength[i].DataLength;
        }
        for (int j = 0; j < form.Controls.Count; j++)
        {
            try
            {
                if (form.Controls[j].Controls[0] is RadNumericTextBox)
                {
                    RadNumericTextBox rnt = (RadNumericTextBox)form.Controls[j].Controls[0];
                    rnt.ClientEvents.OnKeyPress = "NumericOnKeyPress";
                }
            }
            catch (Exception ex) { }
        }
    }
}


Javascript:
function NumericOnKeyPress( sender, args )
{
    alert( 'In KeyPressHandler' );
}

4 Answers, 1 is accepted

Sort by
0
Antonio Stoilkov
Telerik team
answered on 27 Dec 2011, 09:39 AM
Hi Marbry,

Based on the code provided it is hard to determine the problem. You could try debugging the problem. Place a breakpoint  on the line where OnKeyPress is set and observe if it is ever called. Note that you could access the RadNumericTextBox with form.FindControl("id") and then set the client event.

Hope this information will prove helpful.

Regards,
Antonio Stoilkov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Marbry
Top achievements
Rank 1
answered on 27 Dec 2011, 02:32 PM
Yes it is called.  The event handler contains the function name after that line is called, it just never fires the client function.  That's what's confusing, the server code "appears" to be doing exactly what I want when I step through it.

I'm wondering if this is related to it being an autogenerated edit box in the grid, that the handler is being overwritten at some point after I set it?

Since the control is generated by the grid, I won't know beforehand what the ID is, so I don't think using FindControl would work.  But like I stated, it appears to have a valid reference to the control anyway with the existing code when I step through it.  So that doesn't seem to be the problem.
0
Marbry
Top achievements
Rank 1
answered on 27 Dec 2011, 08:31 PM
One problem I discovered is that it is generating TextBox's as the editing items for some numeric fields.  I have some tinyint fields in SQL that come through the datasource as byte, and it's not putting those in a RadNumericTextBox as I might expect.

Still, with the modified code below, it doesn't attach the event handler to the editing control.  I see the "test" text populated in the box when "Edit" is selected, but it still never fires the client handler.

protected void gridEdit_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem form = (GridEditableItem)e.Item;
        for (int i = 0; i < _charFieldLength.Count; i++)
        {
            TextBox dataField = (TextBox)form[_charFieldLength[i].FieldName].Controls[0];
            dataField.MaxLength = _charFieldLength[i].DataLength;
        }
        // Attach the client handler for keypress filtering.
        for (int j = 0; j < form.Controls.Count; j++)
        {
            try
            {
                if (form.Controls[j].Controls.Count > 0)
                {
                    if (form.Controls[j].Controls[0] is RadNumericTextBox)
                    {
                        RadNumericTextBox rnt = (RadNumericTextBox)form.Controls[j].Controls[0];
                        rnt.ClientEvents.OnKeyPress = "NumericOnKeyPress";
                    }
                    else if (form.Controls[j].Controls[0] is TextBox)
                    {
                        TextBox tb = (TextBox)form.Controls[j].Controls[0];
                        if (tb.MaxLength == 0)
                        {
                            tb.Attributes.Add("onkeypress", "NumericOnKeyPress");
                            tb.Text = "test";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                  
            }
        }
    }
}
0
Marbry
Top achievements
Rank 1
answered on 27 Dec 2011, 11:17 PM

Figured it out, it was the way in which the handler was being defined.  The code below works.

tb.Attributes.Add("onkeypress", "return NumericOnKeyPress();");
Tags
Grid
Asked by
Marbry
Top achievements
Rank 1
Answers by
Antonio Stoilkov
Telerik team
Marbry
Top achievements
Rank 1
Share this question
or