ASP.NET PHP.NET CODEITWELL MASHABLE OPENSOURCEWEBDESIGN BLOGDUMPS FACEBOOK APPS

Home ASP.NET PHP WINDOW Web Programming Web Logic Web Design css Archives
Sunday, August 30

Retrieve value of Dynamic controls in asp.net; Most important part for beginners programming

0 comments

Object: our object is to access value of dynamic controls which are generated conditionally. For this, we will save viewstate of dynamic controls.
Suppose we have one dropdown and one button. When user selects “Generate” option, the Dynamic table will be generated. In each cell of table there will be textbox. User enters value in the textboxes and click on button then it will display all user entered values.

In aspx page

  1. <form id="form1" runat="server">  
  2.     <div>  
  3.         <asp:Table ID="tbl" runat="server">  
  4.         </asp:Table>  
  5.     <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"   
  6.         onselectedindexchanged="DropDownList1_SelectedIndexChanged">  
  7.         <asp:ListItem>Select...</asp:ListItem>  
  8.         <asp:ListItem>Generate</asp:ListItem>  
  9.     </asp:DropDownList>  
  10.         <asp:Button ID="btnSet" runat="server" Text="Button" onclick="btnSet_Click" /> </div>  
  11.     </form>  


To create dynamic control:
  1. protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)  
  2.     {  
  3.         if (DropDownList1.SelectedIndex == 1)  
  4.         {  
  5.             CreateDynamicTable();  
  6.         }  
  7.     }  
  8. private void CreateDynamicTable()  
  9.     {  
  10.          
  11.         // Fetch the number of Rows and Columns for the table   
  12.         // using the properties  
  13.         int tblRows = 5;  
  14.         int tblCols = 5;  
  15.         // Now iterate through the table and add your controls   
  16.         for (int i = 0; i < tblRows; i++)  
  17.         {  
  18.             TableRow tr = new TableRow();  
  19.             for (int j = 0; j < tblCols; j++)  
  20.             {  
  21.                 TableCell tc = new TableCell();  
  22.                 TextBox txtBox = new TextBox();  
  23.                 txtBox.ID = "txt-" + i.ToString() + "-" + j.ToString();    
  24.                 txtBox.Text = "RowNo:" + i + " " + "ColumnNo:" + " " + j;  
  25.                 // Add the control to the TableCell  
  26.                 tc.Controls.Add(txtBox);  
  27.                 // Add the TableCell to the TableRow  
  28.                 tr.Cells.Add(tc);  
  29.                  
  30.             }  
  31.             // Add the TableRow to the Table  
  32.             tbl.Rows.Add(tr);  
  33.             tbl.EnableViewState = true;  
  34.             ViewState["tbl"] = true;  
  35.         }        
  36.     }  


On Button click
  1. protected void btnSet_Click(object sender, EventArgs e)  
  2.     {  
  3.         foreach (TableRow tr in tbl.Controls )  
  4.         {  
  5.             foreach (TableCell tc in tr.Controls)  
  6.             {  
  7.   
  8.                 if (tc.Controls[0] is TextBox)  
  9.                 {   
  10.                     Response.Write(((TextBox)tc.Controls[0]).Text);           
  11.                 }  
  12.             }  
  13.             Response.Write("  
  14. ");    
  15.         }  
  16.                 
  17.     }  


This time No output because dynamic controls are lost in postback then what to do.
So we need to save dynamic controls value and generate dynamic controls again.we need to maintain viewstate.
  1. protected override object SaveViewState()  
  2.     {  
  3.         object[] newViewState = new object[2];  
  4.   
  5.         List txtValues = new List();  
  6.                   
  7.         foreach (TableRow row in tbl.Controls)  
  8.         {  
  9.             foreach (TableCell cell in row.Controls)  
  10.             {  
  11.                 if (cell.Controls[0] is TextBox)  
  12.                 {  
  13.                     txtValues.Add(((TextBox)cell.Controls[0]).Text);  
  14.                 }  
  15.             }  
  16.         }  
  17.   
  18.         newViewState[0] = txtValues.ToArray();   
  19.         newViewState[1] = base.SaveViewState();  
  20.         return newViewState;  
  21.     }  
  22. protected override void LoadViewState(object savedState)  
  23.     {  
  24.         //if we can identify the custom view state as defined in the override for SaveViewState  
  25.         if (savedState is object[] && ((object[])savedState).Length == 2 && ((object[])savedState)[0] is string[] )  
  26.         {  
  27.            object[] newViewState = (object[])savedState;  
  28.             string[] txtValues = (string[])(newViewState[0]);  
  29.             if (txtValues.Length > 0)  
  30.             {   
  31.                 //re-load tables  
  32.                 CreateDynamicTable();  
  33.                 int i = 0;  
  34.                 foreach (TableRow row in tbl.Controls)  
  35.                 {  
  36.                     foreach (TableCell cell in row.Controls)  
  37.                     {  
  38.                         if (cell.Controls[0] is TextBox && i < txtValues.Length)  
  39.                         {  
  40.                             ((TextBox)cell.Controls[0]).Text = txtValues[i++].ToString();  
  41.   
  42.                         }  
  43.                     }  
  44.                 }  
  45.             }  
  46.             //load the ViewState normally  
  47.             base.LoadViewState(newViewState[1]);  
  48.         }  
  49.         else  
  50.         {  
  51.             base.LoadViewState(savedState);  
  52.         }  
  53.     }  


Hurray.. Now you can access dynamic control value on button click.



Comments
0 comments
Do you have any suggestions? Add your comment. Please don't spam!
Subscribe to post feed

Your comments here...

Some Useful Books

http://books.google.com.np/books?id=Lan3g76cCFYC&lpg=PP1&dq=photoshop%20shortcuts&hl=en&pg=PT1#v=onepage&q=photoshop%20shortcuts&f=false

About Me

SubscribeSubscribe via RSS

SubscribeRecent Posts

SubscribeHot Links

Archive

Live Traffic Feed