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

Textbox width and validation in automatic editmode

3 Answers 88 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Swapnil
Top achievements
Rank 1
Swapnil asked on 28 Nov 2013, 05:09 AM
Hi,
how can i do increase Textbox width and validation in automatic editmode alongwith i am using custom template mode for insert
previously i used like this but its not working now
i have also attached image ,look it for reference

protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
   {
       if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode))
       {
           //-------------Find TextBox In EditMode And Apply Css To Them------------
           //GridEditableItem edititem = (GridEditableItem)e.Item;
           //TextBox TxtEntityName = (TextBox)edititem["EntityName"].Controls[0];
           //TextBox TxtEntityType = (TextBox)edititem["EntityType"].Controls[0];
           //TextBox TxtEntityPath = (TextBox)edititem["EntityPath"].Controls[0];
           //TextBox TxtDescription = (TextBox)edititem["Description"].Controls[0];
           //TextBox TxtFileName = (TextBox)edititem["FileName"].Controls[0];
           //TxtEntityName.CssClass = "riEditFormTextBox";
           //TxtEntityType.CssClass = "riEditFormTextBox";
           //TxtEntityPath.CssClass = "riEditFormTextBox";
           //TxtDescription.CssClass = "riEditFormTextBox";
           //TxtFileName.CssClass = "riEditFormTextBox";
       }
 
           //Validations For Textbox In Insert And Edit Mode
       if ((e.Item is GridEditableItem && e.Item.IsInEditMode) || (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted))
       {
           GridEditableItem item = e.Item as GridEditableItem;
           GridTextBoxColumnEditor editor = (GridTextBoxColumnEditor)item.EditManager.GetColumnEditor("EntityName");
           TableCell cell = (TableCell)editor.TextBoxControl.Parent;
           RequiredFieldValidator validator = new RequiredFieldValidator();
           validator.ControlToValidate = editor.TextBoxControl.ID;
           validator.ErrorMessage = " * Required";
           validator.Font.Bold = true;
           cell.Controls.Add(validator);
       }
Thanks

3 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 28 Nov 2013, 07:28 AM
Hi Swapnil,

You can set the width of the TextBox of an AutoGenerated EditForm in the ItemDatabound Event and the RequiredFieldValidator for the AutoGenerated EditForm can be written as follows :

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
 {
  if (e.Item is GridEditableItem && e.Item.IsInEditMode && !e.Item.OwnerTableView.IsItemInserted)
    {      
        GridEditableItem edititem = (GridEditableItem)e.Item;
        TextBox TxtEntityName = (TextBox)edititem["EntityName"].Controls[0];
        TxtEntityName.Width = Unit.Pixel(100);
    }
 }
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode && !e.Item.OwnerTableView.IsItemInserted)
    {
        GridEditableItem item = e.Item as GridEditableItem;
        GridTextBoxColumnEditor editor = (GridTextBoxColumnEditor)item.EditManager.GetColumnEditor("EntityName");
        TableCell cell = (TableCell)editor.TextBoxControl.Parent;
        RequiredFieldValidator validator = new RequiredFieldValidator();
        validator.ControlToValidate = editor.TextBoxControl.ID;
        validator.ErrorMessage = "*";
        cell.Controls.Add(validator);
    
}

For the Custom Template EditForm you can set the TextBox width in the form and add the RequiredFieldValidator along with it as follows:

ASPX:
<telerik:RadTextBox ID="RadTextBoxEntityName" runat="server" Width="200px" Text='<%# Bind("EntityName" ) %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
ControlToValidate="RadTextBoxEntityName"></asp:RequiredFieldValidator>

