I am working on a forum application using SQL Server and ASP.Net.
The application has facilities for creating topics and posting replies to specific topics. It must also have a facility for marking specific replies as accepted answers in which case the row gets highlighted in green color [very much like the facility we have
in our forum here]. There is a field in the database called 'AcceptedAnswer'[it belongs to a table called 'ForumMessages'] which is a bit type field. On marking an answer this should get updated to 'True'[by default it is 'False']. I have written the code
for implementing this but it is not working as per desired. The database is being updated as expected but the corresponding row in the databound control is not changing its color on clicking the button. I have used a datalist control to show messages and put
the button inside it. There is also a hiddenfield control which binds the value of the 'AcceptedAnswer' retrieved from database through stored procedure. To change the color dynamically I have used the 'onitemdatabound' attribute of the datalist.
Here is the mark up for the datalist::---
<asp:DataList ID="dLMessages" OnItemDataBound="dLMessages_DataBound" DataKeyField="MessageID" runat="server">
<ItemTemplate>
<table width="100%" style="background-color:#BCDDFE">
<tr>
<td valign="top">
<asp:Image ID="Image1" Width="40px" Height="40px" runat="server" ImageUrl="~/Styles/Messages-Icon.png" />
<b>Re:</b>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("TopicSubject") %>' ForeColor="#FF3300" Font-Underline="True"></asp:Label>
<asp:HiddenField ID="hdnfld1" runat="server" Value='<%# Eval("AcceptedAnswer") %>' />
<asp:HiddenField ID="hdnfld2" runat="server" Value='<%# Eval("UserID") %>' />
<asp:HiddenField ID="hdnfld3" runat="server" Value='<%# Eval("MessageID") %>' />
</td>
</tr>
<tr>
<td valign="top">
<i>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("MessageBody") %>'></asp:Label></i>
</td>
</tr>
<tr>
<td><b>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("USignature") %>'></asp:Label></b></td>
</tr>
<tr>
<td><b>By:</b>
<asp:Label ID="lblAuthor" runat="server" Text='<%# Eval("Username") %>'></asp:Label>
</td>
</tr>
<tr>
<td><b>Posts</b>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("PostCount") %>'></asp:Label>
</td>
</tr>
<tr style="background-color:Teal">
<td>
<table>
<tr>
<td>
<asp:Button ID="btnEditPost" runat="server" Text="EDIT" BackColor="#000066" ForeColor="Yellow" /></td>
<td>
<asp:Button ID="btnAcceptAnswer" CommandArgument='<%# Eval("MessageID") %>' OnClientClick="return confirm('Are you sure you want to mark this answer as accepted?')" OnClick="btnAcceptAnswer_Click" BackColor="#000066" ForeColor="Yellow" runat="server" Text="ACCEPT" />
</td>
</tr>
</table>
</ItemTemplate>
<SeparatorTemplate>
<hr style="color:Blue" />
</SeparatorTemplate>
</asp:DataList>
Here is the code block I am using to change color and to update the database::---
protected void btnAcceptAnswer_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
int messageid = Convert.ToInt32(btn.CommandArgument);
objbll.MarkAnswer(messageid);
this.BindPosts();
}
protected void dLMessages_DataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
HiddenField hdn = (HiddenField)e.Item.FindControl("hdnfld1");
if (Convert.ToBoolean(hdn.Value) == true)
{
dLMessages.ItemStyle.BackColor = Color.FromName("#A0FE96");
}
}
}
Here MarkAnswer() is the corresponding method in the BLL. This part is working fine but not the color changing operation. I have place the whole thing inside an update panel and a content template. Why is my code not working? Please help me find a solution
by steering my in the right direction and point out the errors in my code snippets. Look forward to receiving some active help on this one.
Many thanks in anticipation.
There are more wonders in this world of ours than you can wonder.....
PGChoudhury
Member
11 Points
131 Posts
Datalist Color Changing on Clicking a Button ---
Apr 27, 2012 11:01 AM|LINK
I am working on a forum application using SQL Server and ASP.Net.
The application has facilities for creating topics and posting replies to specific topics. It must also have a facility for marking specific replies as accepted answers in which case the row gets highlighted in green color [very much like the facility we have in our forum here]. There is a field in the database called 'AcceptedAnswer'[it belongs to a table called 'ForumMessages'] which is a bit type field. On marking an answer this should get updated to 'True'[by default it is 'False']. I have written the code for implementing this but it is not working as per desired. The database is being updated as expected but the corresponding row in the databound control is not changing its color on clicking the button. I have used a datalist control to show messages and put the button inside it. There is also a hiddenfield control which binds the value of the 'AcceptedAnswer' retrieved from database through stored procedure. To change the color dynamically I have used the 'onitemdatabound' attribute of the datalist.
Here is the mark up for the datalist::--- <asp:DataList ID="dLMessages" OnItemDataBound="dLMessages_DataBound" DataKeyField="MessageID" runat="server"> <ItemTemplate> <table width="100%" style="background-color:#BCDDFE"> <tr> <td valign="top"> <asp:Image ID="Image1" Width="40px" Height="40px" runat="server" ImageUrl="~/Styles/Messages-Icon.png" /> <b>Re:</b> <asp:Label ID="Label1" runat="server" Text='<%# Eval("TopicSubject") %>' ForeColor="#FF3300" Font-Underline="True"></asp:Label> <asp:HiddenField ID="hdnfld1" runat="server" Value='<%# Eval("AcceptedAnswer") %>' /> <asp:HiddenField ID="hdnfld2" runat="server" Value='<%# Eval("UserID") %>' /> <asp:HiddenField ID="hdnfld3" runat="server" Value='<%# Eval("MessageID") %>' /> </td> </tr> <tr> <td valign="top"> <i> <asp:Label ID="Label2" runat="server" Text='<%# Eval("MessageBody") %>'></asp:Label></i> </td> </tr> <tr> <td><b> <asp:Label ID="Label4" runat="server" Text='<%# Eval("USignature") %>'></asp:Label></b></td> </tr> <tr> <td><b>By:</b> <asp:Label ID="lblAuthor" runat="server" Text='<%# Eval("Username") %>'></asp:Label> </td> </tr> <tr> <td><b>Posts</b> <asp:Label ID="Label3" runat="server" Text='<%# Eval("PostCount") %>'></asp:Label> </td> </tr> <tr style="background-color:Teal"> <td> <table> <tr> <td> <asp:Button ID="btnEditPost" runat="server" Text="EDIT" BackColor="#000066" ForeColor="Yellow" /></td> <td> <asp:Button ID="btnAcceptAnswer" CommandArgument='<%# Eval("MessageID") %>' OnClientClick="return confirm('Are you sure you want to mark this answer as accepted?')" OnClick="btnAcceptAnswer_Click" BackColor="#000066" ForeColor="Yellow" runat="server" Text="ACCEPT" /> </td> </tr> </table> </ItemTemplate> <SeparatorTemplate> <hr style="color:Blue" /> </SeparatorTemplate> </asp:DataList> Here is the code block I am using to change color and to update the database::--- protected void btnAcceptAnswer_Click(object sender, EventArgs e) { Button btn = (Button)sender; int messageid = Convert.ToInt32(btn.CommandArgument); objbll.MarkAnswer(messageid); this.BindPosts(); } protected void dLMessages_DataBound(object sender, DataListItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item) { HiddenField hdn = (HiddenField)e.Item.FindControl("hdnfld1"); if (Convert.ToBoolean(hdn.Value) == true) { dLMessages.ItemStyle.BackColor = Color.FromName("#A0FE96"); } } }Here MarkAnswer() is the corresponding method in the BLL. This part is working fine but not the color changing operation. I have place the whole thing inside an update panel and a content template. Why is my code not working? Please help me find a solution by steering my in the right direction and point out the errors in my code snippets. Look forward to receiving some active help on this one.
Many thanks in anticipation.