简体   繁体   中英

Change html code on server side asp.net

I want to change some string located in html file when it loads. For example, i have a html file:

<html>
<head>
<title>MyTitle</title></head>
<body>
Some Text
<script type='text/javascript'>
/*some script*/
var myString = "TargerInfo";
/*some script*/
</script>
</body>
</html>

I use Page_Load method in code-behind file:

protected void Page_Load(object sender, EventArgs e)
{
/*Insert necessary snippet of code*/
}

What code should i use to change string "TargerInfo" to "OtherString" ?

[EDIT] Sorry, that I have forgot to mention I can add any info to html page only in code-behind class, because this page isn't generated by me. I think i should use something like this:

1) load html file

2) find my string

3) replace it

4) send html file

There is an aspx page, but i add only some part of code and other code is added by VS

Unless I'm missing something (because this seems like a bit of an ASP.NET 101), you have several options...

Create a variable in the code-behind and then use that...

protected string _newText = "";
protected void Page_Load(object sender, EventArgs e)
{
   _newText = "OtherString";
}

And then in the ASPX...

var myString = "<%=_newText%>";

Otherwise you can use the <asp:Literal> control

UPDATE

After an extensive chat with @andDaviD it turns out that the javascript is in a Master page held in SharePoint Foundation.

The Master page is being referenced in his Content page via the DynamicMasterPageFile attribute in the <%@ Page directive, and that is why he said he is able to update some part of the code, but not others.

I am still unsure as to whether it is possible for the Master page to be modified (either by himself or an administrator), that is something he needs to find out from the people in charge at his company. But I believe the adding of a property or method to the Master Page to provide what he needs is the only sensible option.

You could use inline aspx code tags:

<script type='text/javascript'>
/*some script*/
var myString = "<%= getTargetInfo() %>";
/*some script*/
</script>

in codebehind:

protected String getTargetInfo()
{
    return "OtherString";
}

You could use a literal:

protected void Page_Load(object sender, EventArgs e)
{
     literal.Text = string.Format("var myString = \"{0}\"", targetInfoValue);
}

Markup:

<html>
<head>
<title>MyTitle</title></head>
<body>
Some Text
<script type='text/javascript'>
/*some script*/
<asp:Literal id="literal" runat="server" />
/*some script*/
</script>
</body>
</html>

您可以将其放在asp.net的hiddenfield中,并在后面的代码中更改hidden域。

in your code behind:

public string otherString;


otherString = "some text"  //update the string with the value oyu want.

in aspx page put this line in any place you want to see the otherString.

<%=otherString%>

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