Displaying the code within a stylesheet - c#

I am creating a program that allows users to create there own stylesheets and save them. What i want to know, is it possible to load the stylesheet back into the program and display its contents in a textbox. I only need to know how to display its contents.
Thanks to all who look

...load the stylesheet back into the program and display its contents
in a textbox.
The basic logic is:
txtCss.Text = File.ReadAllText("Path to CSS file")
Regards how you set the path, you can either use a physical path eg
"C:\CssFiles\Files1.css"
Or you could use a virtual path in your site using Server.MapPath eg
string filePath = Server.MapPath("~/files/css/file1.css")

Yes, it's possible. If you provide an textfield element on your page where the user defines his stylesheet, you can simply take the text in the texfield when the page posts back and save it on the server side and do whatever you need to it.
If the stylesheet is created outside your web page, you can simply provide a way for the user to upload it via the FileUpload control

Related

c# check if contentplaceholder has a userdefined control

I have a situation where in I am adding .js file to my aspx page using content place holder.
I need to add all the js to end of the page(the said content place holder is at bottom of the master page)
Here's the code for the same
ContentPlaceHolder content = (ContentPlaceHolder)Page.Master.FindControl("additionalJavaScript");
content.Controls.Add(new DeferedMinifiedScript("~/Scripts/Controls/ListBoxControl.js"));
Now the issue is I need to add the ListBoxControl.js file only once on the page.
But this is adding the file everytime a new listbox control is rendered on the page.
I need to put a check in place to see if the file is already available as control on page then skip adding it again.
I tried the following
var abc = new DeferedMinifiedScript("~/Scripts/Controls/ListBoxControl.js");
if(content.Controls.Contains(abc))
{
//Don't add the file again.
}
But this is still adding the file again and again.
Is there a way out?
Also, I can't use RegisterClientScriptInclude or similar functions
You should be able to use this:
https://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.content(v=vs.110).aspx
and Set the contents to a string and then use string to compare to see if it contains your .js string/path to file, then if it does do not do.

how to remove tags in text while displaying on a web page

I am using TFS API and I try am trying to access a property "Description" of an object "WorkItem"
I want to display this property on a web page.
When I display this on the web page , this is what I see :
<p>This task is created for our SSRS team Sesame project.</p>
Firstly , I wanted to know if this is a html tag or does it mean something else in TFS .
And secondly , is there a way I can display this in plain text ?
This task is created for our SSRS team Sesame project.
Please let me know.
If you just want to remove tags, see this simple solution (it uses Regular Expressions):
Remove HTML tags in String
It could be even better if you create it as a extension method.
This is HTML as the System.Description field is an HTML one.
If you just write the field out to a tag it should render as text without the tags. The browser hosting the webpage will render them like any other HTML tags.

How to stop users from manually editing file path in HTML file input control?

I have a asp.net form with 5 HTML file input controls with runat=server and a submit button. after user selects the files and clicks the submit button, the files are upload in the server.
Problem is the HTML file input controls are editable, and user can edit the path after he has already selected the file from the browse button.
If he enters a invalid file path, the file is not uploaded because it does not exist.
How can I stop users from manually changing the file path? I have tried to make the controls read only, but it disables the browse button also.
You can't. Write your server side logic to cope with missing uploads.
you could try something like having a hidden field and when the user selects the file from browse, it populates to the hidden field as well. Then when they upload, you either read from the hidden field for your upload or using JS replace the values back to the original control and read from that.
Is this what you want to have...
You can do like that hiding the control when user select the file
and showing only the path to the user
in a div
if he disagree, let him click on a link called Choose Another next to the path div, so when user click Choose another toggle the same div with flushing the previous value
see this fiddle : http://jsfiddle.net/T2D3X/
While brian's suggestion works (Thank you brian for this), finally we decided to follow the server side handling approach. since in case of invalid file, the file size would be zero, so the response is instant and we do not need to wait till the file is uploaded because in this case there is not file in the first place.
//check if file exists
if (uploadCtrl.PostedFile.ContentLength > 0)
{
uploadCtrl.PostedFile.SaveAs(fileNameWithPath);
uploadCtrl.Dispose();
}
else
{
fileNameWithPath = "invalid";
}
You can also use these methods:
One before validating: http://jsfiddle.net/T2D3X/1/
One After validating: http://jsfiddle.net/T2D3X/2/
Friend of mine posted the same answer before I have re-modified it.
There are some internal jsfiddle error on second link you can ignore that, i have just added more functionally to check on submit what value exactly you are sending.
Let us know if you find the it helpful ;)
Thank!

How to keep control-specific css with the control code (ASP.NET)

If I'm writing an ASCX control, and that control has markup that requires CSS, and that CSS will only be used by the control itself, is there an elegant way (besides just sticking it in the ASCX file itself) to include the .css file with it?
Ideally, I'd have control.ascx, control.ascx.cs, control.js, and control.css all as a little "package".
It brings up one problem and one concern so far:
Problem: Since the control is in a subdirectory, I don't want to use a tag hardcoded with the knoweldge that the css is in a subfolder. I'd like to write it so that it's relative to the control code (ie: same folder) but still be found at runtime.
Concern: If I did this for ten controls, it's ten more server hits I suppose. Maybe ScriptManager or the RadScriptManager or RadStyleSheet manager will magically aggregate them, but its not a showstopper for me either way.
Any ideas on how to solve the relative-path issue?
To get the correct path of your stylesheet relative to the page, call the ResolveClientUrl method of your user control, passing it the path of the stylesheet relative to the control:
HtmlLink link = new HtmlLink();
link.Href = this.ResolveClientUrl("control.css"); // same directory as the control
link.Attributes["type"] = "text/css";
link.Attributes["rel"] = "stylesheet";
this.Page.Header.Controls.Add(link);
Although each stylesheet will result in a separate request, you can mitigate the issue by enabling content expiration for them.

How to copy all data from a HTML doc and save it to a string using C#

I need to create a data index of HTML pages provided to a service by essentially grabbing all text on them and putting them in a string to go into a storage system.
If this were GUI based, I would simply Ctrl+A on the HTML page, copy it, then go to Notepad and Ctrl+V. Simples. If I can do it via good old point n' click, then surely there must be a way to do it programmatically, but I'm struggling to find anything useful.
The HTML docs in question are being loaded for rendering currently using the System.Windows.Controls.WebBrowser class, so I wonder if its somehow possible to grab the data from there?
I'm going to keep hunting, but any pointers would be very appreciated.
Note: We don't want the HTML source code, and would also really rather not have to parse all the source code to get the text unless we absolutely have to.
If I understand your problem correctly, you will have to do a bit of work to get the data.
WebBrowser browser=new WebBrowser(); // This is what you have
HtmlDocument doc = browser.Document; // This gives you the browser contents
String content =
(((mshtml.HTMLDocumentClass)(doc.DomDocument)).documentElement).innerText;
That last line is the browser's view of the rendered content.
This looks like it might be quite helpful.

Categories

Resources