Jeg er igang med at arbejde på et mindre skoleprojekt i form af en chat.
Jeg har lavet chatrums administration, så der kan laves både kategorier og rum i de kategorier.
Det jeg godt kunne bruge nu er den funktionalitet der er i GridView med når der klikkes på edit. Altså, at der bliver sat tekstbokse ind i den række der skal redigeres i stedet for at det bliver lavet i eget panel et andet sted.
Koden ser ud som følger. Jeg har på nuværende tidspunkt lavet paneler der bliver visible når jeg klikker på Edit uden for en hvilken som helst række, men den skal som sagt gerne ind i den rigtige række.
RoomAdmin.aspx:
<%@ Page Title="Chat40 - Admin - Room Administration" Language="C#" MasterPageFile="~/Site.master"
AutoEventWireup="true" CodeFile="RoomAdmin.aspx.cs" Inherits="Admin_RoomAdmin" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<div>
<h1>
Room Categories:</h1>
<asp:LinkButton ID="lbAddCategory" runat="server" Text="Add Category" OnClick="lbAddCategory_Click" />
</div>
<table>
<tr style="text-align: left;">
<th>
Name:
</th>
<th>
Description:
</th>
<th>
IsPublic:
</th>
</tr>
<asp:PlaceHolder ID="phAddCategory" runat="server" Visible="false">
<tr>
<td>
<asp:TextBox ID="txtRoomCategoryName" runat="server" />
</td>
<td>
<asp:TextBox ID="txtRoomCategoryDescription" runat="server" />
</td>
<td>
<asp:CheckBox ID="chkRoomCategoryIsPublic" runat="server" />
</td>
<td>
<asp:Button ID="btnAddCategory" runat="server" Text="Add Category" OnClick="btnAddCategory_Click" />
</td>
</tr>
</asp:PlaceHolder>
<asp:PlaceHolder ID="phEditCategory" runat="server" Visible="false">
<tr>
<td>
<asp:TextBox ID="txtEditRoomCategoryName" runat="server" />
</td>
<td>
<asp:TextBox ID="txtEditRoomCategoryDescription" runat="server" />
</td>
<td>
<asp:CheckBox ID="chkEditRoomCategoryIsPublic" runat="server" />
</td>
<td>
<asp:Button ID="btnEditCategory" runat="server" Text="Save Changes" OnClick="btnEditCategory_Click" />
</td>
</tr>
</asp:PlaceHolder>
<asp:PlaceHolder ID="phListCategories" runat="server">list categories </asp:PlaceHolder>
</table>
<h1>
Rooms:</h1>
<asp:LinkButton ID="lbAddRoom" runat="server" Text="Add Room" OnClick="lbAddRoom_Click" />
<table>
<tr>
<th>
Category
</th>
<th>
Name
</th>
<th>
Description
</th>
<th>
Min Age
</th>
<th>
Max Age
</th>
<th>
Is Public
</th>
</tr>
<asp:PlaceHolder ID="phAddRoom" runat="server" Visible="false">
<tr>
<td>
<asp:DropDownList ID="ddlRoomCategoryID" runat="server">
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="txtRoomName" runat="server" />
</td>
<td>
<asp:TextBox ID="txtRoomDescription" runat="server" />
</td>
<td>
<asp:TextBox ID="txtRoomMinAge" runat="server" />
</td>
<td>
<asp:TextBox ID="txtRoomMaxAge" runat="server" />
</td>
<td>
<asp:CheckBox ID="chkRoomIsPublic" runat="server" />
</td>
<td>
<asp:Button ID="btnAddRoom" runat="server" Text="Add Room" OnClick="btnAddRoom_Click" />
</td>
</tr>
</asp:PlaceHolder>
<asp:PlaceHolder ID="phListRoomsByCategory" runat="server"></asp:PlaceHolder>
</table>
</asp:Content>
RoomAdmin.aspx.cs:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MsDatabaseModel;
using System.Web.UI.HtmlControls;
public partial class Admin_RoomAdmin : System.Web.UI.Page
{
MsDatabaseEntities ctx = new MsDatabaseEntities();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GetRoomCategories();
GetRooms(1);
}
}
#region Room Categories
private void GetRoomCategories()
{
TableRow tr;
TableCell td;
foreach (ChatRoomCategory category in ctx.ChatRoomCategories)
{
tr = new TableRow();
td = new TableCell();
td.Text = category.RoomCategoryName;
tr.Cells.Add(td);
td = new TableCell();
td.Text = category.RoomCategoryDescription;
tr.Cells.Add(td);
td = new TableCell();
CheckBox cb = new CheckBox();
cb.Checked = category.RoomCategoryIsPublic;
cb.Enabled = false;
td.Controls.Add(cb);
tr.Cells.Add(td);
td = new TableCell();
LinkButton lbEditCategory = new LinkButton();
lbEditCategory.Text = "Edit";
lbEditCategory.Click += delegate { UpdateCategory(Convert.ToInt32(category.RoomCategoryID)); };
td.Controls.Add(lbEditCategory);
//td.Text = "<a href='EditRoom.aspx?ID=" + category.RoomCategoryID + "'>Edit</a>";
tr.Cells.Add(td);
phListCategories.Controls.Add(tr);
}
}
protected void UpdateCategory(int input)
{
phEditCategory.Visible = true;
ChatRoomCategory rc = (from c in ctx.ChatRoomCategories where c.RoomCategoryID == input select c).First();
txtEditRoomCategoryName.Text = rc.RoomCategoryName;
txtEditRoomCategoryDescription.Text = rc.RoomCategoryDescription;
chkEditRoomCategoryIsPublic.Checked = rc.RoomCategoryIsPublic;
}
protected void btnAddCategory_Click(object sender, EventArgs e)
{
ChatRoomCategory rc = new ChatRoomCategory
{
RoomCategoryName = txtRoomCategoryName.Text,
RoomCategoryDescription = txtRoomCategoryDescription.Text,
RoomCategoryIsPublic = chkRoomCategoryIsPublic.Checked
};
ctx.AddToChatRoomCategories(rc);
ctx.SaveChanges();
phAddCategory.Visible = false;
GetRoomCategories();
}
protected void lbAddCategory_Click(object sender, EventArgs e)
{
phAddCategory.Visible = true;
GetRoomCategories();
}
protected void btnEditCategory_Click(object sender, EventArgs e)
{
ChatRoomCategory rc = (from c in ctx.ChatRoomCategories where c.RoomCategoryID == 1 select c).First();
rc.RoomCategoryName = txtEditRoomCategoryName.Text;
rc.RoomCategoryDescription = txtEditRoomCategoryDescription.Text;
rc.RoomCategoryIsPublic = chkEditRoomCategoryIsPublic.Checked;
ctx.SaveChanges();
GetRoomCategories();
phEditCategory.Visible = false;
}
protected void lbEditCategory_Click(object sender, EventArgs e)
{
phEditCategory.Visible = true;
}
#endregion
#region Rooms
private void GetRooms(int input)
{
var query = from p in ctx.ChatRooms where p.RoomCategoryID == input select p;
foreach (ChatRoom room in query)
{
TableRow roomTr = new TableRow();
TableCell roomTd1 = new TableCell();
roomTd1.Text = room.ChatRoomCategory.RoomCategoryName;
TableCell roomTd2 = new TableCell();
roomTd2.Text = room.RoomName;
TableCell roomTd3 = new TableCell();
roomTd3.Text = room.RoomDescription;
TableCell roomTd4 = new TableCell();
roomTd4.Text = room.RoomMinAge.ToString();
roomTd4.Style["text-align"] = "center";
TableCell roomTd5 = new TableCell();
roomTd5.Text = room.RoomMaxAge.ToString();
roomTd5.Style["text-align"] = "center";
TableCell roomTd6 = new TableCell();
CheckBox cb = new CheckBox();
cb.Checked = room.RoomIsPublic;
cb.Enabled = false;
roomTd6.Controls.Add(cb);
roomTr.Cells.Add(roomTd1);
roomTr.Cells.Add(roomTd2);
roomTr.Cells.Add(roomTd3);
roomTr.Cells.Add(roomTd4);
roomTr.Cells.Add(roomTd5);
roomTr.Cells.Add(roomTd6);
phListRoomsByCategory.Controls.Add(roomTr);
}
}
protected void GetRoomCategoriesDdl()
{
var categoryQuery = from categories in ctx.ChatRoomCategories
orderby categories.RoomCategoryID
select new { categories.RoomCategoryID, categories.RoomCategoryName };
ddlRoomCategoryID.DataSource = categoryQuery;
ddlRoomCategoryID.DataValueField = "RoomCategoryID";
ddlRoomCategoryID.DataTextField = "RoomCategoryName";
ddlRoomCategoryID.DataBind();
}
protected void lbAddRoom_Click(object sender, EventArgs e)
{
phAddRoom.Visible = true;
GetRoomCategoriesDdl();
}
protected void btnAddRoom_Click(object sender, EventArgs e)
{
ChatRoom r = new ChatRoom
{
RoomCategoryID = Convert.ToInt32(ddlRoomCategoryID.SelectedValue),
RoomName = txtRoomName.Text,
RoomDescription = txtRoomDescription.Text,
RoomMinAge = Convert.ToInt32(txtRoomMinAge.Text),
RoomMaxAge = Convert.ToInt32(txtRoomMaxAge.Text),
RoomIsPublic = chkRoomIsPublic.Checked
};
ctx.AddToChatRooms(r);
ctx.SaveChanges();
phAddRoom.Visible = false;
GetRooms(Convert.ToInt32(ddlRoomCategoryID.SelectedValue));
}
#endregion
}