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.