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

RadGrid GridTextBoxColumnEditor TextBoxControl.ID IS NULL

7 Answers 276 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ron
Top achievements
Rank 1
Ron asked on 25 Apr 2012, 05:41 PM
I'm trying to add a simple range validator to a GridBoundColumn in ItemCreated.
However, TextBoxControl.ID is null.
If I set the ID, then changes from "InPlace" edit forms are also null in e.Item.OwnerTableView.ExtractValuesFromItem

protected

 

void rgMembers_ItemCreated(object sender, GridItemEventArgs e)

 

{

 

    if (e.Item is GridEditableItem && e.Item.IsInEditMode)

 

    {    

 

        GridEditableItem edititem = (GridEditableItem)e.Item;

 

 

        / /validate Percentage

 

 

        GridTextBoxColumnEditor editorP = (GridTextBoxColumnEditor)edititem.EditManager.GetColumnEditor("Percentage");

 

 

        TableCell cellP = (TableCell)editorP.TextBoxControl.Parent;

 

 

        RangeValidator validatorP = new RangeValidator();

 

        validatorP.ID =

"rgvPercentage";

 

        validatorP.ControlToValidate = editorP.TextBoxControl.ID;  //<-- Value is NULL

        validatorP.Type =

ValidationDataType.Double;

 

        validatorP.Display =

ValidatorDisplay.Dynamic;

 

        validatorP.ErrorMessage =

"<br/>% must be between 0 and 100.";

 

        validatorP.MinimumValue =

"0";

 

        validatorP.MaximumValue =

"100";

 

        cellP.Controls.Add(validatorP);
    }

Why is the TextBoxControl.ID null ?
Seems to work with other type fields (i.e RadDatePicker.ID)

Thanks

7 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 26 Apr 2012, 05:18 AM
Hello Ron,

Please check below code snippet its work correctly.
  <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID">
                   </telerik:GridBoundColumn>
<telerik:GridNumericColumn DataField="ID" UniqueName="TestID">
                   </telerik:GridNumericColumn>

protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridEditableItem && e.Item.IsInEditMode)
       {
           
 
           GridNumericColumnEditor neditor = ((GridEditableItem)e.Item).EditManager.GetColumnEditor("TestID") as GridNumericColumnEditor;
           string strTestID = neditor.NumericTextBox.ID;
 
           GridTextBoxColumnEditor beditor = ((GridEditableItem)e.Item).EditManager.GetColumnEditor("ID") as GridTextBoxColumnEditor;
           string strID = neditor.TemplateControl.ID;
 
       }
   }


OR
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
 
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem item = e.Item as GridEditableItem;
            TextBox txt =  (item["ID"].Controls[0] as TextBox);
            // assing property here
        }
}



Thanks,
Jayesh Goyani
0
Shinu
Top achievements
Rank 2
answered on 26 Apr 2012, 05:54 AM
Hi Ron,

Try the following code to add a Range Validator for Bound Column when edit mode is InPlace.

C#:
protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem edititem = (GridEditableItem)e.Item;
        GridTextBoxColumnEditor editorP = (GridTextBoxColumnEditor)edititem.EditManager.GetColumnEditor("Percentage");
        TableCell cellP = (TableCell)editorP.TextBoxControl.Parent;
        RangeValidator validatorP = new RangeValidator();
        editorP.TextBoxControl.ID = "rgvPercentage";
        validatorP.ControlToValidate = editorP.TextBoxControl.ID;
        validatorP.Type = ValidationDataType.Double;
        validatorP.Display = ValidatorDisplay.Dynamic;
        validatorP.ErrorMessage = "<br/>% must be between 0 and 100.";
        validatorP.MinimumValue = "0";
        validatorP.MaximumValue = "100";
        cellP.Controls.Add(validatorP);
    }
}

Thanks,
Shinu.
0
Ron
Top achievements
Rank 1
answered on 26 Apr 2012, 02:50 PM
I used the GridNumericColumn in place of the GridBoundControl, because the ID was available for that method.
For GridBoundControl, the ID is still null whether I use TextBoxContro.ID or TemplateControl.ID
0
Ron
Top achievements
Rank 1
answered on 26 Apr 2012, 02:53 PM
setting the TextBoxControl.ID makes the validator work, but it ends up setting the values entered in the field to null, so I cannot retrieve the data entered. (i.e. if you do an ExtractValuesFromItem, the value for the validated field will be null if the TextBoxControl.ID is set in code)
0
Shinu
Top achievements
Rank 2
answered on 27 Apr 2012, 07:07 AM
Hi Ron,

Unfortunately i couldn't replicate the issue.Please check the code snippet I tried.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateEditColumn="true"  onitemcreated="RadGrid1_ItemCreated" onitemdatabound="RadGrid1_ItemDataBound" onitemcommand="RadGrid1_ItemCommand">
    <MasterTableView EditMode="InPlace">
        <Columns>
            <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID"></telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem edititem = (GridEditableItem)e.Item;
        GridTextBoxColumnEditor editorP = (GridTextBoxColumnEditor)edititem.EditManager.GetColumnEditor("OrderID");
        TableCell cellP = (TableCell)editorP.TextBoxControl.Parent;
        RangeValidator validatorP = new RangeValidator();
        editorP.TextBoxControl.ID = "rgvPercentage";
        validatorP.ControlToValidate = editorP.TextBoxControl.ID;
        validatorP.Type = ValidationDataType.Double;
        validatorP.Display = ValidatorDisplay.Dynamic;
        validatorP.ErrorMessage = "<br/>% must be between 0 and 100.";
        validatorP.MinimumValue = "0";
        validatorP.MaximumValue = "100";
        cellP.Controls.Add(validatorP);
    }
 }  
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
 {
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        Hashtable oldValues = new Hashtable();
        e.Item.OwnerTableView.ExtractValuesFromItem(oldValues, editedItem);
        string val = oldValues["OrderID"].ToString();//Get the old value from the BoundColumn
    }
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.UpdateCommandName)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        Hashtable newValues = new Hashtable();
        e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
        string val = newValues["OrderID"].ToString();  //Get the new Value
    }
}

Hope this helps.
Thanks,
Shinu.
0
Ron
Top achievements
Rank 1
answered on 27 Apr 2012, 01:31 PM
This doesn't work.

if you set editorP.TextBoxControl.ID = "rgvPercentage";
then that value of "OrderID" is null in newValues.

Why do you need to do this, anyway.  Why does TextBoxControl.ID return null for GridBoundColumns?
0
Jayesh Goyani
Top achievements
Rank 2
answered on 28 Apr 2012, 07:38 PM
Hello ron,

Can you please provide your (full) code.?

Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Ron
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Shinu
Top achievements
Rank 2
Ron
Top achievements
Rank 1
Share this question
or