I have a ASP.NET 4 page and I am using tinyMce.
My aim is to change toolbar setting for tinyMce depending on "Role" associated to logged User.
To solve it my idea is to include different version of tinyMce JavaScript code using some logic.
My questions are:
- do you know a better solution?
- how to include JavaScript inside the Head of the page pro grammatically?
Thanks
It sounds as if you're not using MVC with MasterPages (which has a built in method for including such a JavaScript). If you're using web forms, you can put a Literal in the head of your document and assign its text value from the code behind:
<asp:Literal ID="myTinyMCEScript" runat="server" />
...
string adminScriptText = "some javascript to format for admins";
string userScriptText = "some javascript to format for knuckledraggers";
this.myTinyMCEScript.Text = (myUserRole == "admin") ? adminScriptText : userScriptText
Not terribly elegant, but effective.
Edit:
To use a file:
string adminScript = "<script language=\"javascript\" type=\"text\\javascript\" src=\"link/to/adminscript.js\"></script>";
string userScript = "<script language=\"javascript\" type=\"text\\javascript\" src=\"link/to/userscript.js\"></script>";
Since you want to store it in a file, and javascript is run in plain text at the browser, you might as well just stick it in a js file and include that js file.
Related
What I'm doing is grabbing the HTML code for my header from a text file.
But once a User logs in I want it to say Welcome "Username" at the top, which is a dropdown to account settings, cart, etc...
So since I'm inserting the HTML into a DIV on page load, I don't actually have access to any of the elements inside in C#.
How would I go about doing this? Is there any way to access something like a (p id="name")'s inner text, after its loaded in from the text file?
Would like to do this with C# not JS please.
Edit: I have a work around for now, but I am still interested in better answers.
headerText = headerText.Replace("::Username::", Session["Username"] as string);
Here is my code for grabbing the HTML and pasting it in.
string headerText = File.ReadAllText(Server.MapPath("~/global/header.html"));
string footerText = File.ReadAllText(Server.MapPath("~/global/footer.html"));
headerText = headerText.Replace("::Username::", Session["Username"] as string);
divHeader.InnerHtml = headerText;
divFooter.InnerHtml = footerText;
To be more clear, is there anyway to access something like
<asp:Panel ID="panelAccount" runat="server">
which is stored in another HTML file.
I have done similar work in C# earlier, I use HTMLAgilityPack for doing this kind of works. Later I start using Anglesharp since it has a very good CSS selector based support.
Try anglesharp and you can modify the HTML tag like you do in jQuery.
As much as I liked Adrians response, I found out a better way to do what I needed to do, I could do with a MasterPage. I definitely be using Adrians answer for a load of other things though so it still holds valid.
Then in the C# file associated with the master page you create a property like this.
public Panel PanelAccount
{
get { return panelAccount; }
set { panelAccount = value; }
}
Then from the regular WebForm, you can call "PanelAccount" to access that property.
Here is a tutorial for how to do that.
Thanks for everyones downvotes with no inputs, you guys are stars!
Im creating a page generator for ASP page. It takes XML input, and then converts it into ASPX representation.
During the process of conversion, here's some code i used,
var page = new Page();
var pnlUpdate = new UpdatePanel();
page.Controls.Add(pnlUpdate);
Theoritically, it should creates ASP file like this,
<% Page ...>
....
....
<asp:UpdatePanel>
</asp:UpdatePanel>
How do i get the source representation of my programmatically created page object? Using Filter or catching the HttpRespose output gives me the parsed HTML output, not the ASP one.
To my knowledge there is no code in .Net Framework that will convert control tree into APSX page.
You can write your own version (especially if you have very limited set of allowed control/properties) by walking control tree and generating ASPX as you go. Note that you'd need to know what properties are changed from default values...
It probably would be easier to go directly from XML to ASPX, you may be even able to have XSLT transformation to do so.
Basically i have a webpage with embedded css and JavaScript, so what i want to do is extract only the HTML itself, from texts to tables , images and what not.
So far i have the whole web page stored into a string called "html" the contents of this page is just the facebook hompepage for example,but as you will see there's all scripts and other embedded stuff which i don't want to have.
HTMLEdit = //webpage I chose to store in here//
string html = HTMLEdit.DocumentText;
String result = "this i want to only contain the <head>,<body>,<foot>."
I am only interested in displaying the result witch only contains html, i don't want the JavaScript or css or any other stuff
I have looked at the agility pack but there's no documentation on there website to do this and this is my first ever c# project i have decided to make, so excuse my ignorance if i don't make sense.
See this question
HTML Agility Pack strip tags NOT IN whitelist
Maybe adapt that answer, and drop link and script tags.
I'm wondering if anyone knows of a way to allow something like "<<" to be submitted, without setting validaterequest=false
I have a creole parser, and the recommended plugin/macro syntax is:
<<macro-name argo0=foo arg1=bar argN=qux>>
I wrote a little ‘encodeMyHtml’ JavaScript function that is called on the OnClick event when the HTML form’s submit button is clicked. The function encodes the user’s HTML input for the field I’ve specified into a harmless string before it is passed to the server. When I receive that input on the server I simply decode and go on my way.
ValidateRequest is happy, our users are happy, our peers are happy, heck we’re happy.
I add my ‘encodeMyHtml’ JavaScript function in my user control’s OnPageLoad method. This way I can make sure that my JavaScript is added to the parent page only once, no matter how many controls are on the page.
In my control’s OnPageLoad I call this:
private void addEditorJavaScript()
{
// create our HTML encoder javascript function
// this way it shows up once per page that the control is on
string scr = #"<script type='text/javascript'>function encodeMyHtml(name){
var content = document.getElementById(name).value
content = content.replace(/</g,'<');
content = content.replace(/>/g,'>');
document.getElementById(name).value = content;
}</script>";
// add the javascript into the Page
ClientScriptManager cm = Page.ClientScript;
cm.RegisterClientScriptBlock(this.GetType(), "GlobalJavascript", scr);
}
In my control’s ASPX I’m using a gridview. I wrap the gridview’s update asp:LinkButton in a span tag, and in that span tag I put my OnClickEvent.
<span onclick="encodeMyHtml('<%# UniqueID.Replace("$", "_") %>_FormViewContentManager_ContentTextBox')">
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="[Publish]" />
</span><span onclick="encodeMyHtml('
When I get the input on the server side I simply call a couple of Replace methods on the input string to decode the HTML, and I’m done.
You could do a javascript regex replace for "<\S" on the specific field on form submit. But it would fail for browsers that don't support javascript.
You can encode the "<<" on the client using Javascript:
<script language="javascript">
function encodeString(str) {
return str.replace(/</gi, '<').replace(/>/gi, '>');
}
</script>
And then on the server use Server.HtmlDecode to return the string to its original form.
Kentico is a C# / Asp.NET Content Management System that we use and I'm trying to implement authorize.net SIM integration (redirecting the user to the authorize.net servers to make purchase through a form post). Kentico uses master pages so it's proving to be a beast. First issue was getting the form to even post to the authorize.net Servers. I was able to do this using the following.
<script type="text/javascript">
theForm.action = "https://test.authorize.net/gateway/transact.dll";
</script>
Easy Enough (theForm == the master page form), now the issue lies in the fact that I originally was using code behind to populate the hidden input fields and it changes all of the names of these input fields. This makes it impossible for authorize.net to know what you are doing.
Has anyone done any integration like this before? And if so, what is the most appropriate way to solving this problem?
I have a few ideas but they all involve what I consider extremely dirty methods for getting it to work.
you will need to follow these steps to take the names of your input fields under your control:
1) Use your code to set the payment gateway URL
2) Place ASP.NET Literal control on ASPX page, something like:
<asp:Literal runat="server" ID="myFields" />
3) Go to code behind and initialize the literal with HTML code of all your input fields. For each input field set its custom identifier. e.g:
myFields.Text += "<input type="text" id="carnumber" name="cardnumber" />"; ....
After the button is clicked, user is redirected to the payment gateway URL where the POSTed data from your input fields are available under the required identifiers. I hope you will find it helpful.