开发者

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 开发者_如何学JAVAchoose 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.

E.g.

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