Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
I have a .ascx user control that contains a button. I place this user control on a .aspx web page. The web page contains a textbox. When I click on the button in the user control I want the data in the textbox (in the web page) be converted to string on button_click(inside user control). Ive tried using this code but it just give me error as subject above
VB
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim DaTxtBox As TextBox = Me.Parent.FindControl("textbox1")
dim stringme as string =DaTxtBox.text
'tried also this one but it has the same effect
        Dim lbl As TextBox = DirectCast(Parent.FindControl("textbox1"), TextBox)
    End Sub

How can I access control from the aspx page to user control
Posted
Comments
Varun Sareen 13-Feb-12 1:27am    
Have you registered he User control on the page? I think the Text box user control is not being found on the page that's why the error "Object reference not set to an instance of an object." is coming.
janwel 13-Feb-12 2:41am    
ive registered it <%@ Reference Control="~/CourseUserControl.ascx" %> and bind it manually using codes

 
Share this answer
 
Comments
janwel 13-Feb-12 1:43am    
that forum gave me an Idea
[Something is going wrong on the server: I deleted my answer because it looked like it was posted twice, after refresh the page I found it was posted only once and deleted; when I post again all my new posts appear to be deleted.]

Did you try to run it under Debugger? You would find out the problem faster than you used to type this question.

First possible reason: your Parent is null; the exception is thrown when you use the value of this property; you de-reference it trying to call FindControl.

Second possible reason: your FindControl returns null — control is not found. Later on, you try to de-reference DaTxtBox trying to call its Text property.

That's all.

—SA
 
Share this answer
 
Comments
janwel 13-Feb-12 2:27am    
ive tried using debugger and it seems findcontrol was the error.
Sergey Alexandrovich Kryukov 17-Feb-12 13:01pm    
This is one of the very usual cases.
I seems that now I could accept this answer formally, could you (green button) -- thanks.
--SA
HI Friend,

you can create a eventhandler in your user control. and use use that event into your parent control.

So when you click on you button that will fire the parent page event.

For Example:

on my aspx page code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="~/userControl.ascx" TagName="UC1" TagPrefix="UC" %>
<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="txt" runat="server"></asp:TextBox>
    </div>
    <div><UC:UC1 ID="uc1"  runat="server"  önPageTitleUpdated="UC1_PageTitleUpdated" /></div>
    </form>
</body>
</html>


my aspx.Cs file code is following.

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

    }

    public void UC1_PageTitleUpdated(object sender, EventArgs e)
    { 
    
        // This is control event do what ever you want to do
    
    }

}


Now Ascx, user control code

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="userControl.ascx.cs" Inherits="userControl" %>
<asp:Button ID="submit" runat=server  Text="Submit" onclick="submit_Click"/>


and Ascx.vb file code

public partial class userControl : System.Web.UI.UserControl
{
    // Define your custom event
    public event EventHandler PageTitleUpdated;


    protected void Page_Load(object sender, EventArgs e)
    {
       
    }


    protected void submit_Click(object sender, EventArgs e)
    {
    
        // On button click event fire parent page title updated event
            if (PageTitleUpdated != null)
                PageTitleUpdated(sender, e);
        
    }
}



Hope you understood let me know is this solve your problem.
 
Share this answer
 
v2
Comments
janwel 13-Feb-12 1:42am    
can I see your sample sir? Actually what I am thinking right now is create a property on my user control that will get the data on aspx page.
Nigam Patel 13-Feb-12 2:02am    
i updated the solution please check it. and let me know the feed back
janwel 13-Feb-12 2:29am    
your code works sir but the problem is I am using master page. But thanks anyway ive manage tosolve it using java script. But your code works well.. Thanks a lot sir
Instead of trying Me.Parent, try
Dim myParent as Page = this.Page;
Dim DaTxtBox As TextBox = myParent.FindControl("textbox1")
 
Share this answer
 
Comments
janwel 13-Feb-12 1:27am    
Sir it is the same as above. It is nothing. Even if I change some of your code cause it is C

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900