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

requiredfieldvalidator from code behind

1 Answer 99 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Erin
Top achievements
Rank 1
Erin asked on 26 Sep 2013, 01:34 PM
Hi,
How to set a requiredfieldvalidator from code behind.any one can show me a sample?
 
Thanks
Erin

1 Answer, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 1
answered on 26 Sep 2013, 01:39 PM
Hi Erin,

Please try the following code snippet.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false"          
AutoGenerateEditColumn="true" AllowPaging="true" OnItemCreated="RadGrid1_ItemCreated" >
  <MasterTableView DataKeyNames="OrderID">
        <Columns>
          <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID" />
          <telerik:GridBoundColumn DataField="ShipCity" HeaderText="ShipCity" UniqueName="ShipCity" />
         </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 item = e.Item as GridEditableItem;
            GridTextBoxColumnEditor editor = (GridTextBoxColumnEditor)item.EditManager.GetColumnEditor("ShipCity");
            TableCell cell = (TableCell)editor.TextBoxControl.Parent;
            RequiredFieldValidator validator = new RequiredFieldValidator();
            validator.ControlToValidate = editor.TextBoxControl.ID;
            validator.ErrorMessage = "*";
            validator.ForeColor = Color.Red;
            cell.Controls.Add(validator);
        }
  }

Thanks,
Princy
Tags
Grid
Asked by
Erin
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Share this question
or