everybody!
I know, that probably the question was asked many times, but anyway..
The problem is:
I have a solution with 2 projects (C# Class Library and Web-project).
In class library project I've overriden a text box control, so it has built-in validations. On local server everything works nice, but when I upload published project, the problems begin.
Here's the code:
[Browsable(true)]
[Category("Appearance")]
[DefaultValue("~/images/alert.png")]
public string AlertImageUrl
{
get
{
string path = HttpContext.Current.Server.MapPath("~/images/alert.png");
return GetPropertyValue<string>("AlertImageUrl", path);
}
set
{
SetPropertyValue("AlertImageUrl", value);
}
}
protected override void Render(HtmlTextWriter writer)
{
this.Attributes["onfocus"] = "var objs = this.parentNode.childNodes; for (var i = 0; i < objs.length; i++) {if (objs[i].id == 'imgAlert') { objs[i].style.display = 'none'; } }";
writer.WriteFullBeginTag("span");
base.Render(writer);
if (!_validator.IsValid)
{
writer.WriteBeginTag("img");
writer.WriteAttribute("id", "imgAlert");
writer.WriteAttribute("runat", "server");
writer.WriteAttribute("style", "left: -20px; top: 3px;");
writer.WriteAttribute("src", AlertImageUrl);
writer.WriteEndTag("img");
}
writer.WriteEndTag("span");
}
Image is located in App_Themes/Theme_name/images/alert.png
How to get AlertImageUrl correctly? Strange, if I manually set it returns correct path, but WriteAttribute - no..
Any help is appreciated,
Regards, Maris
Use ResolveClientUrl method instead of the Server.MapPath
Related
So, I am trying to upload a certain files with names like '2018-2-10 10-23-34' // February 10, 2018 10:23:34, this is not a one file only, I have like multiple files with names like these. That's why I use HttpFileCollection.
Now, for example that I selected files with file names like these, I want to check if it has the right file name, else it will just SaveAs as it is.
As you can see below, I added a fake code, its fake since its not working or it has a wrong syntax in it.
I saw a code like this, but I don't know how to apply this on my current code with HttpFileCollection, please help.
bool contains = Directory.EnumerateFiles(path).Any(f => f.Contains("three"));
My Code
protected void UploadButton_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
try
{
string date = DateTime.Now.ToString("yyyy-M-d");
DateTime DateValue;
DateValue = DateTime.Parse(date, CultureInfo.InvariantCulture);
string dayoftheweek = "(" + DateValue.ToString("dddd") + ")";
Response.Write(dayoftheweek);
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
HttpFileCollection hfc = Request.Files;
for (int i = 0; i < hfc.Count; i++)
{
foreach (hfc[i].FileName.Contains(date))// What I am trying to do, but wrong syntax or wrong code
{
hfc[i].SaveAs(#path+"\\" + hfc[i].FileName + dayoftheweek);
}// What I am trying to do, but wrong syntax or wrong code
Response.Write(hfc[i].FileName);
hfc[i].SaveAs(#path+"\\" + hfc[i].FileName);
}
}
catch (Exception) { }
}
Your foreach loop is wrong as you mentioned.
//This will make you iterate trough the file collection
for (int i = 0; i < hfc.Count; i++)
{
if(hfc[i].FileName.Contains(date))
{
hfc[i].SaveAs(#path+"\\" + hfc[i].FileName + dayoftheweek);
}
}
The foreach loop is not needed anymore so you can remove
foreach (hfc[i].FileName.Contains(date))
{
}
The problem is your for each loop doesnt have a declaration
foreach(var variable in Enumerable){
//other code
}
so in your case it would be
foreach( var file in hfc)
{
//other code
}
and it should work just fine
I am trying to convert RTF to plain text in a c# program. I figured out how to do it but it isn't very clean. It uses RichTextBox which I'm not a huge fan of:
using (System.Windows.Forms.RichTextBox rtfBox = new System.Windows.Forms.RichTextBox())
{
rtfBox.Rtf = cTrans.NoteDescription;
tItem.ProcedureShortDescription = rtfBox.Text;
}
I was wondering if there is a better way to go about accomplishing this. Perhaps using RichEditDocumentServer? I could not find a ton of info on it though and was wondering if I could get some help on it. My thought was:
var documentServer = new RichEditDocumentServer();
documentServer.Document.RtfText = cTrans.NoteDescription;
tItem.ProcedureShortDescription = documentServer.Document.Text;
I did some more digging and this works. I figured I'd just post this as I couldn't see it answered anywhere on the site. I'm not sure if that is proper protocol.
I ended up putting it in a helper class so it can be called if needed again:
namespace ABELSoft.Dental.Interface.Helper
{
public class RtfToText
{
public static string convert(string rtfText)
{
string _text;
var documentServer = new RichEditDocumentServer();
documentServer.Document.RtfText = rtfText;
_text = documentServer.Document.Text;
return _text;
}
}
}
This is how I called it:
tItem.ProcedureShortDescription = RtfToText.convert(cTrans.NoteDescription);
Does any of you know why the Script.Render is not disabling the bundle for compilation debug ="true" when the bundle path is not relative to the root folder ?
I am creating the bundle using a relative path to the root like (this path type is mandatory, otherwise an exception will be thrown):
Bundle jsBundle = new ScriptBundle("~/bundles/myscripts/");
But when I try to render it I need to provide the full url like below:
Scripts.Render("http://myserver/bundles/myscripts/")
And the bundle is enabled irrespective of compilation debug mode.
Any ideas what I am missing?
My question is very related to this question - I am rendering my bundle that way - now: how can I make it disabled when compilation debug="true" ?
Any ideas ?
Thanks!
Ovi
To answer to my own question: Scripts.Render doesn't toggle the bundling depending on compilation mode if the bundle url is provided as full url like:
Scripts.Render("http://myserver/bundles/myscripts/")
The approach I took was to create my own mvc helper to render the bundle:
public MvcHtmlString BundleScript(string bundleUrl)
{
var javascriptBuilder = new StringBuilder();
bool filesExist = false;
bool isDynamicEnabled = IsDynamicEnabled();
if (!isDynamicEnabled)
{
IEnumerable<string> fileUrls = GetBundleFilesCollection(bundleUrl);
string rootVirtualDirectory = "~/content/js/";
if (fileUrls != null)
{
foreach (string fileUrl in fileUrls)
{
javascriptBuilder.Append(new ScriptTag().WithSource(GetScriptName(fileUrl, rootVirtualDirectory)).ToHtmlString());
}
filesExist = true;
}
}
if (isDynamicEnabled || !filesExist)
{
javascriptBuilder.Append(new ScriptTag().WithSource(bundleUrl).ToHtmlString());
}
return MvcHtmlString.Create(javascriptBuilder.ToString());
}
private IEnumerable<string> GetBundleFilesCollection(string bundleVirtualPath)
{
var collection = new BundleCollection { BundleTable.Bundles.GetBundleFor(bundleVirtualPath) };
var bundleResolver = new BundleResolver(collection);
return bundleResolver.GetBundleContents(bundleVirtualPath);
}
private bool IsDynamicEnabled()
{
return BundleTable.EnableOptimizations;
}
private static string GetScriptName(string scriptUrl, string virtualDirectory)
{
return scriptUrl.Replace(virtualDirectory, string.Empty);
}
I have been attempting this all morning (VS2K10, OL2K7, .NET 3.5) and my PSTs never attach. I've modified the path to include escaped slashes, no dice. I occasionally see the PST get added then disappear when I hit the command bar button for which I am trying to program.
Here is a snip of my code:
void b_removedPSTs_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault)
{
string PSTToAdd = dd_removed_PSTs.Text;
foreach (PSTWithPath p in removedPSTs)
{
if (PSTToAdd == p.name)
{
olApp.Session.AddStore(#p.path);
}
}
UpdateRemovedList();
}
PSTWithPath is a custom class I've created as follows:
public class PSTWithPath
{
public string name;
public string path;
public Outlook.MAPIFolder mapifolder;
public PSTWithPath(string PSTName, string PSTPath, Outlook.MAPIFolder PSTMAPIFolder)
{
name = PSTName;
path = PSTPath;
mapifolder = PSTMAPIFolder;
}
Advice would be greatly appreciated.
Thanks,
Larry
I solved the issue. It seems that the function was being case sensitive. Not sure if this was a MS or Novell thing, but it's working now.
I found a good way to check if a file exists and read the contents if it does, but for some reason I can't create a method out of it.
Here's what I have so far:
<script runat="server">
void Page_Load(Object s, EventArgs e) {
lblFunction.Text = mwbInclude("test.txt");
}
string mwbInclude(string fileName) {
string inc = Server.MapPath("/extra/include/" + Request["game"] + "/" + fileName);
string valinc;
if(System.IO.File.Exists(inc))
{
valinc = System.IO.File.ReadAllText(inc);
}
return valinc;
}
</script>
I wish I could provide more info, but the server this is on doesn't show any feedback on errors, just a 404 page.
I think
valinc = Response.Write(System.IO.File.ReadAllText(inc));
should be
valinc = System.IO.File.ReadAllText(inc);
Why are you setting the Text property and calling Response.Write? Do you want to render the text as a label, or as the whole response?
If you're getting a 404, it's because your page isn't being found, not because there's a problem with the script itself. Have you tried ripping out all of the code and just sticking in some HTML tags as a sanity check?