Hello all,
I am working on category post module. I am using radiobutton list for the category and binding it in code behind using linq 2 ef to show some
nesting effect. When I go to edit mode (recognised through querystring) for a post then the radiobuttonlist is not assigned a category
for that post. However if I bind radiobutttonlist through EntitySourceControl in markup view then it is assigned category of post in
edit mode but I lose nesting effect.
So kindly tell me how I acheive the both. I am using following code...
Code:
<table id="tblPost" width="100%">
<tr>
<td class="colleft">
<asp:TextBox ID="txtTitle" runat="server" ToolTip="Enter Title" Width="100%" Height="25px"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqtxtTitle" ErrorMessage="Post Title can't be empty"
ControlToValidate="txtTitle" runat="server" Display="Dynamic" ForeColor="Red" />
</td>
<td class="colright">
<asp:Button ID="btnPost" runat="server" Text="Post" onclick="btnPost_Click" />
</td>
</tr>
<tr>
<td class="colleft">
<asp:TextBox runat="Server" ID="txtBody" CssClass="post" Width="300px" Height="350px" TextMode="MultiLine" />
<asp:RequiredFieldValidator ID="reqtxtBody" ErrorMessage="Post Description can't be empty" ControlToValidate="txtBody" runat="server" ForeColor="Red" Display="Dynamic" />
</td>
<td class="colright">
<div class="head">Category</div>
<asp:RadioButtonList ID="chkCats" runat="server"></asp:RadioButtonList>
<asp:RequiredFieldValidator ID="reqchkCat" ErrorMessage="Select atlest one category" ControlToValidate="chkCats" ForeColor="Red"
Display="Dynamic" runat="server" />
</td>
</tr>
</table>
and code behind is...
Code:
public partial class Management_AddEditPost : System.Web.UI.Page
{
long _id = -1;
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Request.QueryString.Get("Id")))
{
_id = Convert.ToInt64(Request.QueryString.Get("Id"));
}
if (!Page.IsPostBack && _id > -1)
{
using (DatabaseEntities myEntities = new DatabaseEntities())
{
var post = (from p in myEntities.Posts
where p.Id == _id
select p).SingleOrDefault();
if (post != null)
{
txtTitle.Text = post.Title;
txtBody.Text = post.Body;
chkCats.DataBind();
ListItem myItem = chkCats.Items.FindByValue(post.CategoryId.ToString());
if (myItem != null)
{
myItem.Selected = true; // binding radiobuttonlist here, but not getting assigned with category
}
}
}
}
protected void btnPost_Click(object sender, EventArgs e)
{
using (DatabaseEntities myEntities = new DatabaseEntities())
{
Post myPost;
if (_id == -1) // Insert new item
{
myPost = new Post();
myPost.CreateDateTime = DateTime.UtcNow;
myPost.UpdateDateTime = myPost.CreateDateTime;
myEntities.AddToPosts(myPost);
}
else // Update existing item
{
myPost = (from p in myEntities.Posts
where p.Id == _id
select p).Single();
myPost.UpdateDateTime = DateTime.UtcNow;
}
myPost.Title = txtTitle.Text;
myPost.Body = txtBody.Text;
foreach (ListItem li in chkCats.Items)
{
if (li.Selected)
{
myPost.CategoryId = Convert.ToInt64(chkCats.SelectedValue);
}
}
myEntities.SaveChanges();
Response.Redirect("AddEditPost.aspx");
}
}
Kindly tell me how I assign category to radiobuttonlist in edit mode while still maintain nesting look.
Many thanks...