简体   繁体   中英

ASP.NET: HtmlGenericControl <DIV> - refresh

I have a DIV element:

<div runat="server" id="path">Nothing here... yet</div>

and JavaScript which changes its content dynamically. After some actions my element looks like this (tested with Firebug, JS is ok):

<div runat="server" id="path">FirstTest - SecondTest - ThirdTest</div>

Then I'd like to save it to text file ( <asp:Button runat="server"... ):

<script runat="server">

void Page_Load(Object sender, EventArgs e)
{
    Button1.Click += new EventHandler(this.GreetingBtn_Click);
}

void GreetingBtn_Click(Object sender, EventArgs e)
{
    HtmlGenericControl path = (HtmlGenericControl)Page.FindControl("path");

    Response.AddHeader("content-disposition", "attachment;filename=download.txt");
    Response.ContentType = "text/plain";
    Response.Write(path.InnerText);
    Response.Flush();
    Response.Clear();       
    Response.End();
}

</script>

It also works OK (SaveDialog popups, user choose location), but... in output file there's only one line "Nothing here... yet". It looks like he doesn't react to changes made by JavaScript!

How can I force him to refresh DIV , so I can always save up-to-date content?

Thanks for any help!

You could update an asp:Hidden with the new value and use that value instead on the post back. The PlaceHolder control is not designed to be a two-way control.

Eg

function UpdateText()
{
    var text = ...;    

    document.getElementById("<%= path.ClientID %>").innerText = text;
    document.getElementById("<%= pathHidden.ClientID %>").value = text;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM