Wednesday, June 27, 2012

Gridview User Control data manipulation In ASPX page


  Sometimes we create user control to reuse in our project in different pages means to access the common functionality.Now the difficult thing is to manipulate the data of that user control in our ASPX page.

  here is a simple demo on this topic



  Step-1

   create a table according to your requirement.here the Fms_Demo table is below

     Fms_Damo

     Roll   int  (PK,Identity)
     Age   int
     Name   varchar(50)

 Step-2

   Right click on Project -> Add NewItem -> Web User Control -> Rename the control as 'EventInASPXpage.ascx' and press Ok

Design View :

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

<table class="style1">
    <tr>
        <td>
            &nbsp;</td>
        <td>
            &nbsp;</td>
    </tr>
    <tr>
        <td>
            &nbsp;</td>
        <td>
            &nbsp;</td>
    </tr>
    <tr>
        <td>
            &nbsp;</td>
        <td>
            &nbsp;</td>
    </tr>
    <tr>
        <td>
            &nbsp;</td>
        <td>
            &nbsp;</td>
    </tr>
    <tr>
     
        <td colspan="2">
            <asp:GridView ID="GvUser"  runat="server" onrowcommand="GvUser_RowCommand"
                AutoGenerateColumns="False">
                <Columns>
                    <asp:TemplateField HeaderText="Link">
                        <ItemTemplate>
                            <asp:LinkButton ID="LnkUser" CommandArgument='<%#Eval("Roll") %>' runat="server">LinkButton</asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="name" HeaderText="Name" />
                    <asp:BoundField DataField="Roll" HeaderText="Roll" />
                </Columns>
            </asp:GridView>
            &nbsp;</td>
    </tr>
    <tr>
        <td>
            <asp:Button ID="BtnUserButton" runat="server"
                Text="UserControl Save Button" onclick="BtnUserButton_Click" />
        </td>
        <td>
            &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.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class UserControl_EventInASPXpage : System.Web.UI.UserControl
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataReader dr;
    SqlDataAdapter da;
    DataSet ds;

// Here we have declare event handler to fire event in ASPX page for the respective control. EventHandler is  used for whole control and CommandEventHandler is used for part of a control.


    public event EventHandler buttonClick;
    public event CommandEventHandler linkButtonClick;
 
 

    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
        con.Open();
        cmd = new SqlCommand("select * from fms_demo", con);
        dr = cmd.ExecuteReader();
        GvUser.DataSource = dr;
        GvUser.DataBind();
        con.Close();
    }
 
    protected void BtnUserButton_Click(object sender, EventArgs e)
    {
      buttonClick(Events, e);
    }
    protected void GvUser_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        linkButtonClick(Events, e);
    }
 
}

Step-3


  Right click on Project -> Add NewItem -> Web Form -> Rename the form as 'FrmUserControlEvent.aspx' and press Ok

   Design View

  <%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="FrmUserControlEvent.aspx.cs" Inherits="FrmUserControlEvent" %>
  <%@ Register Src="~/UserControl/EventInASPXpage.ascx" TagPrefix="UserEvent" TagName="UserControl" %>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">  
 
    <asp:UpdatePanel ID="UpdatePanel1" runat="server"  >
        <ContentTemplate>
            <userevent:usercontrol ID="UserControlButton1"  runat="server" Visible="true"  />

    <asp:Panel ID="Panel1" runat="server">
     <table class="style1">
        <tr>
            <td class="style2">
                </td>
            <td class="style2">
                <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
            </td>
        </tr>
        <tr>
            <td style="text-align: right" width="40%">
                <asp:Label ID="Label1" runat="server" Text="Roll"></asp:Label>
            </td>
            <td width="60%">
                <asp:TextBox ID="TxtRoll" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="text-align: right" width="40%">
                <asp:Label ID="Label2" runat="server" Text="Age"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="TxtAge" runat= "server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="text-align: right">
                <asp:Label ID="Label3" runat="server" Text="Name"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="TxtName" runat="server"></asp:TextBox>
            </td>
        </tr>
    </table>

    </asp:Panel>
        </ContentTemplate>    
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="UserControlButton1" />
    </Triggers>
    </asp:UpdatePanel>
   
</asp:Content>

<asp:Content ID="Content3" runat="server" contentplaceholderid="HeadContent">
    <style type="text/css">
        .style2
        {
            height: 21px;
        }
    </style>
</asp:Content>

  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.Data;
using System.Data.SqlClient;
using System.Configuration;


public partial class FrmUserControlEvent : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataReader dr;
 

    protected void Page_Load(object sender, EventArgs e)
    {
       con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);

      UserControlButton1.buttonClick +=new EventHandler(UserControlButton1_buttonClick);
      UserControlButton1.linkButtonClick +=new CommandEventHandler(UserControlButton1_linkButtonClick);
   
    }
    protected void UserControlButton1_linkButtonClick(object sender, CommandEventArgs e)
    {
     
 
        con.Open();
        cmd = new SqlCommand("select name,age from fms_demo where roll='"+ e.CommandArgument.ToString() +"'",con);
        dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            TxtRoll.Text=e.CommandArgument.ToString();
            TxtName.Text = dr[0].ToString();
            TxtAge.Text = dr[1].ToString();
        }
        con.Close();
     
     

    }
    protected void UserControlButton1_buttonClick(object sender, EventArgs e)
    {
        try
        {

            con.Open();
            string ins="update fms_demo set name='"+ TxtName.Text +"',age='"+ TxtAge.Text +"' where roll='"+ TxtRoll.Text +"'";
            cmd = new SqlCommand(ins,con);
            cmd.ExecuteNonQuery();
            con.Close();
            Label4.Text = "Successfully Updated.................";
// To reload the page
            ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "Script", "window.location.reload()", true);
         
        }
        catch (Exception ex)
        {
            Label4.Text = ex.Message.ToString();
        }

    }   
 
}







     

No comments:

Post a Comment