-1

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

andDaviD
  • 555
  • 1
  • 11
  • 26
  • 1
    Is this an aspx page? Then why not simply add the relevant <% %> tags in the aspx file? – Panagiotis Kanavos Jun 21 '12 at 12:50
  • I can't do it , because html page isn't generated by me. – andDaviD Jun 21 '12 at 13:07
  • @andDaviD you should have mentioned that right at the start - a lot of people have spent their time on the assumption that you had control over the page – freefaller Jun 21 '12 at 13:10
  • There is an aspx page, but i add only some part of code and other code is added by VS – andDaviD Jun 21 '12 at 13:22
  • @andDaviD you're still not making sense. From what I can gather, the ASPX file is locked and you are not able to edit it in any way, but you have "some" control over the code-behind file for the referenced class? Can you clarify exactly **what** control you have, as "some part of code" makes no sense at all? – freefaller Jun 21 '12 at 13:24
  • @freefaller, Ok, I have added EntityEditorWithPicker control to my aspx page. And when I deploy this page on my server, it is added other code in my html page. But i don't know where this code is generated. I only have written such code in my aspx page: – andDaviD Jun 21 '12 at 13:27
  • @andDaviD Just for my curiosity, are you actually looking at the "Design" part of the ASPX page in Visual Studio? If so, have you tried clicking on the "Source" button at the bottom of the ASPX designer screen (where it should have "Design | Split | Source")? – freefaller Jun 21 '12 at 13:33
  • @freefaller, I double click on the apsx file and then add my code. – andDaviD Jun 21 '12 at 13:39
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12860/discussion-between-freefaller-and-anddavid) – freefaller Jun 21 '12 at 13:44
  • `But i don't know where this code is generated. I only have written such code in my aspx page: ` Are you talking about the HTML generated by the ASP controls once processed? Could you post the markup for the whole page? Perhaps you have a masterpage tied to it adding in some more markup that you can't immediately see... – dtsg Jun 21 '12 at 13:45
  • 1
    @Duane and others - please see my **updated** answer below for an explanation of exactly what the situation was – freefaller Jun 21 '12 at 14:40
  • @andDaviD Did you manage to get the situation sorted? Would love to hear any updates – freefaller Jun 22 '12 at 10:17
  • @freefaller, I have solved my problem. I found an answer [here](http://stackoverflow.com/questions/531631/get-html-of-current-page-without-viewstate-asp-net). Look at the answer of LukeH. – andDaviD Jun 25 '12 at 08:07

5 Answers5

2

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.

Community
  • 1
  • 1
freefaller
  • 19,368
  • 7
  • 57
  • 87
2

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";
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • I can't do it , because html page isn't generated by me. – andDaviD Jun 21 '12 at 13:06
  • @andDaviD: What does that mean, is this not an aspx page? Then there's also no codebehind. – Tim Schmelter Jun 21 '12 at 13:11
  • There is an aspx page, but i add only some part of code and other code is added by VS. – andDaviD Jun 21 '12 at 13:15
  • @andDaviD: It conveys no sense to me, you say that your IDE is preventing you from adding code to the aspx page? – Tim Schmelter Jun 21 '12 at 13:22
  • Ok, I have added EntityEditorWithPicker control to my aspx page. And when I deploy this page on my server, it is added other code in my html page. But i don't know where this code is generated. I only have write such code in my aspx page: – andDaviD Jun 21 '12 at 13:26
1

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>
dtsg
  • 4,408
  • 4
  • 30
  • 40
1

You can have it in a hiddenfield in asp.net and change the hidden field in code behind.

Rajesh Subramanian
  • 6,400
  • 5
  • 29
  • 42
  • You mean like what you saw here http://www.codeproject.com/Questions/380472/Access-string-value-in-javascript-from-backend-pag ? – Ruel Jun 21 '12 at 12:52
0

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%>
Ozgur Dogus
  • 911
  • 3
  • 14
  • 38
  • I can't do it , because html page isn't generated by me. – andDaviD Jun 21 '12 at 13:07
  • can you explain a bit more please . you write the code behind but not the html ?!im confused – Ozgur Dogus Jun 21 '12 at 13:13
  • @ Ozgur Dogus, Ok, I have added EntityEditorWithPicker control to my aspx page. And when I deploy this page on my server, it is added other code in my html page. But i don't know where this code is generated. I only have write such code in my aspx page: – andDaviD Jun 21 '12 at 13:27