ASP.net database bound comments - asp.net

I have webpage that has details view. I want to add comment box where user can see/add/edit/save comments. Once saved, the comments will be stored into db with record being displayed (storyID). I have table with columns StoryID and Comments where I would like to store comments.
Details view shows article based on session variable (storyid)
Is there any code sample that I can use? Spent good one hour but not luck.
Many thanks in advance

I've done similar things, here are a few ideas to think about.
Get a better DB layout, with your current 2 columns how will it order the thread correctly?
Column Idea ( CommentId, StoryId, Comment, CreatedOn, CreatedBy )
In your DetailsView, you'll need to bind the existing comments to the bottom of the story, right? Here is an example using a repeater.
<div class="Comment">
<ul class="Comment-Items">
<asp:Repeater ID="Item" EnableViewState="True" runat="server">
<ItemTemplate>
<li class="CommentBy">
<%# Eval("CreatedBy") %> - <%# Eval("CreatedOn") %></li>
<li class="CommentText">
<%# Eval("Comment") %></li>
</ItemTemplate>
</asp:Repeater>
</ul>
</div>
Adding Comments, a few ideas...
a. Add new comment via Ajax and insert the new comment into the DOM
b. Add new comment via Ajax and return all the comments to rebind into the DOM
c. Add new comment with POSTBACK and refresh the page

Related

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!

Assign separate link to each repeater item in ASP .net

I was trying to develop a forum website (trying to mimic some of its features) and I am using Entity framework to get my data. I have to use repeater as my instructor asked me to do so.
I would be having something like this using repeater that gets data for "Forum Category" and "Sub-category" from 2 separate database tables:
Forum Category 1
Sub-Category 1
Sub-Category 2
Forum Category 2
Sub-Category 1
Sub-Category 2
Now, I would like, when clicked on those sub-category list to be directed to the page of their own using query string and that is what I am not being able to figure out at this time. When I assign a link to those sub-lists, since they are automatically generated all of them point to the page that I initially assign. I was wondering if there was someway to dynamically assign links for this scenario. I was thinking of using ENUM, but again how do I run a for loop there.
The code I have for the repeater is below:
<asp:Repeater ID="categoryRepeater" runat="server" OnItemDataBound="RepeaterDataBinding">
<ItemTemplate>
<asp:Label ID="categoryLabel" Text='<%#Eval("CatName") %>' runat ="server"
style="font-weight:bold;font-size:large" />
<br />
<asp:Repeater ID="forumRepeater" runat="server">
<ItemTemplate>
<%#Eval("ForumName")%><br/>
<%-- right up there. Instead of hardcoding "DesktopForum1.aspx?ForumID=" which makes all the links
to point to that page, I am trying to achieve a way to assign links to specific pages for each sub-list...
--%>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
I did try searching the forum, but could not come across with a problem that resembled my situation.
If you're able to get the sub-list page URL with the query that populates forumRepeater, this should work:
<a href='<%#Eval("ForumUrl")%>'><%#Eval("ForumName")%></a><br/>
Would you like to put some acnchor for each item?
It will look like that:
<a name="someAnchor"></a>
and Link will be as follows: www.mywebsite.com/blog/posts#someAnchor
For more details: http://www.echoecho.com/htmllinks08.htm
If it is still not good, you need to handle and process querystring values with Javascript

dynamically populating div in asp.net

I am working on a weboage that will display questions and answers (maybe 5 at one time, maybe 7 at another time) returned from a database table. The questions will each be displayed in a div and the related answers displayed in another div. The question will have an icon "Show Answer / Hide Answer"
How can I go about creating a div and then populating it with values from a table?
Thanks
I would use repeater for that.
1.Create data source pulling data from your database
<asp:sqlDataSource Id="sqldsQuestionsAnswers" ... />
2.Create repeater linking to that data source:
<asp:repeater DataSourceId="sqldsQuestionsAnswers" runat="server">
<itemTemplate>
<div>
<%# Eval("question") %>
<hr/>
<%# Eval("answer") %>
</div>
</itemTemplate>
</asp:repeater>
The repeater will display anything whats in <itemTemplate> tag for every row returned by your query.
So if your query returns 2 questions like that:
Question-------------Answer
-----------------------------------
question1?----------answer1
question2?----------answer2
The output would be:
<div>
question1?
<hr/>
answer1
</div>
<div>
question2?
<hr/>
answer2
</div>
I hope it helps...
We need to know more about how you retrieve your question data and the context of the rest of your page, but you can do a few things here (roughly in order of preference):
Bind your data to an <asp:Repeater > control (or even one of the grid controls)
Build a custom or user control to render your questions and drop that on your page
Build your desired html as a string in code and set it to <asp:Panel > control (Panels render as div tags). If you want to be able to refresh your div without reloading the entire page (AJAX), you can use an <asp:UpdatePanel >.
Build your desired html in code and write directly to the response, either via <%= %> or <%: %> bee-stings or with the Response.Write() method.

