Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jul 27, 2007 06:58 AM by Jin-Yu Yin - MSFT
Member
94 Points
25 Posts
Jul 23, 2007 03:32 PM|LINK
Hi,
I have 2 web user controls in same master page.
And in each user control there is datalist.
In first user control's datalist there is link button.
What I'm trying to do that is
when I click linkbutton in first user control, update the second datalist in second user control with Update Panel.
What is the easiest way to add postback trigger to another control in another user control.
Thanks in advance.
All-Star
32101 Points
3764 Posts
Jul 27, 2007 06:37 AM|LINK
In my opinion, it's not a good practice to have one UserControl rely on another UserControl, unless the second one is placed inside the first one.
Any way, you may try the following way:
1. Register the second UC as the trigger programmatically in PageLoad of the first UC:
Page_Load() {
Control c = this.Page.FindControl("id of the second UC");
ScriptManager.GetCurrent(this.Page).RegisterAsyncPostBackControl(c);
}
2. Call Update method explicitly on the UpdatePanel in the second UC's linkButton click event handler:
click()
{
UpdatePanel up = this.Page.FindControl("id of the first UC").FindControl("id of the updatePanel") as UpdatePanel;
up.Update();
Hope this helps although personally I'm against this approach.
21280 Points
1824 Posts
Jul 27, 2007 06:58 AM|LINK
Hi junkyP,
In order to do that, we can do the following stepes :
1. Define a custom property to store the client ID of a hidden trigger control of the Update Panel(Button1clientID property of Gallery User Control): <%@ Control Language="C#" ClassName="Gallery" %> <script runat="server"> protected void Button1_Click(object sender, EventArgs e) { Label1.Text = DateTime.Now.ToString(); } public string Button1clientID { get { return Button1.ClientID; } } </script> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <div style="visibility: hidden"> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" UseSubmitBehavior="false" Text="Button" /></div> </ContentTemplate> </asp:UpdatePanel> 2. Define a custom property in the other User Control(Button1clientID property of Upload User Control): <%@ Control Language="C#" ClassName="Upload" %> <script runat="server"> private string buttonclientID = ""; public string Button1clientID { set { buttonclientID = value; } } </script> 3. Set Upload1.Button1clientID = Gallery1.Button1clientID in the page_Load: <script runat="server"> protected void Page_Load(object sender, EventArgs e) { Upload1.Button1clientID = Gallery1.Button1clientID; } </script> 4. Fire the click event of the hidden trigger control manually: <script type="text/javascript"> function submitForm() { document.getElementById("<%=buttonclientID %>").click(); } </script> <button onclick="javascript:submitForm();">Upload</button>
1. Define a custom property to store the client ID of a hidden trigger control of the Update Panel(Button1clientID property of Gallery User Control):
<%@ Control Language="C#" ClassName="Gallery" %> <script runat="server"> protected void Button1_Click(object sender, EventArgs e) { Label1.Text = DateTime.Now.ToString(); } public string Button1clientID { get { return Button1.ClientID; } } </script> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <div style="visibility: hidden"> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" UseSubmitBehavior="false" Text="Button" /></div> </ContentTemplate> </asp:UpdatePanel>
<%@ Control Language="C#" ClassName="Gallery" %> <script runat="server"> protected void Button1_Click(object sender, EventArgs e) { Label1.Text = DateTime.Now.ToString(); } public string Button1clientID { get { return Button1.ClientID; } } </script>
<asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <div style="visibility: hidden"> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" UseSubmitBehavior="false" Text="Button" /></div> </ContentTemplate> </asp:UpdatePanel>
2. Define a custom property in the other User Control(Button1clientID property of Upload User Control):
<%@ Control Language="C#" ClassName="Upload" %> <script runat="server"> private string buttonclientID = ""; public string Button1clientID { set { buttonclientID = value; } } </script>
3. Set Upload1.Button1clientID = Gallery1.Button1clientID in the page_Load:
<script runat="server"> protected void Page_Load(object sender, EventArgs e) { Upload1.Button1clientID = Gallery1.Button1clientID; } </script>
4. Fire the click event of the hidden trigger control manually:
<script type="text/javascript"> function submitForm() { document.getElementById("<%=buttonclientID %>").click(); } </script> <button onclick="javascript:submitForm();">Upload</button>
<script type="text/javascript"> function submitForm() { document.getElementById("<%=buttonclientID %>").click(); } </script>
Here is the full sample code:
http://forums.asp.net/p/1117746/1739422.aspx#1739422
http://forums.asp.net/t/1117746.aspx (Trigger PostBack to an UpdatePanel from a separate UserControl?)
Let me know if you need more info.
Best Regards
junkyP
Member
94 Points
25 Posts
AsyncPostBackTrigger from another user control
Jul 23, 2007 03:32 PM|LINK
Hi,
I have 2 web user controls in same master page.
And in each user control there is datalist.
In first user control's datalist there is link button.
What I'm trying to do that is
when I click linkbutton in first user control, update the second datalist in second user control with Update Panel.
What is the easiest way to add postback trigger to another control in another user control.
Thanks in advance.
Raymond Wen ...
All-Star
32101 Points
3764 Posts
Re: AsyncPostBackTrigger from another user control
Jul 27, 2007 06:37 AM|LINK
Hi,
In my opinion, it's not a good practice to have one UserControl rely on another UserControl, unless the second one is placed inside the first one.
Any way, you may try the following way:
1. Register the second UC as the trigger programmatically in PageLoad of the first UC:
Page_Load()
{
Control c = this.Page.FindControl("id of the second UC");
ScriptManager.GetCurrent(this.Page).RegisterAsyncPostBackControl(c);
}
2. Call Update method explicitly on the UpdatePanel in the second UC's linkButton click event handler:
click()
{
UpdatePanel up = this.Page.FindControl("id of the first UC").FindControl("id of the updatePanel") as UpdatePanel;
up.Update();
}
Hope this helps although personally I'm against this approach.
Jin-Yu Yin -...
All-Star
21280 Points
1824 Posts
Re: AsyncPostBackTrigger from another user control
Jul 27, 2007 06:58 AM|LINK
Hi junkyP,
In order to do that, we can do the following stepes :
Here is the full sample code:
Related threads:Let me know if you need more info.
Best Regards
Jin-Yu Yin
Microsoft Online Community Support