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

Properties and User Control(ascx)

4 Answers 513 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Anthony
Top achievements
Rank 1
Anthony asked on 28 Aug 2014, 12:27 AM
Hi Everyone, 

I need some help.  I'm building a radgrid with ascx control like this demo Edit Form Type . However, I made some changes where I'm using Entity Framework to store information back to the database.  I'm using the below code to capture the textbox and dropdownlist value and then passing them into my entity code.  

This is where I need some help.  Instead of capturing the textbox value directly, I want to create a property and apply some validation rule in the properties. Then I want to access these properties to get the values from the aspx.cs file. Instead of  string CostCenterName = (userControl.FindControl("txtCostCenterName") as RadTextBox).Text, I need to access the properties.

Code Behind on the .ascx.cs file
public string CostCenterName
        {
             
            get
            {
                return txtCostCenterName.Text;
            }
             
        }

Code Behind on .aspx.cs file
UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
          int CostCenterNo = Int32.Parse((userControl.FindControl("txtCostCenterNo") as RadNumericTextBox).Text);
          string CostCenterName = (userControl.FindControl("txtCostCenterName") as RadTextBox).Text;
          int CostCenterType= Int32.Parse((userControl.FindControl("ddlCostCenterType") as RadDropDownList).SelectedValue);
          string UpdateBy = UserName;
          DateTime UpdateDateTime = DateTime.Now;
 
          var insertNew = new CostCenter
          {
              CostCenterNumber = CostCenterNo,
              CostCenterName = CostCenterName,
              CostCenterTypeID = CostCenterType,
              UpdateBy = UpdateBy,
              UpdateDateTime = UpdateDateTime
          };
          try
          {
              DbContext.AddToCostCenters(insertNew);
              DbContext.SaveChanges();
              SetMessage("Cost Center No " + CostCenterNo + " was added!");
          }

4 Answers, 1 is accepted

Sort by
0
Anthony
Top achievements
Rank 1
answered on 28 Aug 2014, 02:39 AM
Ok...I  figure  it  out.  I need to add user control in the aspx page.
0
Anthony
Top achievements
Rank 1
answered on 28 Aug 2014, 08:58 PM
I think I jump the gun here, registering a user control does not work in the sense of editing RadGrid. I would still appreciate any help of accessing a property when I click on the insert or edit button. 
0
Princy
Top achievements
Rank 2
answered on 29 Aug 2014, 05:13 AM
Hi Anthony,

I'm not clear about your requirement, i guess you want to access the textbox inside a user control in edit/insert form. You can use the ItemCreated event of the radgrid and access the TextBox, set its properties there.

C#:
void grdSample_ItemCreated(object sender, GridItemEventArgs e)
{
   if (e.Item is GridEditableItem && e.Item.IsInEditMode)
  {
   GridEditableItem editItem = (GridEditableItem)e.Item;
   UserControl MyUserControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
   //Access textbox and set it properties 
   TextBox txtCostCenterNo = (TextBox)MyUserControl.FindControl("txtCostCenterNo");
   txtCostCenterNo.ReadOnly = true;
  }
}

Thanks,
Princy
0
Anthony
Top achievements
Rank 1
answered on 29 Aug 2014, 02:54 PM
Hi Princy,

Sorry if my question was not worded  clearly but I want to access a property of the ascx page. Not the element property. For example,

public string UserName
{
     get
            {
              return txtUserName.Text;
             }
}
Tags
Grid
Asked by
Anthony
Top achievements
Rank 1
Answers by
Anthony
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or