I have the following tables:
mytest table
mytest_id int identity pk
mytest_name nvarchar(50)
mytest_lookup_id int fk
mytest_key nvarchar(10)
lookup table
lookup_id int identity pk
lookup_desc nvarchar(50)
I have the following working in a sharepoint web part:
I can insert, edit, and delete records from the mytest table using radgrid.
I can select values from a combo box bound to one of the columns in the grid and the value is selected from the lookup table.
I need to add the following functionality:
When a new record is inserted into the table, I need to be able to programmatically assign a value stored as a property of the class into the mytest_key field of the newly inserted record. In the following example, I would like to be able to assign the myvalue property value into the mytest_key field when a new record is inserted. I attempted to use the grid.ItemInserted event but I am not sure how to go about assigning the value into the mytest_key grid column for the newly inserted record.
Here is the web part code:
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Telerik.Web.UI;
namespace WebPart7
{
[Guid("656556bd-6adc-43a0-ad6a-4ddb8b5039c8")]
public class WebPart7:System.Web.UI.WebControls.WebParts.WebPart
{
string cServer = "myserver";
string cDB = "mydb";
string myvalue = "test";
public WebPart7()
{
}
protected override void CreateChildControls()
{
base.CreateChildControls();
// TODO: add custom rendering code here.
// Label label = new Label();
// label.Text = "Hello World";
// this.Controls.Add(label);
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Page.ClientScript.RegisterStartupScript(typeof(WebPart7), this.ID, "_spOriginalFormAction = document.forms[0].action;_spSuppressFormOnSubmitWrapper=true;", true);
if (this.Page.Form != null)
{
string formOnSubmitAtt = this.Page.Form.Attributes["onsubmit"];
if (!string.IsNullOrEmpty(formOnSubmitAtt) && formOnSubmitAtt == "return _spFormOnSubmitWrapper();")
{
this.Page.Form.Attributes["onsubmit"] = "_spFormOnSubmitWrapper();";
}
}
}
void Grid_ItemInserted(object source, GridInsertedEventArgs e)
{
// I would like to be able to assign the value stored in the myvalue property to the mytest_key field of the newly inserted record.
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Panel panel = new Panel();
panel.ID = "Panel1";
panel.EnableViewState = true;
this.Controls.Add(panel);
string lcConnString;
string lcCommand;
SqlDataSource SqlDataSource1;
lcConnString = "Data Source=" + this.cServer + ";Initial Catalog=" + this.cDB + ";Integrated Security=SSPI;";
lcCommand = "SELECT mytest_id, mytest_name, mytest_lookup_id, mytest_key FROM mytest";
SqlDataSource1 = new SqlDataSource(lcConnString,lcCommand);
SqlDataSource1.UpdateCommand = "UPDATE mytest SET mytest_name = @mytest_name, mytest_lookup_id = @mytest_lookup_id, mytest_key = @mytest_key WHERE (mytest_id = @mytest_id)";
SqlDataSource1.DeleteCommand="DELETE FROM mytest WHERE (mytest_id = @mytest_id)";
SqlDataSource1.InsertCommand="INSERT INTO mytest(mytest_name, mytest_lookup_id, mytest_key) VALUES (@mytest_name, @mytest_lookup_id, @mytest_key)";
SqlDataSource1.ID = "sqlds1";
string lcConnString2;
string lcCommand2;
lcConnString2 = "Data Source=" + this.cServer + ";Initial Catalog=" + this.cDB + ";Integrated Security=SSPI;";
lcCommand2 = "SELECT lookup_id, lookup_desc FROM lookup";
SqlDataSource SqlDataSource2 = new SqlDataSource(lcConnString2,lcCommand2);
SqlDataSource2.ID = "sqlds2";
RadGrid grid = new RadGrid();
grid.ID = "RadGrid1";
grid.AutoGenerateColumns = false;
grid.AllowAutomaticDeletes = true;
grid.AllowAutomaticInserts = true;
grid.AllowAutomaticUpdates = true;
grid.DataSourceID = SqlDataSource1.ID;
grid.Width = 400;
GridBoundColumn mycolumn1 = new GridBoundColumn();
mycolumn1.HeaderText = "mytest_name";
mycolumn1.DataField = "mytest_name";
mycolumn1.SortExpression="mytest_name";
mycolumn1.UniqueName="mytest_name";
grid.MasterTableView.Columns.Add(mycolumn1);
string[] mydatakeynames;
mydatakeynames = new string[1];
mydatakeynames[0] = "mytest_id";
grid.MasterTableView.DataKeyNames = mydatakeynames;
grid.MasterTableView.EditMode = GridEditMode.PopUp;
grid.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
GridDropDownColumn mycolumn2 = new GridDropDownColumn();
mycolumn2.HeaderText = "Lookup";
mycolumn2.DataField = "mytest_lookup_id";
mycolumn2.ListValueField = "lookup_id";
mycolumn2.ListTextField = "lookup_desc";
mycolumn2.DataSourceID = SqlDataSource2.ID;
grid.MasterTableView.Columns.Add(mycolumn2);
GridBoundColumn mycolumn3 = new GridBoundColumn();
mycolumn3.HeaderText = "mytest_key";
mycolumn3.DataField = "mytest_key";
mycolumn3.SortExpression="mytest_key";
mycolumn3.UniqueName="mytest_key";
grid.MasterTableView.Columns.Add(mycolumn3);
GridEditCommandColumn mycolumn4 = new GridEditCommandColumn();
grid.MasterTableView.Columns.Add(mycolumn4);
GridButtonColumn mycolumn5 = new GridButtonColumn();
mycolumn5.HeaderText = "Delete";
mycolumn5.UniqueName = "DelCol";
mycolumn5.CommandName = "Delete";
mycolumn5.Text = "Delete";
mycolumn5.ConfirmText = "Delete?";
grid.MasterTableView.Columns.Add(mycolumn5);
grid.ItemInserted += new GridInsertedEventHandler(Grid_ItemInserted);
panel.Controls.Add(SqlDataSource1);
panel.Controls.Add(SqlDataSource2);
panel.Controls.Add(grid);
RadAjaxManager ajaxManager = RadAjaxManager.GetCurrent(this.Page);
if (ajaxManager == null)
{
ajaxManager = new RadAjaxManager();
ajaxManager.ID = "RadAjaxManager1";
Controls.Add(ajaxManager);
this.Page.Items.Add(typeof(RadAjaxManager), ajaxManager);
}
ajaxManager.AjaxSettings.AddAjaxSetting(grid, panel);
}
}
}
mytest table
mytest_id int identity pk
mytest_name nvarchar(50)
mytest_lookup_id int fk
mytest_key nvarchar(10)
lookup table
lookup_id int identity pk
lookup_desc nvarchar(50)
I have the following working in a sharepoint web part:
I can insert, edit, and delete records from the mytest table using radgrid.
I can select values from a combo box bound to one of the columns in the grid and the value is selected from the lookup table.
I need to add the following functionality:
When a new record is inserted into the table, I need to be able to programmatically assign a value stored as a property of the class into the mytest_key field of the newly inserted record. In the following example, I would like to be able to assign the myvalue property value into the mytest_key field when a new record is inserted. I attempted to use the grid.ItemInserted event but I am not sure how to go about assigning the value into the mytest_key grid column for the newly inserted record.
Here is the web part code:
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Telerik.Web.UI;
namespace WebPart7
{
[Guid("656556bd-6adc-43a0-ad6a-4ddb8b5039c8")]
public class WebPart7:System.Web.UI.WebControls.WebParts.WebPart
{
string cServer = "myserver";
string cDB = "mydb";
string myvalue = "test";
public WebPart7()
{
}
protected override void CreateChildControls()
{
base.CreateChildControls();
// TODO: add custom rendering code here.
// Label label = new Label();
// label.Text = "Hello World";
// this.Controls.Add(label);
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Page.ClientScript.RegisterStartupScript(typeof(WebPart7), this.ID, "_spOriginalFormAction = document.forms[0].action;_spSuppressFormOnSubmitWrapper=true;", true);
if (this.Page.Form != null)
{
string formOnSubmitAtt = this.Page.Form.Attributes["onsubmit"];
if (!string.IsNullOrEmpty(formOnSubmitAtt) && formOnSubmitAtt == "return _spFormOnSubmitWrapper();")
{
this.Page.Form.Attributes["onsubmit"] = "_spFormOnSubmitWrapper();";
}
}
}
void Grid_ItemInserted(object source, GridInsertedEventArgs e)
{
// I would like to be able to assign the value stored in the myvalue property to the mytest_key field of the newly inserted record.
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Panel panel = new Panel();
panel.ID = "Panel1";
panel.EnableViewState = true;
this.Controls.Add(panel);
string lcConnString;
string lcCommand;
SqlDataSource SqlDataSource1;
lcConnString = "Data Source=" + this.cServer + ";Initial Catalog=" + this.cDB + ";Integrated Security=SSPI;";
lcCommand = "SELECT mytest_id, mytest_name, mytest_lookup_id, mytest_key FROM mytest";
SqlDataSource1 = new SqlDataSource(lcConnString,lcCommand);
SqlDataSource1.UpdateCommand = "UPDATE mytest SET mytest_name = @mytest_name, mytest_lookup_id = @mytest_lookup_id, mytest_key = @mytest_key WHERE (mytest_id = @mytest_id)";
SqlDataSource1.DeleteCommand="DELETE FROM mytest WHERE (mytest_id = @mytest_id)";
SqlDataSource1.InsertCommand="INSERT INTO mytest(mytest_name, mytest_lookup_id, mytest_key) VALUES (@mytest_name, @mytest_lookup_id, @mytest_key)";
SqlDataSource1.ID = "sqlds1";
string lcConnString2;
string lcCommand2;
lcConnString2 = "Data Source=" + this.cServer + ";Initial Catalog=" + this.cDB + ";Integrated Security=SSPI;";
lcCommand2 = "SELECT lookup_id, lookup_desc FROM lookup";
SqlDataSource SqlDataSource2 = new SqlDataSource(lcConnString2,lcCommand2);
SqlDataSource2.ID = "sqlds2";
RadGrid grid = new RadGrid();
grid.ID = "RadGrid1";
grid.AutoGenerateColumns = false;
grid.AllowAutomaticDeletes = true;
grid.AllowAutomaticInserts = true;
grid.AllowAutomaticUpdates = true;
grid.DataSourceID = SqlDataSource1.ID;
grid.Width = 400;
GridBoundColumn mycolumn1 = new GridBoundColumn();
mycolumn1.HeaderText = "mytest_name";
mycolumn1.DataField = "mytest_name";
mycolumn1.SortExpression="mytest_name";
mycolumn1.UniqueName="mytest_name";
grid.MasterTableView.Columns.Add(mycolumn1);
string[] mydatakeynames;
mydatakeynames = new string[1];
mydatakeynames[0] = "mytest_id";
grid.MasterTableView.DataKeyNames = mydatakeynames;
grid.MasterTableView.EditMode = GridEditMode.PopUp;
grid.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
GridDropDownColumn mycolumn2 = new GridDropDownColumn();
mycolumn2.HeaderText = "Lookup";
mycolumn2.DataField = "mytest_lookup_id";
mycolumn2.ListValueField = "lookup_id";
mycolumn2.ListTextField = "lookup_desc";
mycolumn2.DataSourceID = SqlDataSource2.ID;
grid.MasterTableView.Columns.Add(mycolumn2);
GridBoundColumn mycolumn3 = new GridBoundColumn();
mycolumn3.HeaderText = "mytest_key";
mycolumn3.DataField = "mytest_key";
mycolumn3.SortExpression="mytest_key";
mycolumn3.UniqueName="mytest_key";
grid.MasterTableView.Columns.Add(mycolumn3);
GridEditCommandColumn mycolumn4 = new GridEditCommandColumn();
grid.MasterTableView.Columns.Add(mycolumn4);
GridButtonColumn mycolumn5 = new GridButtonColumn();
mycolumn5.HeaderText = "Delete";
mycolumn5.UniqueName = "DelCol";
mycolumn5.CommandName = "Delete";
mycolumn5.Text = "Delete";
mycolumn5.ConfirmText = "Delete?";
grid.MasterTableView.Columns.Add(mycolumn5);
grid.ItemInserted += new GridInsertedEventHandler(Grid_ItemInserted);
panel.Controls.Add(SqlDataSource1);
panel.Controls.Add(SqlDataSource2);
panel.Controls.Add(grid);
RadAjaxManager ajaxManager = RadAjaxManager.GetCurrent(this.Page);
if (ajaxManager == null)
{
ajaxManager = new RadAjaxManager();
ajaxManager.ID = "RadAjaxManager1";
Controls.Add(ajaxManager);
this.Page.Items.Add(typeof(RadAjaxManager), ajaxManager);
}
ajaxManager.AjaxSettings.AddAjaxSetting(grid, panel);
}
}
}