Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to use "User Controls" in Web Form? [modified]

How to use "User Controls" in Web Form? [modified]

Scheduled Pinned Locked Moved ASP.NET
questioncsharpwinformssysadmintools
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nguyenhh
    wrote on last edited by
    #1

    Hi Net Experts! This maybe a simple question for you all, but I have not been able to get it works, I created a web form page .aspx, and a user control file .ascx. Here is the code in my .ascx file

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ControlFormBookSearch.ascx.cs" Inherits="Control_ControlFormBookSearch" %>

    <table style="padding:0; border:solid 0px;">
    <tr valign="middle">
    <td style="vertical-align:top;border-bottom:solid 0px">
    Keyword: <asp:TextBox ID="searchValue" runat="server" /> 
    </td>
    </tr>
    </table>

    From my web form .aspx file, I wanted to access the control searchType this way:

    <%@ Register src="ControlBookDetail.ascx" tagname="ControlBookDetail" tagprefix="uc9" %>
    <script runat="server">
    protected void btnSearch_Click(object sender, EventArgs e)
    {
    TextBox searchValue = (TextBox)Page.FindControl("searchValue");//never find this control
    if (searchValue != null)
    Response.Write(searchValue.Text);
    else Response.Write("Bad request");
    }

    But I have not been able to get it to work. Could you please show me how to fix this? thank you for your helps! -Hai

    Hai

    modified on Friday, July 4, 2008 1:26 AM

    S V 2 Replies Last reply
    0
    • N nguyenhh

      Hi Net Experts! This maybe a simple question for you all, but I have not been able to get it works, I created a web form page .aspx, and a user control file .ascx. Here is the code in my .ascx file

      <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ControlFormBookSearch.ascx.cs" Inherits="Control_ControlFormBookSearch" %>

      <table style="padding:0; border:solid 0px;">
      <tr valign="middle">
      <td style="vertical-align:top;border-bottom:solid 0px">
      Keyword: <asp:TextBox ID="searchValue" runat="server" /> 
      </td>
      </tr>
      </table>

      From my web form .aspx file, I wanted to access the control searchType this way:

      <%@ Register src="ControlBookDetail.ascx" tagname="ControlBookDetail" tagprefix="uc9" %>
      <script runat="server">
      protected void btnSearch_Click(object sender, EventArgs e)
      {
      TextBox searchValue = (TextBox)Page.FindControl("searchValue");//never find this control
      if (searchValue != null)
      Response.Write(searchValue.Text);
      else Response.Write("Bad request");
      }

      But I have not been able to get it to work. Could you please show me how to fix this? thank you for your helps! -Hai

      Hai

      modified on Friday, July 4, 2008 1:26 AM

      S Offline
      S Offline
      Sherin Iranimose
      wrote on last edited by
      #2

      Did you add the control to the page by using something like below

      nguyenhh wrote:

      protected void btnSearch_Click(object sender, EventArgs e) { TextBox searchValue = (TextBox)Page.FindControl("searchValue"); Control searchType = Page.FindControl("searchType"); //never find this control if (searchValue != null) Response.Write(searchValue.Text); else Response.Write("Bad request"); }

      This is not the way of using it. You should write some properties to your control, So that you can directly use your control properties. No need to find each control inside it.

      EVEN THE WORD IMPOSSIBLE SAYS I M POSSIBLE.

      N 1 Reply Last reply
      0
      • N nguyenhh

        Hi Net Experts! This maybe a simple question for you all, but I have not been able to get it works, I created a web form page .aspx, and a user control file .ascx. Here is the code in my .ascx file

        <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ControlFormBookSearch.ascx.cs" Inherits="Control_ControlFormBookSearch" %>

        <table style="padding:0; border:solid 0px;">
        <tr valign="middle">
        <td style="vertical-align:top;border-bottom:solid 0px">
        Keyword: <asp:TextBox ID="searchValue" runat="server" /> 
        </td>
        </tr>
        </table>

        From my web form .aspx file, I wanted to access the control searchType this way:

        <%@ Register src="ControlBookDetail.ascx" tagname="ControlBookDetail" tagprefix="uc9" %>
        <script runat="server">
        protected void btnSearch_Click(object sender, EventArgs e)
        {
        TextBox searchValue = (TextBox)Page.FindControl("searchValue");//never find this control
        if (searchValue != null)
        Response.Write(searchValue.Text);
        else Response.Write("Bad request");
        }

        But I have not been able to get it to work. Could you please show me how to fix this? thank you for your helps! -Hai

        Hai

        modified on Friday, July 4, 2008 1:26 AM

        V Offline
        V Offline
        viivekk
        wrote on last edited by
        #3

        This is sample userctrl with textbox inside:

        ]]>

        This is aspx page i include the user ctrl in this page, i use findcontrol method of usercontrol object, by using this u can find controls inside the usercontrols.

        ]]>
        ]]>

        protected void btn\_Click(object sender, EventArgs e)
        {
            ((TextBox)usrctrl1.FindControl("TextBox1")).Text = "adasd";
        }
        
        Untitled Page
        
        N 1 Reply Last reply
        0
        • V viivekk

          This is sample userctrl with textbox inside:

          ]]>

          This is aspx page i include the user ctrl in this page, i use findcontrol method of usercontrol object, by using this u can find controls inside the usercontrols.

          ]]>
          ]]>

          protected void btn\_Click(object sender, EventArgs e)
          {
              ((TextBox)usrctrl1.FindControl("TextBox1")).Text = "adasd";
          }
          
          Untitled Page
          
          N Offline
          N Offline
          nguyenhh
          wrote on last edited by
          #4

          viivekk, Thank you for pointing that out, what I was missing is: instead of using:

          TextBox searchValue = (TextBox)Page.FindControl("searchValue");

          I should use:

          TextBox searchValue = (TextBox)ControlFormBookSearch1.FindControl("searchValue");

          as ControlFormBookSearch1 is the control id i used in my code. Thanks again.

          Hai

          1 Reply Last reply
          0
          • S Sherin Iranimose

            Did you add the control to the page by using something like below

            nguyenhh wrote:

            protected void btnSearch_Click(object sender, EventArgs e) { TextBox searchValue = (TextBox)Page.FindControl("searchValue"); Control searchType = Page.FindControl("searchType"); //never find this control if (searchValue != null) Response.Write(searchValue.Text); else Response.Write("Bad request"); }

            This is not the way of using it. You should write some properties to your control, So that you can directly use your control properties. No need to find each control inside it.

            EVEN THE WORD IMPOSSIBLE SAYS I M POSSIBLE.

            N Offline
            N Offline
            nguyenhh
            wrote on last edited by
            #5

            Sherin Iranimose, I am learning .net, could you give me some example code to do this the correct way. my web form file:

            <%@ Import NameSpace="com.nangmoi" %>
            <%@ Import Namespace="System.Data" %>
            <%@ Import Namespace="System.Web" %>
            <%@ Page Language="C#" MasterPageFile="~/MasterPageHome.master" Title="www.nangmoi.com" %>

            <%@ Register Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            Namespace="System.Web.UI.WebControls" TagPrefix="asp" %>
            <%@ Register src="Control/ControlFormBookSearch.ascx" tagname="ControlFormBookSearch" tagprefix="uc6" %>

            <script runat="server">
            private BookManager objManager = new BookManager();
            protected void Page_Load(object sender, EventArgs e)
            {
            if (!IsPostBack){

                    GridView1.DataSource = objManager.getAuthorByAll();
                    GridView1.DataBind();                         
                }
            }
            
            protected void btnSearch\_Click(object sender, EventArgs e)
            {
                //TextBox searchValue = (TextBox)Page.FindControl("searchValue");
                TextBox searchValue = (TextBox)ControlFormBookSearch1.FindControl("searchValue");
                
                if (searchValue != null)
                    Response.Write(searchValue.Text);
                else Response.Write("Bad request");  
            }
            

            </script>
            <uc6:ControlFormBookSearch ID="ControlFormBookSearch1" runat="server" />  

            user control page

            <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ControlFormBookSearch.ascx.cs" Inherits="Control_ControlFormBookSearch" %>

            <table style="padding:0; border:solid 0px;">
            <tr valign="middle">
            <td style="vertical-align:top;border-bottom:solid 0px">
            Keyword: <asp:TextBox ID="searchValue" runat="server" value="test" /> 
            </td>
            </tr>
            </table>

            user control code behind:

            using System;
            using System.Collections;
            using System.Configuration;
            using System.Data;
            using System.Web;
            using System.Web.Security;
            using System.Web.UI;
            using System.Web.UI.HtmlControls;
            using System.Web.UI.WebControls;
            using System.Web.UI.WebControls.WebParts;

            public partial class Control_ControlFormBookSearch : System.Web.UI.UserControl
            {
            protected void Page_Load(object sender, EventArgs e)
            {

            }
            //How do i do this the correct way?? do i create properties like this?
            //then how do i use i
            
            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups