Hello. This is my first post here. I'm rather new to asp.net, although I have solid knowledges in other programming areas.
So, to be short. I have an Ajax enabled asp.net website to develop(using Enterprise Library, but that's not the problem). I created a master page, some content pages, and a custom control. I want to add the custom control in one of the content pages. The idea is the following: I assume I don't know the tables' structure that I'm using, so I develop some custom code and draw dynamically my controls(a gridview and a detailsview). the problem that i have is that on the content page in need to load in the custom control some data. I try to call in the PreInit method of the content pagea method from the custom control, but that control is null. Also, the insert feature of the custom control doesn't work.
Is there a way to address this issue?
3 2137
Welcome to the site. If I understand you correctly, controls are not for storing data this can be done defined in a class.
No. I can give you an example of what i've done and what should be done.
I have a database filled with tables. I don't want to use their structure in modeling my asp.net pages. That would mean writing 10 thousand times the same controls. I created a custom control where, giving in a custom class the name of the table that custom control is built for, it is able to add checkboxfields, and other bounded fields to a gridview and a detailsview(i will handle in the future with calendars, etc).
So, if you have 100 simple(yet) table, that control can be used on 100 simple pages with only drag and drops into a content page.
The problem that i have is the following: I use a master page. A have a content page, let's say adminbranches.aspx, where i want to place a custom control like that one described above. Simple drag and drop, right? I also give the params required so that the custom control can dynamically create the gridview and the detailsview. As I am a newbie in asp.net, I misplaced this initialization procedure into the OnLoad but that's not correct. As far as I've been told by friends, I must find a way so that in the OnPreInit in the content page I would load those params into the custom control. My implementation uses those params defined in the custom control, so that in the OnPreInit the custom control is still null. So my code doesn't run. I would need a way to invoke in custom control's OnInit, before base.OnInit(), a function or some params from the parent( the content page itself). But I don't know why.
In fact, I don't know if that's the way.
Sorry for this long post. I am not a native English speaker, I am at the end of another 8 frustrating hours in which I didn't complete my tasks and I've lost my hope.
Some code:
The content page: -
using System.Data;
-
using System.Configuration;
-
using System.Collections;
-
using System.Web;
-
using System.Web.Security;
-
using System.Web.UI;
-
using System.Web.UI.WebControls;
-
using System.Web.UI.WebControls.WebParts;
-
using System.Web.UI.HtmlControls;
-
using System.Data.SqlClient;
-
using MyCode;
-
-
public partial class AdminArea_AdminUnits : System.Web.UI.Page
-
{
-
private Sucursale suc = new Sucursale(ConfigurationManager.ConnectionStrings["Connection String"].ConnectionString);
-
private string[] cmds ={ "GetSucursale", "InsertSucursala", "UpdateSucursala", "DeleteSucursala" };
-
-
protected override void OnPreInit(EventArgs e)
-
{
-
base.OnPreInit(e);
-
}
-
-
protected override void OnInit(EventArgs e)
-
{
-
//SqlParameterCollection[] parames ={ suc.GetSPInsertParams(), suc.GetSPUpdateParams(), suc.GetSPDeleteParams() };
-
//this.CustomPanel1.LoadForm("MyCode.Sucursale", "Sucursale", suc.GetTableInfo(), cmds, parames);
-
base.OnInit(e);
-
-
}
-
}
-
-
The custom control: -
-
using System;
-
using System.Data;
-
using System.Configuration;
-
using System.Collections;
-
using System.Web;
-
using System.Web.Security;
-
using System.Web.UI;
-
using System.Web.UI.WebControls;
-
using System.Web.UI.WebControls.WebParts;
-
using System.Web.UI.HtmlControls;
-
using System.Data.SqlClient;
-
using MyCode;
-
-
/*
-
http://codebetter.com/blogs/raymond.lewallen/archive/2005/03/10/59583.aspx
-
-
http://john-sheehan.com/blog/index.php/net-cheat-sheets/
-
-
http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx
-
*/
-
-
struct FormAddons
-
{
-
public static SqlParameterCollection updateparams, insertparams, deleteparams;
-
public static string typename;
-
public static string table;
-
public static object[][] columns;
-
public static string getcmd, insertcmd, updatecmd, deletecmd;
-
};
-
-
public partial class Custom_Controls_CustomPanel : System.Web.UI.UserControl
-
{
-
private int nodetailsitems = 1;
-
-
protected override void OnInit(EventArgs e)
-
{
-
FormAddons.getcmd = "asa";
-
base.OnInit(e);
-
if (FormAddons.columns!=null)
-
{
-
CreateGridViewControls();
-
CreateDetailsViewControls();
-
}
-
-
}
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
SetObjectDataSource();
-
}
-
-
#region creez controalele
-
-
/// <summary>
-
/// creez controalele copil pentru grid
-
/// </summary>
-
private void CreateGridViewControls()
-
{
-
CommandField cmf = new CommandField();
-
cmf.ButtonType = ButtonType.Image;
-
cmf.EditText = "Editati inregistarea";
-
cmf.EditImageUrl = "~/pictures/editbox.ico";
-
cmf.CancelImageUrl = "~/pictures/cancel.ico";
-
cmf.CancelText = "Anulati modificarea";
-
cmf.UpdateImageUrl = "~/pictures/oki.ico";
-
cmf.UpdateText = "Efectuati modificarile";
-
cmf.ShowEditButton = true;
-
this.GridView1.Columns.Add(cmf);
-
-
cmf = new CommandField();
-
cmf.ButtonType = ButtonType.Image;
-
cmf.DeleteImageUrl = "~/pictures/delete_16x.ico";
-
cmf.ShowDeleteButton = true;
-
cmf.DeleteText = "Stergeti inregistrarea";
-
this.GridView1.Columns.Add(cmf);
-
-
int step = 0;
-
foreach (object[] param in FormAddons.columns)
-
{
-
switch (param[1].ToString())
-
{
-
case "bit":
-
CheckBoxField cf = new CheckBoxField();
-
cf.HeaderText = param[0].ToString();
-
if (step == 0)
-
{
-
cf.ReadOnly = true;
-
cf.InsertVisible = false;
-
}
-
cf.DataField = param[0].ToString();
-
cf.SortExpression = param[0].ToString();
-
this.GridView1.Columns.Add(cf);
-
break;
-
default:
-
BoundField bf = new BoundField();
-
if (step == 0)
-
{
-
bf.ReadOnly = true;
-
bf.InsertVisible = false;
-
}
-
bf.HeaderText = param[0].ToString();
-
bf.DataField = param[0].ToString();
-
bf.SortExpression = param[0].ToString();
-
this.GridView1.Columns.Add(bf);
-
break;
-
}
-
step++;
-
}
-
}
-
-
/// <summary>
-
/// creez controalele pentru detailsview
-
/// </summary>
-
private void CreateDetailsViewControls()
-
{
-
foreach (object[] param in FormAddons.columns)
-
{
-
switch (param[1].ToString())
-
{
-
case "bit":
-
CheckBoxField cf = new CheckBoxField();
-
cf.HeaderText = param[0].ToString();
-
if (nodetailsitems == 1)
-
{
-
cf.ReadOnly = true;
-
cf.InsertVisible = false;
-
}
-
cf.DataField = param[0].ToString();
-
cf.SortExpression = param[0].ToString();
-
this.DetailsView1.Fields.Add(cf);
-
break;
-
default:
-
BoundField bf = new BoundField();
-
if (nodetailsitems == 1)
-
{
-
bf.ReadOnly = true;
-
bf.InsertVisible = false;
-
}
-
bf.HeaderText = param[0].ToString();
-
bf.DataField = param[0].ToString();
-
bf.SortExpression = param[0].ToString();
-
this.DetailsView1.Fields.Add(bf);
-
break;
-
}
-
nodetailsitems++;
-
}
-
}
-
-
#endregion
-
-
#region setez datasource
-
-
/// <summary>
-
/// setez metodele si paramii folositi de data sourceul din pagina
-
/// </summary>
-
private void SetObjectDataSource()
-
{
-
#region setez comenzile...
-
this.ObjectDataSource1.TypeName = FormAddons.typename;
-
this.ObjectDataSource1.SelectMethod = FormAddons.getcmd;
-
this.ObjectDataSource1.UpdateMethod = FormAddons.updatecmd;
-
this.ObjectDataSource1.DeleteMethod = FormAddons.deletecmd;
-
this.ObjectDataSource1.InsertMethod = FormAddons.insertcmd;
-
#endregion
-
-
#region setez paramii de update
-
if (this.ObjectDataSource1.UpdateParameters.Count < 1)
-
{
-
foreach (SqlParameter param in FormAddons.updateparams)
-
{
-
switch (param.DbType)
-
{
-
case DbType.String:
-
this.ObjectDataSource1.UpdateParameters.Add(param.ParameterName.Substring(1), TypeCode.String, "");
-
break;
-
case DbType.Int32:
-
this.ObjectDataSource1.UpdateParameters.Add(param.ParameterName.Substring(1), TypeCode.Int32, "");
-
break;
-
case DbType.Binary:
-
this.ObjectDataSource1.UpdateParameters.Add(param.ParameterName.Substring(1), TypeCode.Boolean, "");
-
break;
-
};
-
}
-
}
-
#endregion
-
-
#region setez paramii de insert
-
if (this.ObjectDataSource1.InsertParameters.Count < 1)
-
{
-
foreach (SqlParameter param in FormAddons.insertparams)
-
{
-
switch (param.DbType)
-
{
-
case DbType.String:
-
this.ObjectDataSource1.InsertParameters.Add(param.ParameterName.Substring(1), TypeCode.String, "");
-
break;
-
case DbType.Int32:
-
this.ObjectDataSource1.InsertParameters.Add(param.ParameterName.Substring(1), TypeCode.Int32, "");
-
break;
-
case DbType.Boolean:
-
this.ObjectDataSource1.InsertParameters.Add(param.ParameterName.Substring(1), TypeCode.Boolean, "");
-
break;
-
};
-
}
-
}
-
#endregion
-
-
#region setez paramii de delete
-
if (this.ObjectDataSource1.DeleteParameters.Count < 1)
-
{
-
foreach (SqlParameter param in FormAddons.deleteparams)
-
{
-
switch (param.DbType)
-
{
-
case DbType.String:
-
this.ObjectDataSource1.DeleteParameters.Add(param.ParameterName.Substring(1), TypeCode.String, "");
-
break;
-
case DbType.Int32:
-
this.ObjectDataSource1.DeleteParameters.Add(param.ParameterName.Substring(1), TypeCode.Int32, "");
-
break;
-
case DbType.Binary:
-
this.ObjectDataSource1.DeleteParameters.Add(param.ParameterName.Substring(1), TypeCode.Boolean, "");
-
break;
-
};
-
}
-
}
-
#endregion
-
-
#region setez sourceurile pt ctroale
-
string[] datakeys ={ FormAddons.columns[0][0].ToString() };
-
-
this.DetailsView1.DataKeyNames = datakeys;
-
this.DetailsView1.DataSourceID = "ObjectDataSource1";
-
-
this.GridView1.DataKeyNames = datakeys;
-
this.GridView1.DataSourceID = "ObjectDataSource1";
-
-
this.GridView1.DataBind();
-
this.DetailsView1.DataBind();
-
#endregion
-
}
-
-
/// <summary>
-
/// setez elementele cu care lucrez in form...
-
/// </summary>
-
/// <param name="typename"></param>
-
/// <param name="table"></param>
-
/// <param name="columns"></param>
-
/// <param name="cmd"></param>
-
/// <param name="parames"></param>
-
public void LoadForm(string typename, string table, object[][] columns, string[] cmd, SqlParameterCollection[] parames)
-
{
-
FormAddons.typename = typename;
-
FormAddons.table = table;
-
FormAddons.columns = columns;
-
FormAddons.getcmd = cmd[0];
-
FormAddons.insertcmd = cmd[1];
-
FormAddons.updatecmd = cmd[2];
-
FormAddons.deletecmd = cmd[3];
-
FormAddons.insertparams = parames[0];
-
FormAddons.updateparams = parames[1];
-
FormAddons.deleteparams = parames[2];
-
}
-
-
#endregion
-
#region evenimente
-
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
-
{
-
-
}
-
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
-
{
-
GridView1.PageIndex = e.NewPageIndex;
-
SetObjectDataSource();
-
}
-
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
-
{
-
GridView1.EditIndex = -1;
-
SetObjectDataSource();
-
}
-
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
-
{
-
GridView1.SelectedIndex = -1;
-
SetObjectDataSource();
-
}
-
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
-
{
-
GridView1.EditIndex = e.NewEditIndex;
-
SetObjectDataSource();
-
}
-
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
-
{
-
GridView1.EditIndex = -1;
-
SetObjectDataSource();
-
}
-
protected void Button1_Click(object sender, EventArgs e)
-
{
-
DetailsViewRowCollection rowcollection = this.DetailsView1.Rows;
-
foreach (DetailsViewRow datarow in rowcollection)
-
{
-
if(datarow.Cells[1].Controls[0].GetType().ToString()=="System.Web.UI.WebControls.TextBox")
-
{
-
System.Web.UI.WebControls.TextBox tempTxtBox = (System.Web.UI.WebControls.TextBox)datarow.Cells[1].Controls[0];
-
string a = tempTxtBox.Text;
-
}
-
}
-
ObjectDataSource1.Insert();
-
}
-
#endregion
-
-
}
-
My business class -
-
using System;
-
using System.Data;
-
using System.Data.SqlClient;
-
using System.Configuration;
-
using System.Web;
-
using System.Web.Security;
-
using System.Web.UI;
-
using System.Web.UI.WebControls;
-
using System.Web.UI.WebControls.WebParts;
-
using System.Web.UI.HtmlControls;
-
-
/// <summary>
-
/// Summary description for Sucursale
-
/// </summary>
-
namespace MyCode
-
{
-
public class Sucursale
-
{
-
MyCode.MyFactory fab = new MyFactory();
-
public Sucursale()
-
{
-
}
-
-
public Sucursale(string ConnectionString)
-
{
-
fab.ConnectionString = ConnectionString;
-
}
-
-
public DataSet GetSucursale()
-
{
-
return fab.ExecuteStoredProc("GetSucursale");
-
}
-
-
public DataSet GetSucursalaByID(Int32 ID_Sucursala)
-
{
-
return fab.ExecuteStoredProc("GetSucursalaByID",ID_Sucursala.ToString());
-
}
-
-
public DataSet DeleteSucursala(Int32 ID_Sucursala)
-
{
-
return fab.ExecuteStoredProc("DeleteSucursala", ID_Sucursala.ToString());
-
}
-
-
public DataSet UpdateSucursala(String Denumire, String CodSucursala, System.Nullable<Boolean> SucursalaNoua , Int32 ID_Sucursala)
-
{
-
return fab.ExecuteStoredProc("UpdateSucursala", ID_Sucursala.ToString(), Denumire, SucursalaNoua, CodSucursala);
-
}
-
-
public DataSet InsertSucursala(String Denumire,System.Nullable<Boolean> SucursalaNoua,String CodSucursala)
-
{
-
return fab.ExecuteStoredProc("InsertSucursala", Denumire, SucursalaNoua, CodSucursala);
-
}
-
-
public SqlParameterCollection GetSPGetSpecialParams()
-
{
-
return fab.GetStoredProcParams("GetSucursalaByID",false);
-
}
-
-
public SqlParameterCollection GetSPUpdateParams()
-
{
-
return fab.GetStoredProcParams("UpdateSucursala", false);
-
}
-
-
public SqlParameterCollection GetSPDeleteParams()
-
{
-
return fab.GetStoredProcParams("DeleteSucursala", false);
-
}
-
-
public SqlParameterCollection GetSPInsertParams()
-
{
-
return fab.GetStoredProcParams("InsertSucursala", false);
-
}
-
-
public object[][] GetTableInfo()
-
{
-
DataSet myset = fab.ExecuteStoredProc("GetMetadata","Sucursale");
-
if (myset.Tables.Count != 0)
-
{
-
Object[][] myobj=new object[myset.Tables[0].Rows.Count][];
-
int counter=0;
-
foreach (DataRow dr in myset.Tables[0].Rows)
-
{
-
myobj[counter] = new object[2];
-
myobj[counter][0] = dr.ItemArray[0];
-
myobj[counter][1] = dr.ItemArray[1];
-
counter++;
-
}
-
return myobj;
-
}
-
-
return null;
-
}
-
}
-
}
-
-
And the database connection class -
-
using System;
-
using System.Data;
-
using System.Data.Common;
-
using System.Data.SqlClient;
-
using System.Configuration;
-
using System.Web;
-
using System.Web.Security;
-
using System.Web.UI;
-
using System.Web.UI.WebControls;
-
using System.Web.UI.WebControls.WebParts;
-
using System.Web.UI.HtmlControls;
-
using Microsoft.Practices.EnterpriseLibrary.Data;
-
-
namespace MyCode
-
{
-
public class MyFactory
-
{
-
public string server="Connection String";
-
public string ConnectionString;
-
public MyFactory()
-
{
-
}
-
-
public DataSet ExecuteStringCommand(String command)
-
{
-
Database db = DatabaseFactory.CreateDatabase(server);
-
DbCommand dbcommand = db.GetSqlStringCommand(command);
-
-
DataSet ds=new DataSet();
-
ds=db.ExecuteDataSet(dbcommand);
-
-
return ds;
-
}
-
-
public DataSet ExecuteStoredProc(String storedproc, params object[] parames)
-
{
-
Database db = DatabaseFactory.CreateDatabase(server);
-
DbCommand dbcommand=db.GetStoredProcCommand(storedproc,parames);
-
-
DataSet ds=new DataSet();
-
ds=db.ExecuteDataSet(dbcommand);
-
-
return ds;
-
}
-
-
public SqlParameterCollection GetStoredProcParams(string storedproc,bool includereturns)
-
{
-
SqlConnection sqlcon = new SqlConnection(ConnectionString);
-
SqlCommand cmd = new SqlCommand(storedproc, sqlcon);
-
sqlcon.Open();
-
cmd.CommandType = CommandType.StoredProcedure;
-
SqlCommandBuilder.DeriveParameters(cmd);
-
if (!includereturns)
-
{
-
cmd.Parameters.RemoveAt(0);
-
}
-
sqlcon.Close();
-
sqlcon.Dispose();
-
return cmd.Parameters;
-
}
-
}
-
}
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Dales |
last post by:
I have a custom control that builds what we refer to as "Formlets"
around some content in a page. These are basically content "wrapper"
sections that are tables that have a colored header and...
|
by: Brian |
last post by:
NOTE ALSO POSTED IN
microsoft.public.dotnet.framework.aspnet.buildingcontrols
I have solved most of my Server Control Collection property issues.
I wrote an HTML page that describes all of the...
|
by: Suzanne |
last post by:
Hi all,
I really hope someone out there can help me as I've been tearing my
hair out on this one for a good while and I'm getting really
frustrated now!
My problem is this - my custom...
|
by: Suzanne |
last post by:
Hi all,
I'm reposting this message as I'm experiencing this problem more and
more frequently :
I really hope someone out there can help me as I've been tearing my
hair out on this one for a...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |