Guruji Point - Code You Want To Write

  • Home
  • Asp.Net
  • WCF
  • SQL
  • Type Script
  • AngularJS
  • Tech News
  • Blog
  • Earn Online

Wednesday, 5 July 2017

Get TextBox , Dropdown, CheckBox Values In Aspx.cs Page From User Control Using C#

 asp.net, C#, user-control     No comments   


Introduction
In this article we will learn how to read the values which is inside the user control or rad the values from user controls in asp.net web forms. Read textbox, dropdownlist and other controls values in aspx.cs page. In previous article we will already learnt what the UserControl is and what is the use of user control and how to load user control with in a web forms. 

Previous Updates
In previous articles we have learnt Load the Usercontrol In Aspx Page. Read Write Base64 Encoding And Decoding In SQL Server. Binding Data To HTML using AngularJS. CRUD operation using AngularJS.

As you already know that a user control is self contained visual content that can be placed in the web page. User control is just like an Html control we can put it anywhere with in the web form. 

Read UserControl Values In Aspx.cs Page 
For performing this task first we have a Usercontrol and a aspx WebForm which we already create in previous article. If you knew here please view this load user control with in a aspx web form.
Firstly i would like to share my UserControl's Code of its design or .ascx page.
UserControl Code ---
<%@ Control Language="C#" AutoEventWireup="true" 
CodeFile="uc_MyUserControl.ascx.cs" Inherits="UserControls_uc_MyUserControl" %>

 <table>
     <thead> <h3> User Control Loaded </h3></thead>
   <tr>
      <td> <asp:Label ID="lblName" Text="First Name" runat="server"></asp:Label> </td>
      <td> <asp:TextBox ID="txtFName" Text="" runat="server"></asp:TextBox> </td>
   </tr>
   <tr>
     <td> <asp:Label ID="lblGender" Text="Gender" runat="server"></asp:Label> </td>
     <td> <asp:DropDownList ID="ddlGender" runat="server" Width="173px">
                            <asp:ListItem Text="--Select--" Value="0"></asp:ListItem>
                            <asp:ListItem Text="Male" Value="M"></asp:ListItem>
                            <asp:ListItem Text="Female" Value="F"></asp:ListItem>
                        </asp:DropDownList> </td>
    </tr>
     <tr>
          <td> <asp:Label ID="lblAdi" Text="Adult" runat="server"></asp:Label> </td>
         <td><asp:CheckBox ID="chkAdult" runat="server" Checked="false" /></td>
     </tr>
</table>

Now here is the Code of my aspx page where i load the user control uc_MyUserControl
Aspx Code ---
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyWebForm1.aspx.cs" 
    Inherits="UserControls_MyWebForm1" %>
<%@ Register Src="~/UserControls/uc_MyUserControl.ascx" TagPrefix="uc1" 
    TagName="uc_MyUserControl" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"> 

</head>

<body>
    <form id="form1" runat="server">
     <div>
         <asp:Panel ID="myUcPanel" runat="server">
            <uc1:uc_MyUserControl runat="server" ID="uc_MyUserControl" />
             </asp:Panel>
     </div>
        </br>
        <div>
            <asp:Button ID="btnRead" runat="server" Text="Read UserControl Values" 
                           OnClick="btnRead_Click" />
            </br> </br>
            <asp:Label ID="lblResult" runat="server" Font-Bold="false"></asp:Label>
        </div>
    </form>
</body>
</html>

Now the main code where we read the values of  all controls like textbox, checkbox, dropdownlist from the Usercontrol.
Aspx.cs Code To Read the Values ---
protected void Page_Load(object sender, EventArgs e)
    {
        LoadControl();  //Load User Control In Aspx Web Page
    }

    public void ReadUserControl()
    {
        string Fname, Gender, Adult;
        UserControls_uc_MyUserControl objUC = 
           (UserControls_uc_MyUserControl)Page.FindControl("uc_MyUserControl");
        Fname = ((TextBox)objUC.FindControl("txtFName")).Text;
        Gender = ((DropDownList)objUC.FindControl("ddlGender")).SelectedItem.Text;
        Adult = ((CheckBox)objUC.FindControl("chkAdult")).Checked.ToString();
        if (Adult == "True")
            Adult = "Yes";
        else
            Adult = "No";
        //Print the read values in Label lblResult
        lblResult.Text = "First Name     -  " + Fname+ 
                        "</br> Selected Gender -   " + Gender +
                        "</br> Are You Adult  -  " + Adult;

    }

    public void LoadControl()
    {
        UserControls_uc_MyUserControl objUC = 
          (UserControls_uc_MyUserControl)Page.FindControl("uc_MyUserControl");
        myUcPanel.Controls.Add(objUC);
    }

    protected void btnRead_Click(object sender, EventArgs e)
    {
        ReadUserControl();
    }

OutPut 








  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Facebook Updates

Guruji Point

Categories

Comman table Expression in SQL Dynamic Grid Row IQueryable in c# Learn AngularJS Scope_Identity() Use of indexes comma seperated string in sql row_number() and dense_rank() salary table structure. while loop in sql why do we need cursor why tuple used

About Us

This blog is about the Programming ,Tech News and some intresting facts related Contents. Here you can find fundamental and expert level topic of programming languages very practically related to C# , Asp.Net ,Sql-Server, JavaScript, Jquery, WebServices And also Web-Api, WCF ,WPF, Angular Js.

Contact Us

Email Us - gurujipoints@gmail.com
Contact- 8077657477

Reach Us

Copyright © Guruji Point - Code You Want To Write