SharePoint: Problem with BaseFieldControl

Posted by Anoop on Stack Overflow See other posts from Stack Overflow or by Anoop
Published on 2010-12-28T08:06:05Z Indexed on 2010/12/28 8:54 UTC
Read the original article Hit count: 408

Hi All,

In below code in a Gird

First column is BaseFieldControl from a column of type Choice of SPList. Secound column is a text box control with textchange event. Both the controls are created at rowdatabound event of gridview.

Now the problem is that when

Steps:

1) select any of the value from BaseFieldControl(DropDownList) which is rendered from Choice Column of SPList

2) enter any thing in textbox in another column of grid.

3) textchanged event fires up and in textchange event rebound the grid.

Problem: the selected value becomes the first item or the default value(if any). but if i do not rebound the grid at text changed event it works fine. Please suggest what to do.

using System;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;


namespace SharePointProjectTest.Layouts.SharePointProjectTest

{

 public partial class TestBFC : LayoutsPageBase

 {


  GridView grid = null;

  protected void Page_Load(object sender, EventArgs e)

  {

   try

   {
    grid = new GridView();

    grid.ShowFooter = true;

    grid.ShowHeader = true;

    grid.AutoGenerateColumns = true;

    grid.ID = "grdView";

    grid.RowDataBound += new GridViewRowEventHandler(grid_RowDataBound);

    grid.Width = Unit.Pixel(900);

    MasterPage holder = (MasterPage)Page.Controls[0];

    holder.FindControl("PlaceHolderMain").Controls.Add(grid);


    DataTable ds = new DataTable();

    ds.Columns.Add("Choice");

    //ds.Columns.Add("person");

    ds.Columns.Add("Curr");

    for (int i = 0; i < 3; i++)

    {

     DataRow dr = ds.NewRow();

     ds.Rows.Add(dr);

    }

    grid.DataSource = ds;

    grid.DataBind();

   }

   catch (Exception ex)

   {

   }

  }

  void tx_TextChanged(object sender, EventArgs e)

  {

   DataTable ds = new DataTable();

   ds.Columns.Add("Choice");    
   ds.Columns.Add("Curr");

   for (int i = 0; i < 3; i++)

   {

    DataRow dr = ds.NewRow();

    ds.Rows.Add(dr);

   }

   grid.DataSource = ds;

   grid.DataBind();   

  }     

  void grid_RowDataBound(object sender, GridViewRowEventArgs e)

  {

   if (e.Row.RowType == DataControlRowType.DataRow)

   {    

    SPWeb web = SPContext.Current.Web;

    SPList list = web.Lists["Source for test"];

    SPField field = list.Fields["Choice"];


    SPListItem item=list.Items.Add();


    BaseFieldControl control = (BaseFieldControl)GetSharePointControls(field, list, item, SPControlMode.New);

    if (control != null)

    {


     e.Row.Cells[0].Controls.Add(control);

    }    

    TextBox tx = new TextBox();

    tx.AutoPostBack = true;

    tx.ID = "Curr";

    tx.TextChanged += new EventHandler(tx_TextChanged);

    e.Row.Cells[1].Controls.Add(tx);

   }

  }

  public static Control GetSharePointControls(SPField field, SPList list, SPListItem item, SPControlMode mode)

  {    
   if (field == null || field.FieldRenderingControl == null || field.Hidden) return null;

   try

   {

    BaseFieldControl webControl = field.FieldRenderingControl;

    webControl.ListId = list.ID;

    webControl.ItemId = item.ID;

    webControl.FieldName = field.Title;

    webControl.ID = "id_" + field.InternalName;

    webControl.ControlMode = mode;

    webControl.EnableViewState = true;

    return webControl;

   }

   catch (Exception ex)
   {
    return null;

   }

  }

 }

}

© Stack Overflow or respective owner

Related posts about sharepoint2007

Related posts about sharepoint2010