Tuesday, May 29, 2012

How to connect Database and other events in User control



 
   Before started this topic I have already  given a topic on  User Control  for a fundamental idea. Here a question arises if we will take ASP.NET controls then how to connect database and other events of those controls. We know that user control page is same as ASPX page so coding is also same.

 Step: 1

 Create a user table in our database



 Step: 2
 
   Right click on the folder(User Control) in the web site and add new item as 'WebUserControl.ascx'  and  rename as 'Search.ascx'.

  Add the following code in design page

Source Code :-


  <%@ Control Language="C#" AutoEventWireup="true" CodeFile="LedgerDB.ascx.cs" Inherits="UserControl_LedgerDB" %>
<style type="text/css">
    .style1
    {
        width: 100%;
    }
</style>


<table class="style1">
    <tr>
        <td style="text-align: right" width="40%">
            <asp:Label ID="Label1" runat="server" Text="Search"></asp:Label>
        </td>
        <td><asp:DropDownList ID="DDLUser" runat="server"
    onselectedindexchanged="DDLUser_SelectedIndexChanged" AutoPostBack="True">
    </asp:DropDownList>

            &nbsp;</td>
    </tr>
    <tr>
        <td>
            &nbsp;</td>
        <td width="60%"><asp:GridView ID="GridView1" runat="server">
</asp:GridView>
            &nbsp;</td>
    </tr>
</table>



C# Code :-



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

public partial class UserControl_LedgerDB : System.Web.UI.UserControl
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter da;

    protected void Page_Load(object sender, EventArgs e)
    {

        con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
        DDLedger.Height = m_ImageHeight;

        if(IsPostBack==false )
        {
         
            da = new SqlDataAdapter("select username,userid from users",con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataTable dt = ds.Tables[0];
             DDLUser .DataSource = dt;
             DDLUser .DataTextField = "username";
             DDLUser .DataValueField = "userid";
             DDLUser .DataBind();
        }
    }


    protected void DDLUser_SelectedIndexChanged(object sender, EventArgs e)
    {
        da = new SqlDataAdapter("select * from users where ledgerid='"+  DDLUser .SelectedValue +"'", con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DataTable dt = ds.Tables[0];
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
 
}


Step:3


   After creating this user control right click on the folder(My Project) and add new item as 'Default.aspx'   and  rename as 'MyPage2.aspx'. Then register the user control page in this page as

 <%@ Register TagPrefix="User" TagName="Username" Src="~/UserControl/ Search.ascx" %>
   

   then  add   this  control anywhere in the page as

       < User : Username  ID="username" runat="server" Visible="True"  />

Step:4


Run the page and on selected index change of user we can able to see the details.









No comments:

Post a Comment