show default image in razor if there is no user image - asp.net

This statement is not working, i want to show default image if there is no image uploaded pls help
#if (latestrow.thumb == null)
{
<img src="~/Images/no-pic.gif"/>
}
else
{
<img src="#latestrow.thumb"/>
}

There isn't anything wrong with your code. Assuming latestrow.thumb is null, you'd receive the no-pic.gif.
My only guess is that latestrow.thumb has some value other than null. Perhaps using this would be a better approach assuming the value of thumb could be blank or null (and assuming it is a string):
#if (String.IsNullOrEmpty(latestrow.thumb))
If this doesn't work, check the model being sent to the view and see what's in that property. Chances are it's populated with some value, even perhaps a blank space.
Good luck.

Related

Strange Behaviour of Response.Write()

I am working on an ASP.NET MVC Project. In my controller I check if ModelState is valid and according to the result I assign a value to IsSucceed:
if (ModelState.IsValid)
{
ModelTutucu.modelim.IsSucceed = true;
}
return View("YaziEkle",ModelTutucu.modelim);
In my view I have a div with a "success" class. I have some text written by Response.Write() to the value of IsSucceed:
<div class="success">
#if(Model.IsSucceed) {
string success = "It is successfully saved.";
Response.Write(success);
}
</div>
The problem is that success doesn't appear in the the div. It appears at top of the page. When I do something like this:
<div class="success">Foo</div>
"Foo" appears in the div, but I cant get the same result with Response.Write, it shows strange behaviour. What is the reason of this problem? How can I solve it? Thanks in advance.
You should use the # instead of Response.Write(). Response.Write() is writing before the razor is rendered and written, so that's why it comes out on top, rather than in the middle or where it should be. Using # makes sure that the string is rendered where it should be according to the html markup.
Like:
#if (Model.IsSucceed)
{
string success = "It is successfully saved.";
#success
}

How to make an assertion based on class name in web driver

The HTML is as follows:
<section class="my-account-box-element">
<span class="item icon-home icon-color-1"></span>
</section>
Need to make an assertion based on the class name which changes from 'icon-home icon-color-1' to 'icon-home icon-color-0' depending upon the condition.
First i must warn that you can't add two classes when searching for it using the By, since 'item', 'item-home' on your example are the same. I'm going to assume the main difference is in 'item-color-1' and 'item-color-0'.
WebElement myAccountBoxElement = driver.findElement(By.className("my-account-box-element"));
WebElement spanItem = myAccountBoxElement.findElement(By.tagName("span"));
boolean itemColor = (spanItem.getAttribute("class").contains("item-color-1")) ? true : false;
if (itemColor) {
// do stuff for item-color-1 element
}
// do stuff for the item-color-0 element
}
The above code should work flawlessly provided the above code is the actual HTML, if there are more tags, use findElements() instead and loop in it.
Also i went with a ternary if since it keeps a cleaner code, providing you are only working with those two elements
change as per your needs
assertTrue(driver.findElements( By.Xpath(".//span[contains(#class,'icon-color-1')]")).size() != 0)
Can also use this
By.className("classname");
boolean isElementPresent = driver.findElement(By.className("classname"));

What is the simplest way to find current item ID in the template

In my C# or dreamweaver template I need to know what am I rendering. The problem is that I don't know for sure if I'm looking for a page or component. I could probably use package.GetByType(ContentType.Page) and if it's empty - get content of a component, but I feel there should be a shorter way.
Example of David is shorter:
engine.PublishingContext.ResolvedItem.Item.Id
engine.PublishingContext.ResolvedItem.Item.Id
You can also check the Publishing Context's resolved Item and see if it's a Page or not (if it's not, then it's a Component).
For example:
Item currentItem;
if (engine.PublishingContext.ResolvedItem.Item is Page)
{
currentItem = package.GetByName(Package.PageName);
}
else
{
currentItem = package.GetByName(Package.ComponentName);
}
TcmUri currentId = engine.GetObject(currentItem).Id;
If you want to shortcut the engine.GetObject() call, then you may be able to get the ID from the Item's XML directly:
String currentId = currentItem.GetAsSource().GetValue("ID");
That's how I've seen it done before:
// Contains the call you describe in your question
Page page = GetPage();
if (page == null)
{
// Contains a call using package.GetByName("Component")
// to avoid the situation with multiple Components on the package
Component comp = GetComponent();
// Do component stuff
}
else
{
// Do page stuff
}
Not sure you can encapsulate it much nicer than that really but I may be proved wrong.

