By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,563 Members | 1,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,563 IT Pros & Developers. It's quick & easy.

Inheriting from a GridView

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.