473,409 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

Inheriting from a GridView

I am attempting to better automate a Pager Template within a GridView. I am
succesfully skinning a Drop Down List withing my control (the DDL is added
to my control). I correctly populate the item list that corresponds with the
number of pages, but I am unable to wire up the Selected Index Changed
event. Auto PostBack is set to true and the page is posting back when the
DDL is selected / chaged, but the event is never being called.

Any ideas?

Secon question: how can I dynamically generate the pager template that
contains the DDL?

Thanks!

>>>>

public class DataGridView : GridView {

protected override void OnRowDataBound(GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.Pager) {
LoadPagerTemplate(e.Row);
}

base.OnRowDataBound(e);
}

private void LoadPagerTemplate(GridViewRow row) {
DropDownList dropDownListPager =
(DropDownList)row.FindControl("DropDownListPager") ;

if (dropDownListPager != null) {
for (int i = 1; i <= this.PageCount; i++) {
dropDownListPager.Items.Add(new ListItem(i.ToString(),
i.ToString()));
}

dropDownListPager.SelectedIndexChanged += new
EventHandler(DropDownListPager_SelectedIndexChange d);
}
}

public void DropDownListPager_SelectedIndexChanged(object sender,
EventArgs e) {
this.PageIndex = ((DropDownList)sender).SelectedIndex; // <- this
is never reached when I change the DDL.
this.DataBind();
}
}

Feb 12 '07 #1
5 2949
I am guessing that the failure of the even is somehow related to where I am
wiring it up. On the post back, does the control need to be populated before
the event fires? What method on the grid view should I be overriding?

-Andy
"Andrew Robinson" <ne****@nospam.nospamwrote in message
news:eU**************@TK2MSFTNGP06.phx.gbl...
>I am attempting to better automate a Pager Template within a GridView. I am
succesfully skinning a Drop Down List withing my control (the DDL is added
to my control). I correctly populate the item list that corresponds with
the number of pages, but I am unable to wire up the Selected Index Changed
event. Auto PostBack is set to true and the page is posting back when the
DDL is selected / chaged, but the event is never being called.

Any ideas?

Secon question: how can I dynamically generate the pager template that
contains the DDL?

Thanks!

>>>>>


public class DataGridView : GridView {

protected override void OnRowDataBound(GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.Pager) {
LoadPagerTemplate(e.Row);
}

base.OnRowDataBound(e);
}

private void LoadPagerTemplate(GridViewRow row) {
DropDownList dropDownListPager =
(DropDownList)row.FindControl("DropDownListPager") ;

if (dropDownListPager != null) {
for (int i = 1; i <= this.PageCount; i++) {
dropDownListPager.Items.Add(new ListItem(i.ToString(),
i.ToString()));
}

dropDownListPager.SelectedIndexChanged += new
EventHandler(DropDownListPager_SelectedIndexChange d);
}
}

public void DropDownListPager_SelectedIndexChanged(object sender,
EventArgs e) {
this.PageIndex = ((DropDownList)sender).SelectedIndex; // <-
this is never reached when I change the DDL.
this.DataBind();
}
}
Feb 12 '07 #2
Hello Andy,

From your description, you're creating a custom GridView class(derived from
built-in GridView class) and want to do some customization on the
GridView's paging. However, you're meeting a problem that the
dropdownlist's SelectedIndexChange event handler won't get executed,
correct?

Based on the code snippet you provide, it seems you still use the the
GridView's "PagerTemplate" to define your custom pager layout, correct? If
so, I think you can get the custom dropdownlist work in the Pagertemplate
and has "selectedIndexchanged" event handler registered without creating a
custom GridView class. e.g.

#Here is GridView which define a custom pager template and do the custom
paging without derived from built-in GridView class

