How to access control’s value to another page (Pass Values between Web Pages in ASP.NET 2.0)

October 30, 2007


PostBack to another page in ASP.net 2.0 You can use PostBackUrl attribute of Button control of source page to post to another page. 

FirstPage.aspx:

<form id=”form1″ runat=”server”>
    <div>
<asp:Label ID=”Label1″ runat=”server” Text=”First Page” Width=”305px” ForeColor=”Navy”></asp:Label>
<
br />
<br />
<asp:Label ID=”Label2″ runat=”server” Text=”First Name” Width=”149px”></asp:Label>
<asp:TextBox ID=”txtFirstName” runat=”server”></asp:TextBox>
<br />
<asp:Label ID=”Label3″ runat=”server” Text=”Last Name” Width=”149px”></asp:Label>
<asp:TextBox ID=”txtLastName” runat=”server”></asp:TextBox>
<br /><br />                                     
<asp:Button ID=”BtnSubmit” runat=”server” PostBackUrl=”~/SecondPage.aspx” Text=”Go To Second Page” /></div></form>

SecondPage.aspx:

<form id=”form1″ runat=”server”>
<div>
<asp:Label ID=”Label1″ runat=”server” Text=”Second Page” Width=”305px” ForeColor=”Navy”></asp:Label>
  <br />
 <br />
<asp:Label ID=”Label2″ runat=”server” Text=”First Name” Width=”149px”></asp:Label>
<asp:Label ID=”LblFirstName” runat=”server” ForeColor=”#C00000″ Width=”157px”></asp:Label>
 <br />
<asp:Label ID=”Label4″ runat=”server” Text=”Last Name” Width=”149px”></asp:Label>
<asp:Label ID=”LblLastName” runat=”server” Text=”LblLastName” ForeColor=”#C00000″ Width=”155px”></asp:Label>
</
div>
</form>

SecondPage.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{   
// First we get a reference to the source page by
  
//using the target page’s PreviousPage property,
   
//this PreviousPage property holds the posted page.
   
//Now we check if this property(PreviousPage)is null or not.
if (Page.PreviousPage != null)
{    
// Now we call the FindControl method to get the control
TextBox FirstNameTextBox =
(TextBox)Page.PreviousPage.FindControl(“txtFirstName”);
TextBox LastNameTextBox =
(TextBox)Page.PreviousPage.FindControl(“txtLastName”);
// If it is not null then show the result.
if (FirstNameTextBox != null)
{        
LblFirstName.Text = FirstNameTextBox.Text;
 
}
        
if (LastNameTextBox != null)
      
{
        
LblLastName.Text = LastNameTextBox.Text;
      
}
   
}
 
}
For more detail reading please check:

http://msdn2.microsoft.com/en-us/library/6c3yckfw.asp

- Harpreet Virk


List Controls in ASP.NET 2.0

October 30, 2007

List Controls in ASP.NET 2.0
 
All the list controls inherit from the System.Web.UI.WebControls.ListControl class.  Following are the ListControl Class Properties: 1.  AutoPostBack: If AutoPostBack is true, the form is automatically posted back when the user changes the current selection.
2.  DataSource: Specify the data source for the items.
3.  DataMember: The DataMember property is used to identify the table user want to use.
4.  DataTextField: Indicates the field from data source to use for option text.
5.  DataValueField: Indicates the field from data source to use for option Value.
6.  DataTextFormatString: Gets or sets the formatting string used to control how data bound to the list control is displayed.
7.  Items: Returns a collection of ListItem items.
8.  SelectedIndex: Returns or sets the index of the selected item.
9.  SelectedItem: Returns a ListItem that represent the currently selected Item  List Controls: 

  1. BulletedList: BulletedList controls display list of items with a variety of bullet styles. User can also use this control to create a list of hyperlinks.BulletStyle property can be used to decide what type of style is needed. It can be Numbered (1, 2, 3…), LowerAlpha (a, b, c…) and UpperAlpha (A, B, C…), LowerRoman (i, ii, iii…) and UpperRoman (I, II, III…), and the bullet symbols Disc, Circle, Square.
  2. CheckBoxList: Represents a list of check boxes. Each list item in a CheckBoxList represents an individual check box.
  3. DropDownList: Allows the user to select a single item from a drop-down list.
  4. ListBox: Displays a list of items from which user can make a selection.
  5. RadioButtonList: The items are rendered as radio buttons.

 Code:ListControl.aspx.cs  
protected void Page_Load(object sender, EventArgs e)       
    
{     
           
       
ArrayList aryColor = new ArrayList();
            
       
if (!Page.IsPostBack)
           
          
{
               
            
aryColor.Add(“Red”);
          
  aryColor.Add(“Black”);
          
  aryColor.Add(“White”);
          
  aryColor.Add(“Blue”);

            //CheckBoxList
        
    ChklColor.DataSource = aryColor;
        
    ChklColor.DataBind();
        
    //DropDownList     
            
DdlColor.DataSource = aryColor;
 
            
DdlColor.DataBind();
            
            
//ListBox
            
            
LbColor.DataSource = aryColor;
          
  LbColor.DataBind();           
            
//RadioButtonList
             
            
RblColor.DataSource = aryColor;
          
  RblColor.DataBind(); 
            
//BulletedList
   
            
BlColor.DataSource = aryColor;
          
  BlColor.DataBind();
         }     
    
}
 
Code:ListControl.aspx     
<form id=”form1″ runat=”server”>
<asp:CheckBoxList ID=”ChklColor” runat=”server”>
</
asp:CheckBoxList> <br />
<asp:DropDownList ID=”DdlColor” runat=”server”>
</
asp:DropDownList> <br />
<asp:ListBox ID=”LbColor” runat=”server”>
</
asp:ListBox> <br />
 
<asp:RadioButtonList ID=”RblColor” runat=”server”>
</
asp:RadioButtonList> <br />
       
<asp:BulletedList ID=”BlColor” runat=”server” BulletStyle=”Circle”></asp:BulletedList> <br />

<!– Hyperlink BulletedList –>       
<asp:BulletedList ID=”BlHyperlink” DisplayMode=”HyperLink” runat=”server”>  
                        <asp:ListItem Value=”http://www.google.com”>GOOGLE</asp:ListItem>
<asp:ListItem Value=”http://www.asp.net”>ASP.NET</asp:ListItem>
<asp:ListItem Value=”http://msdn.microsoft.com”>MSDN</asp:ListItem>
</asp:BulletedList>

 </form>

- Harpreet virk