Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Wednesday, February 01, 2012

Getting Data from SharePoint List to CheckBoxList and RadioButtonList and showing them in two columns side by side

OutPut:-


.Ascx File for CheckBoxList
<asp:CheckBoxList ID="chkArticles" runat="server" RepeatDirection="Horizontal" DataValueField="Title" DataTextField="Title" RepeatColumns="2"></asp:CheckBoxList>

Code to Bind Data for CheckBoxList in .Ascx.cs file
 SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPWeb currentWeb = SPContext.Current.Web;
                    SPList lstArticles = currentWeb.Lists["Articles"];
                    SPQuery sQuery = new SPQuery();
                    sQuery.Query = "<OrderBy><FieldRef Name='Title' Ascending='True' /></OrderBy>";
                    sQuery.ViewFields = "<FieldRef Name='Title' />";
                    sQuery.ViewFieldsOnly = true;
                    SPListItemCollection myColl = lstArticles.GetItems(sQuery);
                    if (myColl.Count > 0)
                    {
                        chkArticles.DataSource = myColl.GetDataTable();
                        chkArticles.DataBind();
                    }
                });
.Ascx file for RadioButtonList
<asp:RadioButtonList ID="rblArticles" runat="server" RepeatDirection="Horizontal" DataValueField="Title" DataTextField="Title" RepeatColumns="2"></asp:RadioButtonList>
 Code to Bind Data for RadioButtonList in .Ascx.cs file
 SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPWeb currentWeb = SPContext.Current.Web;
                    SPList lstArticles = currentWeb.Lists["Articles"];
                    SPQuery sQuery = new SPQuery();
                    sQuery.Query = "<OrderBy><FieldRef Name='Title' Ascending='True' /></OrderBy>";
                    sQuery.ViewFields = "<FieldRef Name='Title' />";
                    sQuery.ViewFieldsOnly = true;
                    SPListItemCollection myColl = lstArticles.GetItems(sQuery);
                    if (myColl.Count > 0)
                    {
                        rblArticles.DataSource = myColl.GetDataTable();
                        rblArticles.DataBind();
                    }
                });











No comments:

Post a Comment