I have recently upgraded my application to .net 4.0. I have a problem with the hyperlink navigateurl relative paths being rendered properly. I am calling a webmethod through jquery which loads a usercontrol and get the html which I display it on the page. The usercontrol has a gridview with a hyperlink field which is bound to some application relative paths using tilde(~).
For replicating this issue I have created a website say "TestWebsite" under "Default Website" in IIS. I have a page "Test.aspx" under the root folder. Also I have UserControls folder which has my usercontrol "TestUserControl.ascx". Now I just call my webmethod in the page which will load the control and return the html. The issue I am having is the Hyperlink url relative path is rendered incorrectly. It displays
http://localhost/SubFolder/Sample1.aspx instead of starting with my root folder (TestWebsite)
http://localhost/TestWebsite/SubFolder/Sample1.aspx .
Here is the sample code
Test.aspx
<div>
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "POST",
url: "Test.aspx/TestMethod",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$("#result").html(msg.d);
}
});
});
</script>
</div>
<div id="result"></div>
Test.aspx.cs - webmethod
[WebMethod]
public static string TestMethod()
{
StringWriter tw = new StringWriter();
Control control = null;
Page page = new Page();
try
{
control = page.LoadControl("~/UserControls/TestUserControl.ascx");
}
catch (Exception ex)
{
string name = ex.Message;
}
HtmlForm form = new HtmlForm();
form.ID = "_form";
page.Controls.Add(form);
form.Controls.Add(control);
page.Controls.Add(form);
HttpContext.Current.Server.Execute(page, tw, true);
string html = tw.ToString();
return html;
}
UserControl
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("TestLink");
DataRow dr = dt.NewRow();
dr["TestLink"] = "~/SubFolder/Sample1.aspx";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["TestLink"] = "~/SubFolder/Sample2.aspx";
dt.Rows.Add(dr);
GVTest.DataSource = dt;
GVTest.DataBind();
}
TestUserControl.ascx
Iam not able to copy my code here due to formatting issues but I have a simple gridview here with a hyperlink server control where I bind the navigateurl.
After some search on the web I found this similar issue at
Asp.Net single control render for AJAX calls
where it was mentioned to set AppRelativeTemplateSourceDirectory. but even after setting this to AppDomainAppVirtualPath, it doesn't work for me.
After Server.Execute the relative url (~/SubFolder/) changes to one level up giving (../SubFolder/)
I am not sure if I am missing anything? I would appreciate if any one could help me on resolving this issue.
FYI: I am using IIS 7.5 and windows 7.
Thanks, Prashant