SQL Server query + ASP.NET: Counting unique user's post(s) and then making a link to open those posts in a gridview

I was just going through in the making of a project. I am using SQL Server 2008 Expresss, ASP.NET (VB backend), .NET Framework 3.5
My question is related to :
I have a table with columns such as : userid, postid, postname, postval etc.
Now what I would like to do is to present a list of unique user's post counts.
As well as a simple navigation facility such as: a link from those unique post counts by a user. Suppose "abc" is the user.. on clicking the no. of counts of posts by abc (in a list of others) I will be redirected to another list having a gridview or any suitable tabular format for viewing those particular posts by "abc" user.
How can I achieve this..? My mind is all but confused over the query / implementation part :X
Cheers,
-[echo9]-
now what I would like to do is to
present a list of unique user's post
counts
SELECT
userid
,count(postid) as post_count
FROM
the_table
Group By
userid
Now you can bind this to a datagrid (or even better a datarepeater) with an item template that looks like:
<ItemTemplate>
<div>
<a href="myShowListPage.aspx?userid='<%# DataBinder.Eval(Container.DataItem,
"userid") %>' <%# DataBinder.Eval(Container.DataItem, "userid") %>
</div>
<div>
<%# DataBinder.Eval(Container.DataItem,
"post_count") %>' </div>
</ItemTemplate>
Now you will have to use styles and css classes to get the layout and effect you want, but this will move you in the right direction.
note: another suggestion that you learn entity framework is valuable, but may not get you moving very quickly
One solution is to use Entity Framework ..
Read about it

How to populate UpdatePanel in Repeater in ASPX (not code-behind)?

I have a Repeater which displays a list of items (in a grid-like/table view). When a user clicks an item, I display an UpdatePanel under this item with additional information relevant to the item (similar to Accordion control). I know how to populate the elements of the UpdatePanel in code-behind (I pass the ID of the selected element in the Repeater control as CommandArgument, get additional info for this ID, and set up the text fields of the active UpdatePanel controls). But I'm wondering if I could set up binding directly in the ASPX (instead of code-behind). When I used the <%= %> syntax to assign text fields of the UpdatePanel control the values of the page properties, e.g. <%= Comment %>, it sort of worked, but it changed the fields of all UpdatePanels in the repeater. Is there any way to bind the active UpdatePanel to the current values and leave already bound UpdatePanels unchanged?
Are you looking to display a container that displays additional information? Is there other activity in the "box" that requires it be an updatepanel?
<asp:repeater>
<itemtemplate>
<%# Eval("Name") %> <%# Eval("LastName") %><br />
<span onclick="$get('<%# Eval("Id") %>')">View Age</span>
<div id="<%# Eval("Id")%>" style="display:none;">
Age: <%# Eval("Age") %>
</div>
<itemtemplate>
</asp:repeater>
Ithink that's right, some syntax may be off a bit (typing without intellisense). Would that work?
I used ID as a unique identifier for the div id and the onclick command. You could also use jquery, asp:controls or whatever else you wanted.
Easiest way is to nest a FormView inside the update panel. Then the only thing you need to do in the code behind is get the additional info, assign it to the FormView.DataSource, and call FormView.DataBind(). Everything in the FormView will use the <%# Eval("SomeColumn") %> syntax. You'll probably need to use a FindControl() to get a reference to the FormView. I'd type up the code for you but I'll save you some headaches down the road and say DON'T DO THIS.
The update panel is about the most inefficient way to do any ajax stuff. The only way to get it all to wire up correctly with this repeater and server side code is to either have a gigantic viewstate or to rebind the repeater in your page load. You are turning a request that could be 300ms into something that will take over a second...or longer! Get familiar with a good ajax framework and don't be afraid to write real html. At the very least, use a webservice that loads a usercontrol with your markup.
I know the update panel is easy, and it's built in. It might even be adequate for what you are doing, but you must resist. You'll be glad you did.

Resources