==============aspx==================
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1" PageSize="2"
OnRowDataBound="GridView1_RowDataBound" >
<Columns>
<asp:BoundField DataField="id" HeaderText="id"
InsertVisible="False" ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="name"
SortExpression="name" />
<asp:BoundField DataField="description"
HeaderText="description" SortExpression="description" />
</Columns>
<PagerTemplate>
<div style="text-align:right">
<asp:DropDownList ID="lstPages" runat="server"
AutoPostBack="true"
OnSelectedIndexChanged="lstPages_SelectedIndexChan ged"
OnPreRender="lstPages_PreRender">
</asp:DropDownList>
</div>
</PagerTemplate>
</asp:GridView>
==========code behind====================
public partial class nav_MyGridPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Pager)
{
DropDownList lst = e.Row.FindControl("lstPages") as
DropDownList;

if (lst.Items.Count != GridView1.PageCount)
{
lst.Items.Clear();
for (int i = 1; i <= GridView1.PageCount; i++)
{
lst.Items.Add(i.ToString());
}
}
}

}

protected void lstPages_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList lst = (DropDownList)sender;

int pageindex = int.Parse(lst.SelectedValue);
Response.Write("<br/>New PageIndex: " + pageindex);

GridView1.PageIndex = lst.SelectedIndex;
GridView1.DataBind();
}

protected void lstPages_PreRender(object sender, EventArgs e)
{
DropDownList lst = (DropDownList)sender;

lst.SelectedIndex = GridView1.PageIndex;

}
}
===================================
If you still want to create a custom GridView class, I think you should put
the code( which create custom sub controls in PagerTemplate and registering
event handler dynamically) in the OnRowCreated event. This is because for
dynamic created control or event handler, they need to be created and
registered in every page request, while the "OnRowDataBound" is not
guaranteed to occur on each page reqeust. In addition, you can consider
directly hook the "InitializePager" function of the GridView class, this
is the function that do the eariliest GridView pager row's
initialization(for the Row's cells and sub controls or template ....). You
can even discard the base class's initialize codelogic and do it yourself.
e.g.

=========================
namespace ClassLibrary1
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:MyGridView runat=server></{0}:MyGridView>")]
public class MyGridView : GridView
{

protected override void InitializePager(GridViewRow row, int
columnSpan, PagedDataSource pagedDataSource)
{
base.InitializePager(row, columnSpan, pagedDataSource);

//do your customzation on the Pager Row here(or you can comment
the above base.InitializePager and completely do the pager row initalize
yourself

}

protected override void OnRowCreated(GridViewRowEventArgs e)
{
base.OnRowCreated(e);

//here is just like what you have in GridView's "RowCreated"
event handler
}

}
}
=======================================

Hope this helps some.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Feb 13 '07 #3
Steve,

Thanks for the help. I am using this gridview on potentially 100 to 200
pages so I want to include the paging functionality within an inherited
control. I will try the two events that you suggested rather than the row
binding event that I was using. Guessing this will fix things.

Thanks,

"Steven Cheng[MSFT]" <st*****@online.microsoft.comwrote in message
news:tC**************@TK2MSFTNGHUB02.phx.gbl...
Hello Andy,

From your description, you're creating a custom GridView class(derived
from
built-in GridView class) and want to do some customization on the
GridView's paging. However, you're meeting a problem that the
dropdownlist's SelectedIndexChange event handler won't get executed,
correct?

Based on the code snippet you provide, it seems you still use the the
GridView's "PagerTemplate" to define your custom pager layout, correct? If
so, I think you can get the custom dropdownlist work in the Pagertemplate
and has "selectedIndexchanged" event handler registered without creating a
custom GridView class. e.g.

#Here is GridView which define a custom pager template and do the custom
paging without derived from built-in GridView class

==============aspx==================
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1" PageSize="2"
OnRowDataBound="GridView1_RowDataBound" >
<Columns>
<asp:BoundField DataField="id" HeaderText="id"
InsertVisible="False" ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="name"
SortExpression="name" />
<asp:BoundField DataField="description"
HeaderText="description" SortExpression="description" />
</Columns>
<PagerTemplate>
<div style="text-align:right">
<asp:DropDownList ID="lstPages" runat="server"
AutoPostBack="true"
OnSelectedIndexChanged="lstPages_SelectedIndexChan ged"
OnPreRender="lstPages_PreRender">
</asp:DropDownList>
</div>
</PagerTemplate>
</asp:GridView>
==========code behind====================
public partial class nav_MyGridPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Pager)
{
DropDownList lst = e.Row.FindControl("lstPages") as
DropDownList;

if (lst.Items.Count != GridView1.PageCount)
{
lst.Items.Clear();
for (int i = 1; i <= GridView1.PageCount; i++)
{
lst.Items.Add(i.ToString());
}
}
}

}

