Search code examples
c#asp.netgridviewwebusercontrol

FindControl of a Textbox in a UserControl OnRowUpdate


Hi I've got an error after clicking update and triggering the OnRowUpdate event.. what is the right format of getting the value of a textbox in a usercontrol?

Error 55 'System.Web.UI.UserControl' does not contain a definition for 'PlanID' and no extension method 'PlanID' accepting a first argument of type 'System.Web.UI.UserControl' could be found (are you missing a using directive or an assembly reference

protected void grd_Plan_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            string PlanCode =((UserControl)Grid_Plan.Rows[e.RowIndex].FindControl("ucPlanID")).PlanID;
        }

//usercontrol code behind

public string PlanID
        {
            get
            {
                return txtPlanID.Text;
            }
            set
            {
                txtPlanID.Text = value;
            }
        }

Solution

  • Try this:-

    UserControl ucPlanID = (UserControl)Grid_Plan.Rows[e.RowIndex].FindControl("ucPlanID");
    if (ucPlanID != null)
    {
       string PlanCode = ((TextBox)ucPlanID.FindControl("PlanID")).Text;
    }
    

    Considering, PlanID is the ID of your textbox.