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>
You can have it in a hiddenfield in asp.net and change the hidden field in code behind.
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%>
Related
I want to set HTML inside a div in my code, but the end result looks like this:
Here is my code:
protected void grdTags_SelectedIndexChanged(object sender, EventArgs e)
{
string RelatedText = grdTags.SelectedRow.Cells[5].Text;
dv.InnerHtml = RelatedText;
}
Here is the ASPX:
<div id="dv" runat="server">
</div>
It looks like you're passing in text into HTML. Consider using HttpUtility.HtmlEncode()
I've not tested this, but try:
dv.InnerHtml = HttpUtility.HtmlDecode(grdTags.SelectedRow.Cells[5].Text);
I am developing an ASP.NET web application. in which have a home page,
when we press F2 in home page we need to load item Master page &
when press F4 in home page we need to load city Master page.
I don't know how to open these pages in ASP.NET C# using short cut keys..
Can any one help me?
write a javascript code onkeypress
function keypress() {
switch (event.keyCode) {
case ascii of f2:
//redirect/or load item master
}
}
or
<script type="text/javascript">
document.attachEvent('onkeyup', KeysShortcut);
// Now implement the KeysShortcut
function KeysShortcut ()
{
if (event.keyCode == 49)
{
document.getElementById('<%= button1.ClientID %>').click();
}
}
</script>
Using linkbutton Example...
C#
Note : The javascript, depending on the keycode, invokes the click event of the element, in our case the link button. The element is obtained by using document.getElementById(' '<%=Control.ClientID%>'').
Note: While using Master Pages, you need to refer to the control using the control's ClientID. I have directly used the ID generated.
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript" language="javascript" src="shortcut.js"> </script>
</head>
Page 1
Add a javascript file to the project called shortcut.js. Add the following code to the javascript file.
function HomeKey()
{
if(event.keyCode == 72)
{
document.getElementById('ctl00_ContentPlaceHolder1_btnHome').click();
}
}
now on backside
C#
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "Home", "document.attachEvent('onkeyup',HomeKey);", true);
}
Please consider the following:
code behind:
public void InitiateTable()
{
Controls.Add(new LiteralControl("<table class=\"table table-invoice\" >")); //Line that gave me error
...
Controls.Add(new LiteralControl("</table>"));
}
on my ASP.Net page:
<% InitiateTable(); %>
When I run the code, it gives me an error saying:
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
Can someone please explain what I have done wrong?
Thank you.
Note Changed LiteralControl to Control as suggested in comment.
You cannot modify the controls collection where the container contains <% %> code blocks as the error message tells you. Controls.Add will modify the controls collection. To get around this:
In your aspx page change
<% InitiateTable(); %>
to
<asp:Literal runat="server" id="mytable" />
then in your page load method, add a call to InitiateTable()
then in your InitiateTable() method change your code to something like this:
public void InitiateTable()
{
var literalControlValue = new StringBuilder();
literalControlValue.Append("<table class=\"table table-invoice\" >");
...
literalControlValue.Append("</thead>"));
mytable.Text = literalControlValue.ToString();
}
Controls.Add(new LiteralControl("<table class='table table-invoice'>"));
The issue is when you use Controls you are referencing the Page itself rather than the area you call InitiateTable.
If you're inserting controls into this area, this is what a Placeholder is for:
<asp:Placeholder id="phTable"></Placeholder>
Then in your Page_Load or some other event code:
private void Page_Load(object sender, System.EventArgs e)
{
phTable.Controls.Add(new LiteralControl("<table class=\"table table-invoice\" >"));
phTable.Add(new LiteralControl("</thead>"));
}
Let's say I have the following page:
<html>
<body>
<content1>
</content1>
<content2>
</content2>
<content3>
</content3>
</body>
</html>
I want in my Metro app to display only the part of the page contained between <content2></content2>. For a full page I would use a <WebView> and the Navigate() method. But I don't seem to find a way to adapt that to what I need.
to actually write any solution code I would need to know what are Content1 and content3. assuming them being DIV with an ID, I can say
void WebView5_LoadCompleted(object sender, NavigationEventArgs e)
{
string script = #"var d=document.getElementById('content1');d.style.visibility='hidden'";
string[] args = { script };
string foo = WebView5.InvokeScript("eval", args);
}`
I have a web application in which I want to track two different views of the same page from google analytic.
From code behind I am managing the two different views..but didn't find the way to manage the below script from code behind.
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("XX-XXXXXX-XX");
pageTracker._setDomainName(".DOMAIN.com");
pageTracker._trackPageview();
} catch (err) { }
</script>
So how can I change this script dynamically from code behind...?
A hack will be to place two hidden fields in the aspx part
<asp:HiddenField ID="TrackerCode" runat="server" ClientIDMode="Static">
</asp:HiddenField>
<asp:HiddenField ID="DomainName" runat="server" ClientIDMode="Static">
</asp:HiddenField>
Then at page load assign them
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TrackerCode.Text = "XX-XXXXXX-XX";
DomainName.Text = ".DOMAIN.com";
}
}
And finally place your script at the bottom of the page. ( After the labels we created )
<script type="text/javascript">
try {
var tcode = document.getElementById("TrackerCode").value;
var domain = document.getElementById("DomainName").value;
var pageTracker = _gat._getTracker( tcode );
pageTracker._setDomainName( domain );
pageTracker._trackPageview();
} catch (err) { }
</script>
Declare one function and define all your logic inside this function.
And call that function when the DOM is ready or on window.onload.
window.onload=function(){
//your function called it here or right your code here
}
Hope it will work