hey..i am new to asp.net and i want to know how to render a partial page (.ascx) into .aspx page on a link click
+1
A:
Include the user control file in your ASPX page but set it to invisible:
<%@ Page Language="C#" %>
<%@ Register TagName="test" TagPrefix="asp" Src="~/Test.ascx" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:test runat="server" ID="test" Visible="false" />
</form>
</body>
</html>
Then put a link on your page and when this link is clicked set the visibility of the control to true in the click handler:
test.Visible = true;
And here's the whole example:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ToDD._Default" %>
<%@ Register TagName="test" TagPrefix="asp" Src="~/Test.ascx" %>
<script type="text/C#" runat="server">
protected void ShowClick(object sender, EventArgs e)
{
test.Visible = true;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:test runat="server" ID="test" Visible="false" />
<br/>
<asp:LinkButton ID="BtnShow" runat="server" Text="Show" OnClick="ShowClick" />
</div>
</form>
</body>
</html>
UPDATE:
As requested here's the same example using javascript:
<%@ Page Language="C#" %>
<%@ Register TagName="test" TagPrefix="asp" Src="~/Test.ascx" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript">
function show() {
document.getElementById('container').style.display='block';
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="container" style="display:none;">
<asp:test runat="server" ID="test" />
</div>
<br/>
<a href="#" onclick="show();">Show</a>
</div>
</form>
</body>
</html>
Darin Dimitrov
2009-11-26 07:16:45
thank you for the answer...but i wanted to use only JavaScriptcan you please provide the same with javascript
dexter
2009-11-26 13:05:40
after running i am getting this error System.Web.HttpException: Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster – the error came showing following line highlighted(i am using MVC) <% Html.RenderPartial("LogOnUserControl"); %>
dexter
2009-11-26 13:34:46
hey...thanx for the replay but theres one problem ...above code ..is rendering ascx page automatically ...i want it to be onclick ie manually can you pls help...i cut runat=server in <asp:test runat="server" ID="test" />but after that the 'show' lnk stopped workingpls help
dexter
2009-11-27 06:21:40