Tutorials
Resources
Forum
Interview
Jobs
Projects
Offshore Development
Mentor
|
Code Converter
|
Articles
|
Code Factory
|
Computer Jokes
|
Members
|
Peer Appraisal
|
IT Companies
|
Bookmarks
|
Revenue Sharing
|
Talk to Us
|
My Profile
Sign In
Register
AdSense Revenue
Gifts
Active Members
Today
Athira Appukut...
(253)
D.Jeya kumar(J...
(143)
vipul
(101)
Last 7 Days
Athira Appukut...
(1712)
D.Jeya kumar(J...
(1341)
Sridhar R
(1336)
more...
dynamic control at runtime with maintaining viewstate
Posted Date: 05 Jul 2008 Resource Type:
Code Snippets
Category:
ASP.NET WebForms
Posted By:
Sujit Kumar
Member Level:
Gold
Rating:
Points
: 2
Suppose you want to generate runtime control and add it with your page and you want that when your page goes for postback then the entered value is remain in your textbox etc.
then you have to do these things.
first create container control on aspx page like:
now on .cs you have to write to generate control and add control to this table Table1.
first create two static variable at class
public static int NoOfRows = 5;
public static int ctlID = 0;
now write a function to create control
public void createTable()
{
try
{
// Total number of cells per row (columns).
int cellCtr;
// Current cell counter
int cellCnt;
//rowCnt = row;
cellCnt = 4;
DataTable objDT = new DataTable();
objDT = clsInspection.Get_Faults();
for (int Counter = 1; Counter <= NoOfRows; Counter++)
{
// Create new row and add it to the table.
TableRow tRow = new TableRow();
Table1.Rows.Add(tRow);
for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++)
{
// Create a new cell and add it to the row.
TableCell tCell = new TableCell();
if (cellCtr == 1)
{
DropDownList ddl = new DropDownList();
ddl.ID = "ddl_" + ctlID.ToString();
ddl.Width = 170;
ddl.CssClass = "dropdown";
ddl.DataSource = objDT;
ddl.DataTextField = "Faults";
ddl.DataValueField = "Fault_ID";
ddl.DataBind();
ddl.Items.Insert(0, "Select Faults");
tCell.Controls.Add(ddl);
}
else
{
TextBox txt = new TextBox();
txt.ID = "txt_" + ctlID.ToString();
txt.CssClass = "textbox";
txt.Width = 170;
tCell.Controls.Add(txt);
}
tRow.Cells.Add(tCell);
ctlID++;
}
}
}
catch (Exception ex)
{
//lblErrorMessage.Text = "Error : " + ex.Message.ToString();
//mdlPopup.Show();
}
}
now for maintaine viewstate you have to override method
protected override void CreateChildControls()
{
// Here we are recreating controls to persist the ViewState on every post back
if (Page.IsPostBack)
{
ctlID = 0;
NoOfRows += 1;
createTable();
}
else
{
createTable();
NoOfRows = 5;
}
}
the above method will maintane viewstate for your created control.
now you want that there will be a button on your page and when you click on button one new row will be crete with controls then you have to put button on page only and nothing to do for adding new row for Table1.
Responses
Author:
adeel alvi
17 Jul 2008
Member Level:
Bronze
Points
: 1
Great!!!
you were more better and simpler then msdn article regarding this topic.
"IT is simple to be difficult but it very difficult to be simple "
Regards
Adeel
Feedbacks
Popular Tags
What are tags ?
Search Tags
Sign In
Dynamic control with viewstate
.
Post Feedback
This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must
Sign In
to post a response.
Next Resource:
Displaying web pages in iframe
Previous Resource:
Remoting in ASP.NET
Return to Discussion Resource Index
Post New Resource
Category:
ASP.NET WebForms
Post resources and
earn money
!
Related Resources
How to get child controls in an usercontrol from an aspx page
Find ASP.NET Child Controls
redirecting the form using clientscript
ASP.NET 2.0 - Reading TextBox Values from previous Webform - Passing value between Pages
Save and retrive the value in ViewState object before the PostBack
dotNet Slackers
BizTalk Adaptors
Web Design
doors in nj
Contact Us
Privacy Policy
Terms Of Use