I was told to write the question here instead of in c# forum, so I post it here as well.

Hi..

I am trying to update a webcontrol on a main site from a usercontrol but how can this be done.

Let's say I want to update an asp:textbox from a usercontrol on the page, but how is this done most respectfully. The usercontrol does ofcause not know the controls from the main site but I guess that the "findcontrol"-method (in the usercontrol) executed on the parent-control must do what I need (as long that I check that the control exist on the main page); but how is this done.

Something like this maybe:

MAIN SITE:
<SOME TEXTBOX id="main">
<uc:USERCONTROL id="UC">

USERCONTROL codebehind somewhere:
((Textbox)This.parent???.findcontrol("main")).Text = "something from uc";

here is the example solution :
Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" 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>
        <uc1:WebUserControl ID="WebUserControl1" runat="server" />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></div>
    </form>
</body>
</html>

Default.aspx.cs :

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

    }
}

WebUserControl.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

WebUserControl.ascx.cs :

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

    }
	protected void Button1_Click(object sender, EventArgs e)
	{
		((TextBox)Parent.FindControl("TextBox1")).Text = "something";
	}
}

Thanks..

So my surgestion about "((TextBox)Parent.FindControl("TextBox1")).Text = "something";", as you also make use of, is the solution to do this? But the parent is rendered before the uc so this includes use of ajax I assume..?!

Thanks..

So my surgestion about "((TextBox)Parent.FindControl("TextBox1")).Text = "something";", as you also make use of, is the solution to do this? But the parent is rendered before the uc so this includes use of ajax I assume..?!

i never send a post without trying it locally on my PC. As long as it works, why do you care which one is rendered first? if you really wonder it, just put a break point to load event handlers of both usercontrol and webform.
no this example does not use ajax.

i never send a post without trying it locally on my PC. As long as it works, why do you care which one is rendered first? if you really wonder it, just put a break point to load event handlers of both usercontrol and webform.
no this example does not use ajax.

My question whether the parent.findcontrol was the way to go, and it seems that way. I'm not talking about the code exactly. That I was given this fine example of usage I thanks alot, but I did not expect such fine an example.
My question on which is rendered first wasn't a real question but more a comment on the main page is rendered first which mean that the control referered to is not changed without changing the main site which is already rendered and lead to another postback. Understand my point.

The exact question is that i need ajax to render the main page when the usercontrol is making a postback, but how is this done without flickering/postback all the page.
To update the textfield (main site) for example I need to surround this by an updatepanel and then the textbox-control can be updated without flicker, but how do I ensure that the usercontrol's runat server command to update the updatepanel is not updating the usercontrol as well and make postback on this as well? should I surround all the usercontrol-controls with an updatepanel?
I don't know if I am clear in my question. If not let me know.

CODE MAIN SITE:

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true">
    </asp:ScriptManager>
  
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
      <asp:Literal ID="LiteralMessages" runat="server"></asp:Literal>
      <br />
      <uc1:WebUserControl ID="WebUserControl1" runat="server" />

    </ContentTemplate>
  </asp:UpdatePanel>

CODE UC:

protected void cb_enabled_CheckedChanged(object sender, EventArgs e)
{
...
((Literal)Parent.FindControl("LiteralMessages")).Text += Msg.PrintMessages(MYDLL.GetMessages(Request, Session));	
...
}


<asp:Label ID="lblName" runat="server" Width="150px"></asp:Label>
<asp:CheckBox ID="cb_enabled" runat="server" 
  oncheckedchanged="cb_enabled_CheckedChanged" AutoPostBack="true" />

What I want is that the checkbox when changed sends a message back to the main site and not postback the entire site so it flickers.

And thank you very much for the first reply, you answered my question very well; and I should have been more precise in my question.

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.