Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 28th, 2011, 12:52 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile Pre select radiobuttonlist in edit mode

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...

Last edited by sophia; December 29th, 2011 at 01:14 PM..
 
Old December 29th, 2011, 01:11 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

ok solved after many fails...





Similar Threads
Thread Thread Starter Forum Replies Last Post
Detect Edit Mode everest ASP.NET 2.0 Basics 0 December 19th, 2006 05:32 PM
Datagrid - edit mode Programator ASP.NET 1.0 and 1.1 Professional 1 September 5th, 2003 08:38 AM
Datagrid - edit mode Programator ASP.NET 1.x and 2.0 Application Design 1 September 5th, 2003 08:37 AM
Datagrid - edit mode Programator Classic ASP Professional 0 September 5th, 2003 03:13 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.