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
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>
精彩评论