Thanks,
Shinu
0
Swapnil
Top achievements
Rank 1
answered on 28 Nov 2013, 07:46 AM
Thanks
I did This Like
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
       if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode))
       {
           //-------------Find TextBox In EditMode And Apply Css To Them------------
           GridEditableItem edititem = (GridEditableItem)e.Item;
           TextBox TxtEntityName = (TextBox)edititem["UserName"].Controls[0];
           TextBox TxtEntityType = (TextBox)edititem["FirstName"].Controls[0];
           TextBox TxtEntityPath = (TextBox)edititem["LastName"].Controls[0];
           TextBox TxtDescription = (TextBox)edititem["UserEmail"].Controls[0];
           TextBox TxtFileName = (TextBox)edititem["UserPhone"].Controls[0];
           TxtEntityName.CssClass = "riEditFormTextBox";
           TxtEntityType.CssClass = "riEditFormTextBox";
           TxtEntityPath.CssClass = "riEditFormTextBox";
           TxtDescription.CssClass = "riEditFormTextBox";
           TxtFileName.CssClass = "riEditFormTextBox";
 
           //-------Find Textbox In Automatic Update Mode And Apply Validations To Them-------
           GridEditableItem item = e.Item as GridEditableItem;
           GridTextBoxColumnEditor editor = (GridTextBoxColumnEditor)item.EditManager.GetColumnEditor("UserName");
           TableCell cell = (TableCell)editor.TextBoxControl.Parent;
           RequiredFieldValidator validator = new RequiredFieldValidator();
           validator.ControlToValidate = editor.TextBoxControl.ID;
           validator.ErrorMessage = " * Required";
           validator.Font.Bold = true;
           cell.Controls.Add(validator);
 
           GridEditableItem item1 = e.Item as GridEditableItem;
           GridTextBoxColumnEditor editor1 = (GridTextBoxColumnEditor)item1.EditManager.GetColumnEditor("FirstName");
           TableCell cell1 = (TableCell)editor1.TextBoxControl.Parent;
           RequiredFieldValidator validator1 = new RequiredFieldValidator();
           validator1.ControlToValidate = editor1.TextBoxControl.ID;
           validator1.ErrorMessage = " * Required";
           validator1.Font.Bold = true;
           cell1.Controls.Add(validator1);
 
           GridEditableItem item2 = e.Item as GridEditableItem;
           GridTextBoxColumnEditor editor2 = (GridTextBoxColumnEditor)item2.EditManager.GetColumnEditor("LastName");
           TableCell cell2 = (TableCell)editor2.TextBoxControl.Parent;
           RequiredFieldValidator validator2 = new RequiredFieldValidator();
           validator2.ControlToValidate = editor2.TextBoxControl.ID;
           validator2.ErrorMessage = " * Required";
           validator2.Font.Bold = true;
           cell2.Controls.Add(validator2);
 
           GridEditableItem item3 = e.Item as GridEditableItem;
           GridTextBoxColumnEditor editor3 = (GridTextBoxColumnEditor)item3.EditManager.GetColumnEditor("UserEmail");
           TableCell cell3 = (TableCell)editor3.TextBoxControl.Parent;
           RequiredFieldValidator validator3 = new RequiredFieldValidator();
           validator3.ControlToValidate = editor3.TextBoxControl.ID;
           validator3.ErrorMessage = " * Required";
           validator3.Font.Bold = true;
           cell3.Controls.Add(validator3);
 
           GridEditableItem item4 = e.Item as GridEditableItem;
           GridTextBoxColumnEditor editor4 = (GridTextBoxColumnEditor)item4.EditManager.GetColumnEditor("UserPhone");
           TableCell cell4 = (TableCell)editor4.TextBoxControl.Parent;
           RequiredFieldValidator validator4 = new RequiredFieldValidator();
           validator4.ControlToValidate = editor4.TextBoxControl.ID;
           validator4.ErrorMessage = " * Required";
           validator4.Font.Bold = true;
           cell4.Controls.Add(validator4);
          
       }
       if (e.Item is GridEditFormInsertItem)
       {
           GridEditableItem edit = (GridEditableItem)e.Item;
           if (Convert.ToInt32(Session["RoleId"].ToString()) == 1)
           {
               RadComboBox cmbUserType = (RadComboBox)edit.FindControl("cmbusertype");
               DataTable dt1 = ObjGlobas.UserType();
               cmbUserType.DataSource = dt1;
               cmbUserType.DataTextField = "UserType";
               cmbUserType.DataValueField = "UserTypeId";
               cmbUserType.DataBind();
 
               RadComboBox cmbcompanyname = (RadComboBox)edit.FindControl("cmbcompanyname");
               DataTable dt2 = ObjGlobas.CompDetails();
               cmbcompanyname.DataSource = dt2;
               cmbcompanyname.DataTextField = "CompName";
               cmbcompanyname.DataValueField = "CompId";
               cmbcompanyname.DataBind();
           }
           if (Convert.ToInt32(Session["RoleId"].ToString()) == 2)
           {
               RadComboBox cmbUserType = (RadComboBox)edit.FindControl("cmbusertype");
               DataTable dt3 = ObjGlobas.UserTypeDynamic();
               cmbUserType.DataSource = dt3;
               cmbUserType.DataTextField = "UserType";
               cmbUserType.DataValueField = "UserTypeId";
               cmbUserType.DataBind();
 
               UserId = Convert.ToInt32(Session["UserId"]);
               RadComboBox cmbComapnyName = (RadComboBox)edit.FindControl("cmbcompanyname");
               DataTable dt4 = ObjGlobas.CompDetailsDynamic(UserId);
               cmbComapnyName.DataSource = dt4;
               cmbComapnyName.DataTextField = "CompName";
               cmbComapnyName.DataValueField = "CompId";
               cmbComapnyName.DataBind();
           }
 
           
       }
   }
Is it correct way or should i change validation in itemcretaed event ?
Thanks
0
Shinu
Top achievements
Rank 2
answered on 29 Nov 2013, 04:07 AM
Hi,

You can wire the ItemCreated event of the grid to add a validator as suggested in this help documentation. You need only a single GridEditableItem instance, with which you can access all the TextBoxes and add validator. Please check the following code snippet.

C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode && !e.Item.OwnerTableView.IsItemInserted)
    {
        GridEditableItem item = e.Item as GridEditableItem;
 
        GridTextBoxColumnEditor editor = (GridTextBoxColumnEditor)item.EditManager.GetColumnEditor("FirstName");
        TableCell cell = (TableCell)editor.TextBoxControl.Parent;
        RequiredFieldValidator validator = new RequiredFieldValidator();
        validator.ControlToValidate = editor.TextBoxControl.ID;
        validator.ErrorMessage = "*";
        cell.Controls.Add(validator);
 
        GridTextBoxColumnEditor editor1 = (GridTextBoxColumnEditor)item.EditManager.GetColumnEditor("LastName");
        TableCell cell1 = (TableCell)editor1.TextBoxControl.Parent;
        RequiredFieldValidator validator1 = new RequiredFieldValidator();
        validator1.ControlToValidate = editor1.TextBoxControl.ID;
        validator1.ErrorMessage = "*";
        cell1.Controls.Add(validator1);
        
 
        .  .  .
 
    }
}

Thanks,
Shinu.
Tags
Grid
Asked by
Swapnil
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Swapnil
Top achievements
Rank 1
Share this question
or