I need to make a slideshow, on a webpage.I am not allowed to use javascript, it must be coded using c# and html using the razor syntax. I have an array of images, my lecturer suggested using switch statements, but I have been trying for weeks with no avail. I have tried, form posts, if statements, switch statements.
he will not give any more help with this.
this is my array and this is how I'm calling the variable, this works fine, however i cant figure out how to make it change when i click next or previous. I've removed all code from the buttons as nothing ive tried will work.
any help would be greatly appreciated
string[] images = {"images/1.jpg", "images/2.jpg", "images/3.jpg", "images/4.jpg", "images/5.jpeg", "images/6.png", "images/7.png", "images/8.png"};
<img src="#image" width="250px" height="250px" /><br />
<button>Next</button>
<button>Previous</button>
You could make next and previous "buttons" be links that call a controller with the number of the next and previous page in the query string parameter
You would of course ideally use JavaScript for this.
An alternative, and IMO not the best way, is to do something like:
Previous
Next
...and then get your razor template to display the appropriate image:
#{
string imageID = Request.QueryString["imageID"];
// fetch the image based on this ID
// (insert your logic here to get your image)
string theImage = #fetch.your.image
<img src="#theImage" />
}
Related
using MS visual studio 2012, c# 4.5 and Umbraco 7.1.
I have been tweaking my master page, and I noticed I had to make images recursive (display on every page).
with property's its no problem, you simply say for example:
#Umbraco.Field("pageTitle", recursive: true)
this works fine, however for images its a little different as im using a media picker as the datatype for the image, when you do this you need to add some extra code like the following
if (Model.Content.HasValue("titleImage"))
{
var dynamicMedia1 = Umbraco.Media(Model.Content.GetPropertyValue("titleImage"));
<a class="pull-left">
<img class="media-object" src="#dynamicMedia1.umbracoFile" alt="" />
</a>
}
this display the image correctly which is great, but as it does not use umbraco.field I cant see how to use "recursive". I've tried looking at some old documentation but doesn't really reference images. And as most will know the new info Isn't quiet available + from what I found there was no mention of recursive images.
Is this possible? Am I missing something?
cheers guys
For anyone interested,
seems as the though all you need to do is add and underscore on the name, this flags the image as recursive.
.... Model.Content.GetPropertyValue("_titleImage")
You can also use Model.Content.HasValue("logotype", true) where "true" is the value of a boolean that sets the property's recursiveness. Set to "false" it will look for the property on the current page.
I am still new to the coding field and am having a bit of trouble with a part, hoping someone can help me. I am working on a MVC page where i am trying to move text, the user inputs, around the page to a few pre-set spots and without having to refresh the page. Do i need a type of script for this? And if i do what would be best? Thanks for the help.
You can do it this way.
Enter Text here... <input id="testText" type="text" onKeyUp="javascript:showText($(this), event);"/>
<br/>
<br/>
<br/>
See it here... <span id="testTextSpan"></span>
Then, add this script...
<script type="text/javascript">
var showText = function (el, e) {
$('#testTextSpan').html(el.val());
};
<script>
See it here in this JSFiddle...
http://jsfiddle.net/ZEcNp/
Be sure to include JQuery libraries in your project, or reference the CDN.
If you'd like me to include an example with raw javascript, with no JQuery, I can also provide that for you.
I would recommend jQuery to do all that client-side.
You would just need an ID for each element you would use as the predefined spots, using either .appendTo, .insertAfter, .html, or something else depending on your exact needs.
I retrieve some html codes from database and I want to display this values in a webform.
You can see my code below. It does not display labels and textboxes. However when I View the .aspx page source in the browser I can see retrieved labels and textboxes with Eval. Why I can not see labels and textboxes in the page?
database values:
code behind:
using (BurganEntities burganEntities = new BurganEntities())
{
List<DynamicField> dynamicFields=(from dynamicField in burganEntities.DynamicField select dynamicField).ToList();
cdcatalog.DataSource = dynamicFields;
cdcatalog.DataBind();
}
aspx:
The fast answer is because asp,net controls are compiled server side but you using them as text on the final render html page - so you have skip this compile, and the asp.net page did not know nothing about them.
The solution is to avoid asp.net controls and use regular html controls. You can still get their return on code behind, you may miss some easy to use functionality, but you can make your work with alternative way.
Other possible solution is to read the database and dynamically create the controls. For example you can add a flag on your database line that says, now create a text box, and on code behind you just create that text box dynamically.
Your code is simply outputting the <asp:TextBox /> to the browser; it isn't parsing it with the WebForms processor to convert it to an <input /> element.
In your database, you should probably be storing:
<input id="txtsdsd" name="txtsdsd" class="textbox" onkeypress="return NumberOnly()" />
and then using Request.Form() to retrieve the value.
I'm not sure if you've started to write your dynamic controls but as an addition to second reply, I would like to mention more sources about dynamic controls.
Although there is no concept of controls in ASP.NET MVC any longer, you can follow ASP.NET webform data access page.
As you want to compile your code at server side; on any postback you will loose dynamic content. So read this and all done.
Or as you mentioned you didn't get values of textboxes, please see the following method,
var textBox = FindControl("<id_of_textbox>") as TextBox;
if(textBox != null)
{
var textBoxValue = ((TextBox)textBox).Text;
}
</id_of_textbox>
see FindControl method at this page
I have some text being fetched from the DB, which I am binding to the DataList ItemTemplate in the following form:
<asp:LinkButton runat="server" Text='<%#Eval("url")%>' />
The text that is fetched from the DB might be long and I want to restrict it to (let's say 50 chars at max. with a ... afterwards) in the above eval assignment.
How can this be done here?
Secondly, how do I specify the link here in LinkButton so that on clicking on it, it goes to the specified, the link should open in a new window as in taget=_blank
You can use a tag directly
<a href='<%#Eval("url")%>' taget=_blank> <%# BindText(Eval("url"))%></a>
Codebehind:
public string BindText(obj url)
{
if(url!=null) {return (url.ToString().length > 50) ? url.ToString().Substring(0,50) + '...': url.ToString() ;}
return "";
}
One easy way to handle that would be to create a "Truncate" extension of type String which simply strips X characters from the end of it.
Regarding "target=_blank" - you should be able to accomplish this with the Attributes property of the LinkButton.
Depending on the target browser, using CSS text-overflow is an elegant way to do this at the client instead of the server (maximizes space; only that text which must be truncated will be truncated, and it also takes into account simple punctuation rules).
https://developer.mozilla.org/en/CSS/text-overflow
This blog post shows a decent solution in that it seeks whitespace in which to inject the ellipses (rather than blind truncation).
For setting the target of a LinkButton...
<asp:LinkButton runat="server" target="_blank">
ASP.Net will (usually) ignore attributes that it doesn't recognize and just render them to the client verbatim. However, this won't actually work because a LinkButton is meant to initiate a postback. You can use an anchor tag instead.
What's the best way to approach the following situation in asp.net mvc2 (c#):
I have a form where a user can add more fields ... for example I ask user the details of personal computing devices that they have. For simplicity sake let's say I ask for the brand and type (dropdown list of Desktop/Laptop/Tablet/Other) and they can provide unlimited devices. I will start with a pair of text boxes:
<label>Type</label>
<select><option>Desktop</option><option>Laptop</option><option>Tablet</option><option>Other</option></select>
<label>Brand</label>
<input type="text" />
<input type="button" value="Add more device" />
And then as user click the "Add more device" button I will display another pair of Brand and Type text boxes ... and so on.
My question is how should I use HTML helper for this, how I construct the viewModel, etc.
I am from PHP background and I used to use the following in PHP:
<input type="text" name="brand[]" />
which on the back end will give me an array of brands ... not sure if that's doable in asp.net environment. I would appreciate any input. Thank you.
[I added the following lines to make my question clearer]
I think I have not been very clear with my question.
Suppose I have the following viewmodel:
public class UserRegisterViewModel
{
public string DeviceBrand { get; set; }
public string DeviceType { get; set; }
}
That works well when I have two text boxes:
<%: Html.TextBoxFor(m => m.DeviceBrand) %>
<%: Html.TextBoxFor(m => m.DeviceType) %>
but the situation that I am facing is I need to allow user to add more pair of device brand and type text boxes ... user should be able to add as many as he needs to.
How should I write my viewmodel and view?
I hope this makes my question a bit clearer. I don't have problem in hiding and showing the text boxes (and yes I use JQuery for that).
Thank you.
In C#, like C++ arrays are fixed sizes. You can however use a List<> to do something similar. It allows you to dynamically add data with an add function. Like so:
List<object> name = new List<object>();
name.Add(an_object);
So say a list of strings:
List<string> myStrings == new List<string>();
myStrings.Add("Blah");
They're accessed just like arrays - Console.WriteLine(myStrings[0]) outputs Blah
Depending on scenarion there are following solutions:
If you already know which Textboxes will be shown add them in your page and set their visiblity to false (like in CSS "display:none") And show them later.
If you dont know in advance which textboxes will be shown you can still append them in HTML DOm (Add it to your page's controls collection)
To show them there are two apporaches:
In first case Either use jQuery or Javascript to show them back.
In first case Use can also do it from server side that will result in whole page post back.
In second Apporach Also you can use jQuery or Javascript to add new textboxes in the form
In second case you can add new boxes from your C# code on server but again it will result in whole page post back.
Use any approach that best suits you
What you can do is having UserRegister as Model and have UserRegisterViewModel
whith List property
Then in your view you can have loop to render your viewmodel list property
in a foreach for example in C#
Lets say you have your ViewModel defined as:
public class BrandListViewModel
{
public List<UserRegisterViewModel> Brands {get;set;}
}
Your View would need to output HTML as:
<select name="Brands[0].DeviceType">...</select>
<input type="text" name="Brands[0].DeviceBrand"/>
and
<select name="Brands[1].DeviceType">...</select>
<input type="text" name="Brands[1].DeviceBrand"/>
You need to keep the index going so that the model binder can construct your list in the correct order. Now, you could use jQuery to do this and as you append items to the page just get a count and increment by one. If you want to remove a single one, you need to rename all of the fields.
You could also use a pure jQuery method, parse the elements, push items to an array and send the array to the server.