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.

Thursday, January 03, 2013

Insert Search Update into a single Lookup column using Radio Button List



WebPart

Backend List

Note:-
  • Sample Category is the Parent List.
  • RBL Sample is the child list which is using Lookup.
  • SampleCategory is the Lookup column Name in the Child List.
Ascx
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestRbListLookUpUserControl.ascx.cs" Inherits="TestRbListLookUp.TestRbListLookUp.TestRbListLookUpUserControl" %>
<table align="center">
<tr>
<td>
Enter Title
</td>
<td>
<asp:TextBox ID="txtTitle" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Select Option
</td>
<td>
<asp:RadioButtonList ID="rblCategory" RepeatColumns="3" runat="server" DataValueField="ID" DataTextField="Title"></asp:RadioButtonList>
</td>
</tr>
<tr><td colspan="2" align="center"><asp:Button ID="btnsave" runat="server"
        Text="Save" onclick="btnsave_Click" />&nbsp;<asp:Button ID="btnSearch"
        runat="server" Text="Search" onclick="btnSearch_Click" />&nbsp;<asp:Button
        ID="btnUpdate" runat="server" Text="Update" onclick="btnUpdate_Click" /></td></tr>
<tr><td colspan="2" align="center"><asp:Label ID="lblMessage" runat="server"></asp:Label> </td></tr>
</table>

Ascx.cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Security;

namespace TestRbListLookUp.TestRbListLookUp
{
    public partial class TestRbListLookUpUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindRadioButtonList();
            }
        }

        protected void btnsave_Click(object sender, EventArgs e)
        {
            try
            {
                SPWeb currentWeb = SPContext.Current.Web;
                SPList lst = currentWeb.Lists["RBL Sample"];
                SPListItemCollection myColl = lst.Items;
                SPListItem item = myColl.Add();
                item["Title"] = txtTitle.Text;
                item["SampleCategory"] = rblCategory.SelectedValue.ToString();
                item.Update();
                txtTitle.Text = "";
                rblCategory.ClearSelection();
                lblMessage.Text = "Insertion Successful";
            }
            catch (Exception Ex)
            {
                lblMessage.Text = Ex.ToString();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                SPWeb currentWeb = SPContext.Current.Web;
                SPList lst = currentWeb.Lists["RBL Sample"];
                SPQuery sQuery = new SPQuery();
                sQuery.Query = "<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>"+txtTitle.Text.ToLower()+"</Value></Eq></Where>";
                SPListItemCollection myColl = lst.GetItems(sQuery);
                if (myColl.Count > 0)
                {
                    SPListItem item = myColl[0];
                    SPFieldLookupValue value = new SPFieldLookupValue(item["SampleCategory"].ToString());
                    foreach (ListItem oItem in rblCategory.Items)
                    {
                        int i = int.Parse(oItem.Value.ToString());
                        int j = int.Parse(value.LookupId.ToString());
                        if (i == j)
                        {
                            oItem.Selected = true;
                            break;
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                lblMessage.Text = Ex.ToString();
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                SPWeb currentWeb = SPContext.Current.Web;
                SPList lst = currentWeb.Lists["RBL Sample"];
                SPListItemCollection myColl = lst.Items;
                foreach (SPListItem item in myColl)
                {
                    if (item["Title"].ToString() == txtTitle.Text)
                    {
                        item["SampleCategory"] = rblCategory.SelectedValue.ToString();
                    }
                    item.Update();
                    break;
                }
                txtTitle.Text = "";
                rblCategory.ClearSelection();
                lblMessage.Text = "Updation Successful";
            }
            catch (Exception Ex)
            {
                lblMessage.Text = Ex.ToString();
            }
        }

        public void BindRadioButtonList()
        {
            try
            {
                SPWeb currentWeb = SPContext.Current.Web;
                SPList lst = currentWeb.Lists["Sample Category"];
                SPQuery sQuery = new SPQuery();
                sQuery.Query = "<OrderBy><FieldRef Name='ID' /></OrderBy>";
                SPListItemCollection myColl = lst.GetItems(sQuery);
                if (myColl.Count > 0)
                {
                    rblCategory.DataSource = myColl.GetDataTable();
                    rblCategory.DataBind();
                }

            }
            catch (Exception Ex)
            {
                lblMessage.Text = Ex.ToString();
            }
        }
    }
}

No comments:

Post a Comment