Hello sir,

I have problem in getting values of textbox which in .ascx file (user control). at aspx file(the file at which control is added).
code is as below.
code in .ascx file

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="attachfile.ascx.cs" Inherits="attachfile" %>
 <asp:TextBox ID="TextBox1" runat="server" meta:resourcekey="TextBox1Resource1"></asp:TextBox>
 <br />

code in .ascx.cs file

using System;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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;
using System.Xml.Linq;

public partial class attachfile : System.Web.UI.UserControl
{
       public string topicTital;
        protected void Page_Load(object sender, EventArgs e)
        {
        }
}

code in aspx file

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="attachfileindir.aspx.cs" Inherits="attachfile" %>
 <%@ Register src="attachfile.ascx" tagname="attachfile" tagprefix="uc1" %>
 <head></head>

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 <html>
 <body>
 <form id="form1" runat="server"> 
  <br />
<div>
      <br />
      <uc1:attachfile ID="attachfile1" runat="server" />
      <br />
      <br />
      
  </div>
  <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
      
 </form>
  </body>
 </html>

code in aspx.cs file

public partial class attachfile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
     // i want to strore that textbox value here..so how.....?     
  }

plz help someone.. .

Recommended Answers

All 8 Replies

public property in code in .ascx.cs file,

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

public property in code in .ascx.cs file,

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

I have wrritten that but it return an error .
Code for ctext.ascx.cs

public string textvalue
    {
        get
        {
            return TextBox1.Text;
        }
        set
        { 
         TextBox1.Text = value; 
        }
    }

code for temp.aspx.cs
on button click..

ctext1 uc =Page.LoadControl("ctext.ascx");
string x = uc.textvalue;
Response.Write(x);

where ctext1 is id for control ctext
and error is...
'temp1.ctext1' is a 'field' but is used like a 'type'
plz help ....sir

>Use @register directive to register user control.
>LoadControl() method return Control type so you need to do typecast.

ctext1 uc =(ctext)Page.LoadControl("ctext.ascx");
uc.Text="Some_text";

now the error comming is:
'temp1.ctext1' is a 'field' but is used like a 'type'
also i have registered this control on aspx page.
if U have any other way to get textbox value of .ascx page into the .aspx page then plz suggest me,
thanks Mr. adatapost to reply and become helpful to me..

>if U have any other way to get textbox value of .ascx page into the .aspx page then

Unfortunately there is no other way. Take a look,

attachfile.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="attachfile.ascx.cs" Inherits="attachfile" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

attachfile.ascx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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;
using System.Xml.Linq;

public partial class attachfile : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    public string Text
    {
        get { return TextBox1.Text; }
        set { TextBox1.Text = value; }
    }
}

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register src="attachfile.ascx" tagname="attachfile" tagprefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Configuration;
using System.Data;
using System.Linq;
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;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //UserControlClassname var=(UserControlClassName)LoadControl("...");
        attachfile uc = (attachfile)LoadControl("~/attachfile.ascx");
        uc.Text = "Hello";
        form1.Controls.Add(uc);
    }
}

>if U have any other way to get textbox value of .ascx page into the .aspx page then

Unfortunately there is no other way. Take a look,

attachfile.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="attachfile.ascx.cs" Inherits="attachfile" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

attachfile.ascx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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;
using System.Xml.Linq;

public partial class attachfile : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    public string Text
    {
        get { return TextBox1.Text; }
        set { TextBox1.Text = value; }
    }
}

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register src="attachfile.ascx" tagname="attachfile" tagprefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Configuration;
using System.Data;
using System.Linq;
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;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //UserControlClassname var=(UserControlClassName)LoadControl("...");
        attachfile uc = (attachfile)LoadControl("~/attachfile.ascx");
        uc.Text = "Hello";
        form1.Controls.Add(uc);
    }
}

IN Default.aspx U have missed to add attachfile.ascx.
and Sir,this exam is going to insert values in text box into usercontrol.
but i am waiting to get value from textbox(attachfile.ascx) into the variable (Default.aspx)..
OK. let me explain what i have done.
I have create two usercotrol -one has textbox and 2nd has checkboxes.
now i have added this both into the Default.aspx file.
and on button click at Default.aspx i am trying to store the userintput(text in textbox and checkbox value(select/noselect)
into variable v1 and v2 ,and finally i want to store this value into database.....
i hope U will be able to understad my problem.
bcz of i have never before used usercontrol ,i am failing to get simple thing....
.

>IN Default.aspx U have missed to add attachfile.ascx.

No. Check the Default.aspx code at post #6.

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
[B]<%@ Register src="attachfile.ascx" tagname="attachfile" tagprefix="uc1" %>[/B]
.....

>,this exam is going to insert values in text box into usercontrol. ..

Then you should consider switching your major.

thank you, sir...

>IN Default.aspx U have missed to add attachfile.ascx.

No. Check the Default.aspx code at post #6.

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
[B]<%@ Register src="attachfile.ascx" tagname="attachfile" tagprefix="uc1" %>[/B]
.....

>,this exam is going to insert values in text box into usercontrol. ..

Then you should consider switching your major.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.