My current scenario is I retrieved the HTML of a page using the following method.
readStream = New StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet))
Dim fullString As String = readStream.ReadToEnd()
I did further formatting of the HTML before setting it to the innerHtml of my div.
displaystring = Regex.Replace(fullString, "<div.*?>", "<div>")
displaystring = Regex.Replace(fullString, "<span.*?>", "<span>")
displaystring = Regex.Replace(fullString, "<table.*?>", "<table>")
For example, I have a part of HTML code which is
<a>Brazil</a>
And I have tried fullstring = Regex.Replace(fullstring, "<a>Brzail</a>", <asp:Linkbutton name="lnkTest" runat="server"). And I realized it cannot work because ASP controls cannot be appended as string.
How can I control.Add just after <a>Brazil</a>? Any other suggestions are welcome too.
You cannot add ASP.Net Server control like this. You can use html control anchor tag or button and on click of this you can make an AJAX call.
Sample code will be like this
fullstring = Regex.Replace(fullstring, "<a>Brzail</a>", "<a href='#' onclick='clickMe'>Link</a>")
In Server side C# you can write a page method
like `
[PageMethod]
public static string MyMethod()
{
//Your logic
return "";
}
`
In javascript
function cickMe()
{
$.ajax({
url: "Sample.aspx/MyMethod",
success: function(data){
//your logic
}
});
}
Related
I am using a jQuery editor and when the user hits the submit button i put the content into asp.net Panel control as html and then when i render this Panel the html i added is not
retrieved.
function MoveData() {
var sHTML = $('#summernote_1').code();
// dvFrontPageHtml is asp.net Panel
$('[id*=dvFrontPageHtml]').html(sHTML);
setTimeout(function () {
javascript: __doPostBack('ctl00$ctl00$ContentPlaceHolderBody$ContentPlaceHolderBody$lnkSave', '');
}, 10000);
return false;
}
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter stWriter = new System.IO.StringWriter(sb);
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(stWriter);
dvFrontPageHtml.RenderControl(htmlWriter);
string Message = sb.ToString();
The message does not returning the html added.
I dont want to use jQuery ajax call as of now.
Any suggestions
without seeing all the relevant code its hard to pinpoint the problem.
but im pretty sure you are trying to find an ASP.net control by its serverside ID from clientside.
dvFrontPageHtml is the Controls ID by which asp.net identifies it, and unless you explicitly tell ASP.Net otherwise, it will generate a different ID for the control to be used by scripts at clientside
you need to retrieve the panel's clientside ID thats being generated for it by asp.net
you do it by a preprocessor directive <%=dvFrontPageHtml.ClientID%>:
$('[id*=<%=dvFrontPageHtml.ClientID%>]').html(sHTML);
alternatively, if you want the clientside ID to be same as the serverside ID, you can set the control's attribute ClientIDMode="Static".
UPDATE:
from your comment it seems the problem is elsewhere. what comes to mind, is that RenderControl() takes the control as it was when sent to the client in the Response. but the control is not being submitted to the server in next Request, so you will not be able to retrieve its altered html.
what you can do as a workaround, is hook into ASP.NET's build in postback mechanism, and submit the panel's html as a custom event argument:
for the example, lets assume this is our html:
<asp:Panel ID="dvFrontPageHtml" runat="server" ClientIDMode="Static">test</asp:Panel>
<asp:Button ID="BT_Test" runat="server" Text="Button"></asp:Button>
this will be our javascript:
$(function(){
// add custom event handler for the submit button
$("#<%=BT_Test.ClientID%>").click(function (ev) {
//prevent the default behavior and stop it from submitting the form
ev.preventDefault();
//alter the panels html as you require
var sHTML = $('#summernote_1').code();
$('[id*=dvFrontPageHtml]').html(sHTML);
//cause a postback manually, with target = BTCLICK and argument = panel's html
__doPostBack('BTCLICK', $('[id*=dvFrontPageHtml]').outerHTML());
});
});
and here we capture the postback on serverside:
//we monitor page load
protected void Page_Load(object sender, EventArgs e)
{
string Message;
//check if its a postback
if (IsPostBack)
{
//monitor for our custom target "BTCLICK"
if (Request.Form["__EVENTTARGET"].CompareTo("BTCLICK") == 0)
{
// retrieve the panels html from the event argument
Message = Request.Form["__EVENTARGUMENT"];
}
}
}
my question is related to the fileupload control.I am loading my usercontrollers dynamically via ashx handler.so far so good.but when I put
<ext:FileUploadField runat="server" />
my ashx handler give me error which tell me null reference .
I am doing this with the text,textfield elements ,in that case, no problem ,my user control loading without no problems.
(<ext:TextField runat="server" FieldLabel="deneme"></ext:TextField>)
someone tell me where is the problem .
here is the my ashx handler,
public void ProcessRequest(HttpContext context)
{
string mainTabPnl = context.Request["container"];
string url = context.Request["url"];
string id = context.Request["id"];
Ext.Net.Panel pn = new Ext.Net.Panel()
{
Title = "MY TITLE",
Closable = true,
Layout = "Fit",
Height=500,
ID=id,
Items = {
new UserControlLoader{
Path=url
}
}
};
pn.AddTo(mainTabPnl);
new DirectResponse().Return();
}
ext:FileUploadField
should be placed inside html form tag
<form>
<ext:FileUploadField
</form>
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
Literal lTags = new Literal();
lTags.Text = "<meta name=\"MetaTagsDemo\" content=\"Meta demo tag\" />";
this.Header.Controls.Add(lTags);
}
I have the the above code in my default.aspx.cs. When Default.aspx page is loaded I do see the control getting added within section but on top of the page (very 1st line in the page), the html display is "".
What am I doing wrong here?
I have another page named Browse.aspx where I have the same feature but this page doesn't show the html output.
UPDATE
Source for meta tags is database and whole metatag is stored in one field
You should add meta tag as a html control, something like this :
HtmlMeta tag = new HtmlMeta();
tag.Name = "MetaTagsDemo";
tag.Content = "Meta demo tag";
Page.Header.Controls.Add(tag);
UPDATE:
Yes, you should parse database input, with HtmlAgilityPack this is a easy task, for example :
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml("<meta name=\"MetaTagsDemo\" content=\"Meta demo tag\" />");
foreach (HtmlNode meta in doc.DocumentNode.SelectNodes("//meta"))
{
string metaName = meta.Attributes["Name"].Value;
string content = meta.Attributes["Content"].Value;
/// do something
}
UPDATE 2
No HtmlAgilityPack, here is a quick and dirty method, you should test it
string meta = "<meta name=\"MetaTagsDemo\" content=\"Meta demo tag\" />";
string[] splitted = meta.Split('"');
string metaName = splitted[1];
string metaContent = splitted[3];
i need open pop up in asp.net using post Method and window.open to rezise te new windows.
my code:
Open the pop up:
function mdpbch(URL) {
child = window.open(URL, "passwd","dependent=1,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=600,height=475");
child.location.href = URL;
if (child.opener == null) {
child.opener = window;
}
child.opener.name = "opener";
}
URL:
function PagoEnLinea(Banco)
{
switch(x){
case "BCH":
document.frmEnvia.action = SERV + "/llamacom.asp";
url = SERV + "lamacom.asp
alert(url);
mdpbch(url);
document.frmEnvia.submit();
break;
}
}
ASPX:
<body>
<form id="frmEnvia" runat="server" name="formulario" method="post" target="_blank">
<div style="visibility:hidden;">
<asp:TextBox ID="txtXml" runat="server" Visible="true" />
</div>
.....
</body>
on page load (code behind) i create a xml string and put it in the textbox txtXml.
i need use post method becose the server validate te method, and window.open becose need customize the pop up
thanks
I think you should manipulate the object:
parentWindow
Inside the document object; before you submit the form, something like this:
switch(x){
case "BCH":
document.frmEnvia.action = SERV + "/llamacom.asp";
url = SERV + "lamacom.asp
alert(url);
mdpbch(url);
//here you manipulate the parentWindow element
document.parentWindow.scrollbars... (add the attributes you need)
//--
document.frmEnvia.submit();
break;
}
Since you are setting the name of the popup to passwd, you should then be able to set the target attribute of the form to passwd.
So the only change needed is the following:
<form ... target="passwd">
Is there any way through which I can get HTML of my current page. By current page I mean let's say I am working on Default.aspx and want to get HTML by providing a button on it.
How to get it.
EDITED in response to clarification of the requirements
You can override the page's render method to capture the HTML source on the server-side.
protected override void Render(HtmlTextWriter writer)
{
// setup a TextWriter to capture the markup
TextWriter tw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(tw);
// render the markup into our surrogate TextWriter
base.Render(htw);
// get the captured markup as a string
string pageSource = tw.ToString();
// render the markup into the output stream verbatim
writer.Write(pageSource);
// remove the viewstate field from the captured markup
string viewStateRemoved = Regex.Replace(pageSource,
"<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\".*?\" />",
"", RegexOptions.IgnoreCase);
// the page source, without the viewstate field, is in viewStateRemoved
// do what you like with it
}
Not sure why you want what you want, but... this is off the top of my head, i.e. I didn't try this code.
Add a client-side onclick to your button to show markup and do something like this:
function showMarkup() {
var markup = "<html>" + document.getElementsByTagName("html")[0].innerHTML + "</html>";
alert(markup); // You might want to show a div or some other element instead with the markup variable as the inner text because the alert might get cut off.
}
If you need this rendered markup posted back to the server for some reason, store the encoded markup in a hidden input and post that back. You can register the script below on the server-side using ClientScriptManager.RegisterOnSubmitStatement . Here's the cleint-side code.
var markup = escape("<html>" + document.getElementsByTagName("html")[0].innerHTML + "</html>");
var hiddenInput = $get('hiddenInputClientId');
if (hiddenInput) {
hiddenInput.value = markup;
}
Hope that helps,
Nick
I'm still not sure what your objective is with this. But if you want the total rendered output of the page then your probably better of looking at some client side code as this would be run once the server has returned the fully rendered HTML.
Otherwise you could proably catch the page unload event and do something with the rendered content there.
More info needed on what you want from this.