Tuesday 14 February 2012

dynamic user control in asp.net(part-1)

PressMe
Pressme1
Add a user control in Ur project
add a text box in the user control
asp:TextBox ID="tb" runat="server"
add a page in Ur project
add a placeholder,two hiddenfield and button on ur page
asp:PlaceHolder ID="PlaceHolder1" runat="server"
asp:HiddenField ID="hf" runat="server" Value="1"
asp:HiddenField ID="hf1" runat="server" Value="1"
asp:Button ID="btn" runat="server" Width="80" Text="Add More" onclick="btn_Click"

code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
int c;
int y;
protected int Rows
{
get
{
return ViewState["Rows"] != null ? (int)ViewState["Rows"] : 0;
}
set
{
ViewState["Rows"] = value;
}
}

// Columns property to hold the Columns in the ViewState
protected int Columns
{
get
{
return ViewState["Columns"] != null ? (int)ViewState["Columns"] : 0;
}
set
{
ViewState["Columns"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
y = Convert.ToInt32(hf.Value);
ViewState["y"] = 1;
for (c = 0; c < y; c++)
{
//Set the Rows and Columns property with the value
//entered by the user in the respective textboxes
this.Rows = Int32.Parse(hf1.Value);
this.Columns = y;
}
;
}
else
{
c = 0;
y = c + 1;
}

CreateDynamicTable();
}
protected override void LoadViewState(object earlierState)
{
base.LoadViewState(earlierState);
if (ViewState["dynamictable"] == null)
CreateDynamicTable();
}
private void CreateDynamicTable()
{

PlaceHolder1.Controls.Clear();
// Fetch the number of Rows and Columns for the table
// using the properties
int tblRows = Rows;
int tblCols = Columns;
// Create a Table and set its properties
Table tbl = new Table();
// Add the table to the placeholder control
PlaceHolder1.Controls.Add(tbl);
// Now iterate through the table and add your controls
for (int i = 0; i < tblRows; i++)
{
TableRow tr = new TableRow();
for (int j = 0; j < tblCols; j++)
{
TableCell tc = new TableCell();
// TextBox txtBox = new TextBox();
//txtBox.MaxLength = 120;
Control c = Page.LoadControl("WebUserControl.ascx");
// ChatBox chtbx = new ChatBox();
//txtBox.Multiline = true;
//// Add vertical scroll bars to the TextBox control.
//txtBox.ScrollBars = ScrollBars.Vertical;
//// Allow the RETURN key in the TextBox control.
//txtBox.AcceptsReturn = true;
//// Allow the TAB key to be entered in the TextBox control.
//txtBox.AcceptsTab = true;
//// Set WordWrap to true to allow text to wrap to the next line.
//txtBox.WordWrap = true;
// txtBox.Text = "shibashish dynamic control";
// Add the control to the TableCell
tc.Controls.Add(c);
// Add the TableCell to the TableRow
tr.Cells.Add(tc);
}
// Add the TableRow to the Table
tbl.Rows.Add(tr);
}

// This parameter helps determine in the LoadViewState event,
// whether to recreate the dynamic controls or not

ViewState["dynamictable"] = true;
}
protected void btn_Click(object sender, EventArgs e)
{
hf.Value = (y + 1).ToString();
CreateDynamicTable();
}
}

No comments:

Post a Comment