不会为子网格视图中的Dropdownlist控件触发SelectedIndexChanged事件

时间:2014-08-05 05:32:27

标签: c# asp.net gridview

我有父网格视图和子网格视图,在子网格视图中我有一个下拉列表,我需要为该下拉列表触发onselectedindexchanged事件,但它并不是因为它存在于子网格视图中。如果我将该下拉列表放在父网格视图中,那么selectedindexchanged正在触发,但我想将该下拉列表放在子gridview中,并且当用户更改下拉列表值时需要触发onselectedindexchanged事件。在下面的gridview图像中,您可以在子gridview中看到Status列。它包含下拉列表,我想在后面的代码中触发该下拉列表选中的索引更改事件。

enter image description here

<asp:GridView ID="gvCountry" runat="server" AutoGenerateColumns="false">
 <Columns>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <img alt="" id="imgsymbol" style="cursor: pointer" src="images/plus.png" />
                                    <asp:Panel ID="pnlOrders" runat="server" Style="display: none">
                                        <asp:GridView ID="gvBrokerdetails" runat="server" AutoGenerateColumns="false" CssClass="mGrid" OnRowCommand="gvBrokerdetails_RowCommand"
                                            AlternatingRowStyle-CssClass="alt" PagerStyle-CssClass="pgr" GridLines="None" AllowPaging="true">
                                            <Columns>
                                                <asp:BoundField DataField="BrokerName" HeaderText="Name" />
                                                <asp:TemplateField HeaderText="Status">
                                                    <ItemTemplate>
                                                        <asp:DropDownList ID="ddlStatus" runat="server" OnSelectedIndexChanged="ddlStatus_SelectedIndexChanged" AutoPostBack="true">
                                                            <asp:ListItem Text="Include" Value="0"></asp:ListItem>
                                                            <asp:ListItem Text="Exclude" Value="1"></asp:ListItem>
                                                        </asp:DropDownList>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Delete">
                                                    <ItemTemplate>
                                                        <asp:ImageButton ID="imgBtnDeleteRow" runat="server" Width="25px" Height="25px" CommandName="CrossImageButton" CommandArgument='<%#Eval("BrokerId")%>' ImageUrl="~/images/cross-button.png" />
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                            </Columns>
                                        </asp:GridView>
                                    </asp:Panel>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="CountryName" HeaderText="Country Name" />
                        </Columns>
                    </asp:GridView>

代码隐藏:

public void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

但以上是事件不是在呼唤。是否有任何其他进程要实现,或者我需要在代码中指定任何新属性。

1 个答案:

答案 0 :(得分:0)

在父网格视图的RowDataBound事件中找到gridview

protected void gvCountry_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        GridView mygrid = e.Row.FindControl("gvBrokerdetails") as GridView;
    }
}

处理mygrid的事件,它是您的子网格,您可以处理您想要的任何事件或为其分配任何属性。 Selected_IndexChanged的示例位于

之下
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        GridView mygrid = e.Row.FindControl("gvBrokerdetails") as GridView;
        mygrid.SelectedIndexChanged += new EventHandler(mygrid_SelectedIndexChanged);
    }
}

void mygrid_SelectedIndexChanged(object sender, EventArgs e)
{
     // Write your code here
}