Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have created a simple gridview usercontrol. I have bind it and displaying in my aspx page.
How to access the existing properties and event? Those are not coming in property of the user control in aspx page.
This is my first time working on ascx page.. please help me with steps to create a gridview user control with all its existing events and properties.

Below is my ascx.cs page :

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class GridControll : System.Web.UI.UserControl
{
    //public event EventHandler PageTitleUpdated;
    public delegate void RowDataBound(object sender, GridViewRowEventArgs e);
    public event RowDataBound GridRowDataBound;
    protected void Page_Load(object sender, EventArgs e)
    {
        BindGrid();
    }
    public void BindGrid()
    {

        string strCon = System.Configuration.ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
        string strSql = "select CountryCode,CountryName,CountryLocale,Status from countrymaster";
        SqlConnection con = new SqlConnection(strCon);
        con.Open();
        SqlDataAdapter dadapter = new SqlDataAdapter();
        dadapter.SelectCommand = new SqlCommand(strSql, con);
        DataSet dset = new DataSet();
        dadapter.Fill(dset);
        con.Close();
        this.GridView1.DataSource = dset;
        GridView1.DataBind();

    }
    /// <summary>
    ///get grid columns collection
    /// </summary>
    public DataControlFieldCollection Columns
    {
        get { return GridView1.Columns; }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        OnRowDataBound(e);
    }
    protected void OnRowDataBound(GridViewRowEventArgs e)
    {
        if (GridRowDataBound != null)
            GridRowDataBound(this, e);
    }

}


Below is my aspx page

XML
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagPrefix="UG" TagName="GridControl" Src="~/GridControll.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <UG:GridControl ID="GridControl1" runat="server"></UG:GridControl>
    </div>

    </form>
</body>
</html>

Thanks
Abhineet
Posted
Updated 28-Jun-11 6:04am
v2
Comments
BobJanova 28-Jun-11 11:04am    
You are not rendering any output. Did you mean to inherit from an existing grid control, instead of UserControl?
Sergey Alexandrovich Kryukov 28-Jun-11 11:16am    
What events and properties do you call "existing"? Inherited? Available in child control you use in your user control? Why accessing them is a problem? Do you want to access them inside you implementation or outside?
--SA
Abhineetk 29-Jun-11 1:45am    
Thanks for yr reply..
Actually I need to create a tool for grid view with all existing features available in gridview.. plus some new features that i need to add as per my requirement.. like paging, sorting, scroll, styles, and most important static column
Sergey Alexandrovich Kryukov 29-Jun-11 5:08am    
Thank you for explanation, now I see. Then you cannot ask "How to access the existing properties and event?" You just need to create them.
--SA
Abhineetk 29-Jun-11 6:53am    
PLease give me some sample for this

1 solution

Looking at you ascx.cs file, i put some of the assumption before geting to answer
1. In your ascx file you have the GridView1 as child control of GridControll.
2. Expecting all the properties and event of GridView1 to be avalialbe on GridControl1.

To achieve this there are different ways:

Option 1:
Inherit your user control from GridView and all the properties and events would be naturally available.

Option 2:
Expose the required properties and events on the GridControl1 similar to you have done for Columns property.

Option 3:
Make GridView1 as public in your usercontrol and access the properties and event you want as GridControll.GridView1.<propertyname>.
 
Share this answer
 
Comments
Abhineetk 29-Jun-11 1:50am    
vivek.. yr option 2 and option 3 seems good to work on.. but can u help me with some sample how to make it..or if u can edit my code to help me.

Thanks
Sergey Alexandrovich Kryukov 29-Jun-11 5:09am    
Reasonable, a 5.
--SA
Abhineetk 29-Jun-11 6:53am    
Please give me some sample for this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900