Flex validation popups no longer showing up in popup dialogs?

I am working on an application which has quite a bit of field-validation in it. The validation works great and I am 1000000% sure the validation message popups were appearing earlier. Now I did quite a bit of work and refactoring. One of the things I changed was the way I open up Popups/Dialog. In order to have these centered over the entire application instead of the opening component I refactored the way I open dialogs. I used the source of the Alert as a base for this but extended it quite a bit as I was having other issues (Focus Manager etc.) (I am just mentioning this as I am assuming that my missing popups are related to this).
Here comes the code responsible for opening popups in my application:
public function show(realParent:Sprite,
displayParent:Sprite = null,
closeHandler:Function = null,
moduleFactory:IFlexModuleFactory = null):Dialog {
// Get the parent ...
// If none is set, use the top-level-application.
if (!displayParent) {
var sm:ISystemManager = ISystemManager(FlexGlobals.topLevelApplication.systemManager);
// no types so no dependencies
var mp:Object = sm.getImplementation("mx.managers.IMarshallPlanSystemManager");
if (mp && mp.useSWFBridge())
displayParent = Sprite(sm.getSandboxRoot());
else
displayParent = Sprite(FlexGlobals.topLevelApplication);
}
// Register for close-events, making sure the pop-up is closed.
if (closeHandler != null) {
this.addEventListener(CloseEvent.CLOSE, closeHandler);
}
// Setting a module factory allows the correct embedded font to be found.
if (moduleFactory) {
this.moduleFactory = moduleFactory;
} else if (realParent is IFlexModule) {
this.moduleFactory = IFlexModule(realParent).moduleFactory;
} else {
if (realParent is IFlexModuleFactory) {
this.moduleFactory = IFlexModuleFactory(realParent);
} else {
this.moduleFactory = FlexGlobals.topLevelApplication.moduleFactory;
}
// also set document if parent isn't a UIComponent
if (!parent is UIComponent) {
this.document = FlexGlobals.topLevelApplication.document;
}
}
// Make the dialog center itself relative to the parent.
PopUpManager.addPopUp(this, displayParent, true);
PopUpManager.centerPopUp(this);
return this;
}
What could be responsible for the Validation popups not showing up any more? Where should I look?
Chris
Ok ... so I figgured this out by myself again. I coould bang my head at the wall for taking so long for finding it though.
If I use the Spart forms, the FormItems and Forms themselves can define error text areas in order to output error messages. So as soon as the FormItem posesses a skin part with the id "errorTextDisplay" the error messages go there. I was now expecting that if there was no such part, the old notifications would be used ... nope.
After about 2-3 Hours of messing around with the code of FormItem and it's skins, I noticed that the "contentGroup" explicitly defined an attribute to suppress error tooltyips by setting showErrorTip to false. Simply removing the "errorTextDisplay" from the skin and changing the showErrorTip to true made my popups appear nicely :-)
Hopefully this post might help someone with the same problems.

asp.net how to store text box values(tables) thru postbacks

I have created a dynamic matrix display using asp:table/cell combination. and added a textbox displaying a value in each cell. at this point i am creating a view state, which sto res the complete table.
Now if i update the text boxes and click on save, i get the empty table(because of postback) and also the values in ViewState are stale - they are not the updated user values.
so is there a way to create a view state on each text box change. how can i store the edited textbox values?
You do not need to store the text boxes values because they all ready posted back.
You only need to read them from the post back.
Request.Form[YourTextBox.UniqueID]
Now if you wish to save them on viewState you can use a code like
string YouData
{
set
{
ViewState["cYouData"] = value;
}
get
{
if (ViewState["cYouData"] != null)
return ViewState["cYouData"].ToString();
else
return string.Empty;
}
}
But the only reason to do that is to understand if something has change, and to find if something has change a simple hash can do the work.
Detect the PostBack and re-load the values there:
if (!Page.IsPostBack)
{
// initial load
}
else
{
// postback
}

Resources