Better way to find control in ASP.NET

后端 未结 9 1294
太阳男子
太阳男子 2020-11-22 03:17

I have a complex asp.net form,having even 50 to 60 fields in one form like there is Multiview, inside MultiView I have a GridView, and inside GridV

9条回答
  •  花落未央
    2020-11-22 04:03

    The following example defines a Button1_Click event handler. When invoked, this handler uses the FindControl method to locate a control with an ID property of TextBox2 on the containing page. If the control is found, its parent is determined using the Parent property and the parent control's ID is written to the page. If TextBox2 is not found, "Control Not Found" is written to the page.

    private void Button1_Click(object sender, EventArgs MyEventArgs)
    {
          // Find control on page.
          Control myControl1 = FindControl("TextBox2");
          if(myControl1!=null)
          {
             // Get control's parent.
             Control myControl2 = myControl1.Parent;
             Response.Write("Parent of the text box is : " + myControl2.ID);
          }
          else
          {
             Response.Write("Control not found");
          }
    }
    

提交回复
热议问题