protected void lstPages_SelectedIndexChanged(object sender, EventArgs
e)
{
DropDownList lst = (DropDownList)sender;

int pageindex = int.Parse(lst.SelectedValue);
Response.Write("<br/>New PageIndex: " + pageindex);

GridView1.PageIndex = lst.SelectedIndex;
GridView1.DataBind();
}

protected void lstPages_PreRender(object sender, EventArgs e)
{
DropDownList lst = (DropDownList)sender;

lst.SelectedIndex = GridView1.PageIndex;

}
}
===================================
If you still want to create a custom GridView class, I think you should
put
the code( which create custom sub controls in PagerTemplate and
registering
event handler dynamically) in the OnRowCreated event. This is because for
dynamic created control or event handler, they need to be created and
registered in every page request, while the "OnRowDataBound" is not
guaranteed to occur on each page reqeust. In addition, you can consider
directly hook the "InitializePager" function of the GridView class, this
is the function that do the eariliest GridView pager row's
initialization(for the Row's cells and sub controls or template ....). You
can even discard the base class's initialize codelogic and do it yourself.
e.g.

=========================
namespace ClassLibrary1
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:MyGridView runat=server></{0}:MyGridView>")]
public class MyGridView : GridView
{

protected override void InitializePager(GridViewRow row, int
columnSpan, PagedDataSource pagedDataSource)
{
base.InitializePager(row, columnSpan, pagedDataSource);

//do your customzation on the Pager Row here(or you can comment
the above base.InitializePager and completely do the pager row initalize
yourself

}

protected override void OnRowCreated(GridViewRowEventArgs e)
{
base.OnRowCreated(e);

//here is just like what you have in GridView's "RowCreated"
event handler
}

}
}
=======================================

Hope this helps some.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.



Feb 13 '07 #4
Thanks for your reply Andrew,

RowDatabound event/method will still be useful since you may need to do
some update on your pager whenver databinding occurs. Anyway, you can try
those methods first, if you meet any further problem, please feel free to
post here. I'll be glad to be of assistance.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Feb 14 '07 #5
Hello Andrew,

Have you got any further progress on this issue? If there is anything else
we can help, please feel free to followup

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.

Feb 16 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: theKirk | last post by:
using Visual Studio 2005 C# ASP.NET I know there has to be a simple way to do this....I want to use C# in a code behind for aspx. Populate a GridView from an xml file Add Fields to the...
6
by: Nalaka | last post by:
Hi, I have a gridView (grid1), which as a templateColumn. In the template column, I have put in a gridView (grid2) and a ObjectDataSource (objectDataSource2). Question is... How to I pass the...
2
by: Charles Law | last post by:
I want a set of controls that all have a border, like a group box. I thought I would create a base control containing just a group box from which my set of controls could inherit. Assuming that...
5
by: Dick | last post by:
I have a GridView bound to an ObjectDataSource. I have a Button that calls GridView.DataBind. I want the row that is selected before the DataBind to still be selected afterwards. This happens...
1
by: WebMatrix | last post by:
Hi, I started a new web application which so far has several grdiviews displaying data. I find myself reimplementing the same logic (copy/pasting really) in grid's event ItemDataBound and...
2
by: antonyliu2002 | last post by:
I've been googling for some time, and could not find the solution to this problem. I am testing the paging feature of gridview. I have a very simple web form on which the user can select a few...
0
by: =?Utf-8?B?UGhpbGlw?= | last post by:
I am referencing external controls defined in web.config system.web/pages/controls... illustrated as follows... <pages validateRequest="false" enableViewStateMac="true"...
6
by: RobertTheProgrammer | last post by:
Hi folks, Here's a weird problem... I have a nested GridView setup (i.e. a GridView within a GridView), and within the nested GridView I have a DropDownList item which has the...
3
by: Peter | last post by:
I have a GridView which is populated by List<ofObjects> Does anyone have example of how to sort the columns of this GridView? I have found examples without DataSourceControl but these use...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.