Update website control from usercontrol?

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 28
Reputation: sniper1983 is an unknown quantity at this point 
Solved Threads: 0
sniper1983 sniper1983 is offline Offline
Light Poster

Update website control from usercontrol?

 
0
  #1
May 15th, 2009
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";
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: Update website control from usercontrol?

 
1
  #2
May 15th, 2009
here is the example solution :
Default.aspx:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2.  
  3. <%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>
  4.  
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6.  
  7. <html xmlns="http://www.w3.org/1999/xhtml" >
  8. <head runat="server">
  9. <title>Untitled Page</title>
  10. </head>
  11. <body>
  12. <form id="form1" runat="server">
  13. <div>
  14. <uc1:WebUserControl ID="WebUserControl1" runat="server" />
  15. <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></div>
  16. </form>
  17. </body>
  18. </html>
Default.aspx.cs :
  1. using System;
  2. public partial class _Default : System.Web.UI.Page
  3. {
  4. protected void Page_Load(object sender, EventArgs e)
  5. {
  6.  
  7. }
  8. }

WebUserControl.ascx:
  1. <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
  2. <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
WebUserControl.ascx.cs :
  1. using System;
  2. using System.Web;
  3. using System.Web.UI;
  4. using System.Web.UI.WebControls;
  5. public partial class WebUserControl : System.Web.UI.UserControl
  6. {
  7. protected void Page_Load(object sender, EventArgs e)
  8. {
  9.  
  10. }
  11. protected void Button1_Click(object sender, EventArgs e)
  12. {
  13. ((TextBox)Parent.FindControl("TextBox1")).Text = "something";
  14. }
  15. }
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 28
Reputation: sniper1983 is an unknown quantity at this point 
Solved Threads: 0
sniper1983 sniper1983 is offline Offline
Light Poster

Re: Update website control from usercontrol?

 
0
  #3
May 15th, 2009
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..?!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: Update website control from usercontrol?

 
0
  #4
May 15th, 2009
Originally Posted by sniper1983 View Post
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.
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 28
Reputation: sniper1983 is an unknown quantity at this point 
Solved Threads: 0
sniper1983 sniper1983 is offline Offline
Light Poster

Re: Update website control from usercontrol?

 
0
  #5
May 15th, 2009
Originally Posted by serkan sendur View Post
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:
  1.  
  2.  
  3. <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true">
  4. </asp:ScriptManager>
  5.  
  6. <asp:UpdatePanel ID="UpdatePanel1" runat="server">
  7. <ContentTemplate>
  8. <asp:Literal ID="LiteralMessages" runat="server"></asp:Literal>
  9. <br />
  10. <uc1:WebUserControl ID="WebUserControl1" runat="server" />
  11.  
  12. </ContentTemplate>
  13. </asp:UpdatePanel>

CODE UC:
  1. protected void cb_enabled_CheckedChanged(object sender, EventArgs e)
  2. {
  3. ...
  4. ((Literal)Parent.FindControl("LiteralMessages")).Text += Msg.PrintMessages(MYDLL.GetMessages(Request, Session));
  5. ...
  6. }
  7.  
  8.  
  9. <asp:Label ID="lblName" runat="server" Width="150px"></asp:Label>
  10. <asp:CheckBox ID="cb_enabled" runat="server"
  11. 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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 28
Reputation: sniper1983 is an unknown quantity at this point 
Solved Threads: 0
sniper1983 sniper1983 is offline Offline
Light Poster

Re: Update website control from usercontrol?

 
0
  #6
May 15th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC