Databound DIV in asp .net - asp.net

Looking for a custom ASP .NET control were the InnerHTML of a DIV is connected to a databas to fetch the content to render the HTML content from the databas inside the DIV.
Is there one out there I have missed or anyone could tell if its possible to make my own DIV component to make it DataBound?
Thanks,
Stefan

You can't databind to a div, but you can databind to something like a Repeater, which is mainly good for showing rows of data (i.e. repeating the same markup for multiple data items).
If you just want to show one field from one row, you're probably better off with something like a literal:
<div>
<asp:Literal id="myLiteral" runat="server" />
</div>
And then in the code behind...
myLiteral.Text = "some string from the database or wherever";

Related

Access web part properties from within CMSRepeater Template

Is there any way I can access a webpart's properties from withing a repeater's template (or vice versa)?
<div ID="RepeaterWrapper" runat="server">
<cms:CMSRepeater ID="repItems" runat="server">
<ItemTemplate>
<div class="col-sm-4">
<!-- I want to access this div in my code behind or else have it access a property from the code behind-->
</div>
</ItemTemplate>
</cms:CMSRepeater>
</div>
I want to set the inner div's bg color and I can't use classes as the property is given as a hexadecimal color so it would mean a few thousand classes!
Worst case scenario I can do it with some js but would rather a "purer" way of doing it if it exists.
Thanks in advance
Assuming your datasource has that background color in the returned data, once you bind your datasource to the repeater you have access to that within the item templates. Simply use something like this:
<div class="col-sm-4 <%# Eval("BgColorColumnName") %>">
Now if you want to set a value from the actual webpart itself, you need to make sure the property is a public property then you can use something like:
<div class="col-sm-4 <%# YourPublicPropertyName %>">
Are all the items going to have the same color? If its per item, then modify the items you are pulling to include the value.
If this was in portal method you could grab the XML from the Page Template table and get values from it. Since it's purely from code, and it's a repeater, usually you need to store the data somewhere outside the repeater itself (in the items you repeat, or in the current page form data).
If you can access it anywhere from a Macro, then you can use the CMS.MacroEngine.MacroContext.Current.ResolveMacro() to resolve that and get the value.
Can you give us a little more info on where the div BG color would be stored? why it has to be in the repeater itself?

bootstrap metismenu populated by datasource in asp.net

I want to create a "Tree" structured menu that is generated by a datasource.
I know how to do that with an asp.net tree view or a gridview.
However recently I have grown to love bootstrap and I just love the bootstrap mantis menu.
However metismenu only seem to work on lists 'li'. Then this became a problem for me as the list I want to generate is not static is dynamic. So I can't really bind the value one by one I need to have some other way of doing this.
Can anyone give me some ideas on how to achieve this in asp.net webforms?
PS: would a repeater work for this scenario?
You could use a repeater or a listview or pretty much anything that gives you control of the output html...
You have not presented a massive amount of information to go on but I suspect that you have at least some data that contains a text value and a URL.
private void BindMenuData()
{
var menuData = DataLayer.GetMenuStructure();
rptMenu.DataSource = menuData;
rptMenu.DataBind();
}
Then in your HTML/ASPX you will have something like
<asp:Repeater id="rptMenu" runat="Server">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<a href='<# Eval("URL")>'><# Eval("LinkText")></a>
</li>
</ItemTempate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
If you then needed sub menus you would need to nest another repeater inside the item template and bind to it in the repeater itemDataBinding/Databound (Cant remember which) event.
The sub menus leads to an extra layer of complexity which could quite easily descend into a mess of spaghetti code so take some time to think it through!
Of course the fall back could always be a method which uses a string builder to generate the HTML for the menu and spit it out to literal.... Not the best option in my opinion though!

How can I load data into an HTML div tag?

I am trying to create a product review web page. The product review has product image, title, description, etc, in a SQL database. How can I load this datas into an HTML div tag, using ASP.NET?
Add the following to your div
<div runat="server" Id="dataDiv"></div>
And then in your CodeBehind you can call your div with its Id
dataDiv.InnerHtml= YourData;// this can be anything from database or a variable
hope it helped.
Thanks,
Aneef
You can add an ID property and runat="server" to <div> to be able to reference it from code behind, and add HTML using the InnerHtml property.
You can also use an <asp:Panel> control, which emits a <div>, and insert your HTML by adding a Literal control from code behind that contains your markup. That approach also allows you to add controls to the <div>, should you need to.

Image from dataset in ASP gridview

I receive a certain image name in a boundfield (i have two images in the same folder) and it comes from testing in the codebehind and binding a datatable into this gridview.
example:
it comes as "image1.jpg" or "image2.jpg"
I'd like this text not to be displayed, instead, i want these images that are in
/folder1/folder2 of my solution (i access images like this, and it works)
either i transform it into html tag...
<img src='/folder1/folder2/image1.jpg' border='0'>
(that's the reason for the first question i've made)
...OR I create an asp image somehow inside the boundfield.
Is there any way to do so ?
I've used:
<asp:Literal runat="server" Text="<%#Eval("myField")%>" />
Inside a templatefield

How to get at repeater Item from jQuery

I've got a repeater on the page. The repeater is actually in an .ascx. In the repeater each item has a few things such as an Add button, and a couple of other fields.
What I am trying to the Container.DataItem but the one that relates to the Add button that was clicked. If the user clicks the add button in the repeater list, give me reference to the Container.DataItem for that related to the button in that ItemTemplate that the user just clicked.
The add button is really just a regular HTML hyperlink wrapped around a regular HTML image. I added an ID to the hyperlink but don't know how to really link the two and gain reference to the DataItem.
I'm all set and can fly with jQuery and do whatever client-side stuff I want.
Example of what I started:
<script type="text/javascript">
$(document).ready(function()
{
$(myUserControl.MyRepeater).
}
</script>
The repeater doesn't generate any HTML of its own. It'll only write what you tell it to write.
So what you'll have to do is somehow identify each item, preferably with unique id's in the HTML. That way you can gain access to it through the DOM.
For example, if the rendered markup looked like this then you could actually access "item1" via jquery:
<div id="item1">abc</div>
<div id="item2">def</div>
To accomplish this you might try something like this in your ItemTemplate :
<ItemTemplate>
<div id="item<%#Container.ItemIndex %>">bla</div>
</ItemTemplate>
The Repeater and the DataItems are server-side control which is available from the code-behind of your pages/controls. You cannot access it from client-side code (javascript).

Resources