I am having a DropdownLIst in the HeaderTemplate of the Grid I have written some ser开发者_如何学JAVAver side code on selectedINdexChanged event of this dropdown But this event never fires. I have also Enabled the ViewState of dropdown and the Page to true Ant ide what must be the Problem
I have solved this problem in my environment.... Please check out below code..
This is my gridview
in aspx page.
<asp:GridView ID="grvGrid" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="CustomerID" OnRowDataBound="grvGrid_RowDataBound">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True"
SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName"
SortExpression="CompanyName" />
<asp:TemplateField>
<HeaderTemplate>
<%--<asp:Label ID="lblMon1" runat="server"></asp:Label>--%>
<asp:DropDownList id="ddlMon" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlMon_SelecdtedIndexChanges">
<asp:ListItem Text="1" Value="1" Selected="True">1</asp:ListItem>
<asp:ListItem Text="2" Value="2">2</asp:ListItem>
<asp:ListItem Text="3" Value="3">3</asp:ListItem>
</asp:DropDownList>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblblbl" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Get Grid View Bind in pageload event
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GetTable();
grvGrid.DataSource = dstable;
grvGrid.DataBind();
}
}
Find DropDown control and bind it's event
protected void grvGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (Page.IsPostBack)
{
if (e.Row.RowType == DataControlRowType.Header)
{
DropDownList ddlmon = e.Row.FindControl("ddlMon") as DropDownList;
ddlmon.SelectedIndexChanged += new EventHandler(ddlMon_SelecdtedIndexChanges);
}
}
}
DropDown SelectedIndex Changes Event
protected void ddlMon_SelecdtedIndexChanges(object sender, EventArgs e)
{
// Your Code paste here
}
Possible problem here is your DropDownList
might not set the property
AutoPostBack="true" try add this to your `DropDownList`
精彩评论