避免在asp.net C#下拉列表更改回发

时间:2014-11-21 05:42:29

标签: c# jquery asp.net gridview

我已经使用下拉列表嵌套了gridview。当我更改父网格的下拉框时,Child gris&d; d下拉框应该会自动更改。但由于postpack问题执行所有操作后,子网格隐藏...

我的代码:

<asp:GridView ID="gvParentMenu" Width="100%" runat="server" BorderStyle="Solid" BorderColor="#000" BorderWidth="1px" GridLines="None" DataKeyNames="menu_id"  AutoGenerateColumns="false" OnPageIndexChanged="gvParentMenu_PageIndexChanged" OnPageIndexChanging="gvParentMenu_PageIndexChanging" OnRowCancelingEdit="gvParentMenu_RowCancelingEdit" OnRowCommand="gvParentMenu_RowCommand" OnRowCreated="gvParentMenu_RowCreated" OnRowDataBound="gvParentMenu_RowDataBound" OnRowDeleted="gvParentMenu_RowDeleted" OnRowDeleting="gvParentMenu_RowDeleting" OnRowEditing="gvParentMenu_RowEditing" OnRowUpdated="gvParentMenu_RowUpdated" OnRowUpdating="gvParentMenu_RowUpdating">
<HeaderStyle BackColor="#76C4ED" Font-Bold="true" ForeColor="White" BorderColor="#76C4ED" />
<Columns>

<asp:TemplateField ItemStyle-Width="20px">
<ItemTemplate>
<a href="JavaScript:divexpandcollapse('div<%# Eval("menu_id") %>');">
<img id="imgdiv<%# Eval("menu_id") %>" border="0" src="common/img/button-plus-icon.png" />
</a>
</ItemTemplate>
</asp:TemplateField>

<asp:BoundField DataField="menu_id" HeaderText="Menu Id" Visible="false" />
<asp:BoundField DataField="menu_desc" HeaderText="Menu Name" />
<asp:TemplateField HeaderText="Rights">
<ItemTemplate>

<asp:DropDownList ID="drpRights"  runat="server" Width="100%" AutoPostBack="true" SelectedValue='<%# Bind("rights") %>' OnSelectedIndexChanged="drpRights_SelectedIndexChanged">
<asp:ListItem Text="Select" Value="0"></asp:ListItem>
<asp:ListItem Text="View" Value="1"></asp:ListItem>
<asp:ListItem Text="View & Insert" Value="2"></asp:ListItem>
<asp:ListItem Text="View & Insert & Edit" Value="3"></asp:ListItem>
<asp:ListItem Text="View & Insert & Edit & Delete" Value="4"></asp:ListItem>
<asp:ListItem Text="View & Insert & Edit & Delete & Report" Value="5"></asp:ListItem>
<asp:ListItem Text="Report" Value="6"></asp:ListItem>
</asp:DropDownList>
<asp:HiddenField ID="hdnParGridId" runat="server" Value='<%#Eval("menu_id")%>' />     
</ItemTemplate>
</asp:TemplateField>


<asp:TemplateField ItemStyle-BorderColor="White" ItemStyle-BorderStyle="None" ItemStyle-BorderWidth="0px">
<ItemTemplate>
<tr >
<td colspan="100%">


<div id="div<%# Eval("menu_id") %>" style="display: none; position: relative; left: 28px; overflow: auto" class="table-responsive">


<asp:GridView ID="gvChildGrid" runat="server" AutoGenerateColumns="false" BorderStyle="Solid" BorderColor="#000" BorderWidth="1px" GridLines="None" Width="70%">
    <HeaderStyle BackColor="#76C4ED" Font-Bold="true" ForeColor="White" />
    <HeaderStyle BackColor="#76C4ED" Font-Bold="true" ForeColor="White" />
    <Columns>
        <asp:BoundField DataField="menu_id" HeaderText="Menu Id" HeaderStyle-HorizontalAlign="Left" Visible="false" />
        <asp:BoundField DataField="menu_desc" HeaderText="Menu Name" HeaderStyle-HorizontalAlign="Left" />

        <asp:TemplateField HeaderText="Rights">
            <ItemTemplate>

                <asp:DropDownList ID="drpChildRights"  runat="server" Width="90%" SelectedValue='<%# Bind("rights") %>'>
                    <asp:ListItem Text="Select" Value="0"></asp:ListItem>
                    <asp:ListItem Text="View" Value="1"></asp:ListItem>
                    <asp:ListItem Text="View & Insert" Value="2"></asp:ListItem>
                    <asp:ListItem Text="View & Insert & Edit" Value="3"></asp:ListItem>
                    <asp:ListItem Text="View & Insert & Edit & Delete" Value="4"></asp:ListItem>
                    <asp:ListItem Text="View & Insert & Edit & Delete & Report" Value="5"></asp:ListItem>
                    <asp:ListItem Text="Report" Value="6"></asp:ListItem>

                </asp:DropDownList>
                    <asp:HiddenField ID="hdnChildGridId" runat="server" Value='<%#Eval("menu_id")%>' /> 
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<%--    <br />--%>
</div>


</td>
</tr>
</ItemTemplate>
</asp:TemplateField>

</Columns>
</asp:GridView>

C#代码在这里:

 protected void drpRights_SelectedIndexChanged(object sender, EventArgs e)
        {


                try
                {
                    int index = ((sender as DropDownList).NamingContainer as GridViewRow).RowIndex;
                    DropDownList drp = (DropDownList)gvParentMenu.Rows[index].FindControl("drpRights");
                    GridView gvChild = (GridView)gvParentMenu.Rows[index].FindControl("gvChildGrid");

                    gvChild.Visible = true;
                    foreach (GridViewRow row in gvChild.Rows)
                    {
                        if (row.RowType == DataControlRowType.DataRow)
                        {
                            DropDownList drpChild = (DropDownList)row.FindControl("drpChildRights");

                            if (drp.SelectedItem.Value == "0")
                            {
                                drpChild.SelectedItem.Value = "0";
                            }
                            else if (drp.SelectedItem.Value == "1")
                            {
                                drpChild.SelectedItem.Value = "1";
                            }
                            else if (drp.SelectedItem.Value == "2")
                            {
                                drpChild.SelectedItem.Value = "2";
                            }
                            else if (drp.SelectedItem.Value == "3")
                            {
                                drpChild.SelectedItem.Value = "3";
                            }
                            else if (drp.SelectedItem.Value == "4")
                            {
                                drpChild.SelectedItem.Value = "4";
                            }
                            else if (drp.SelectedItem.Value == "5")
                            {
                                drpChild.SelectedItem.Value = "5";
                            }
                            else if (drp.SelectedItem.Value == "6")
                            {
                                drpChild.SelectedItem.Value = "6";
                            }

                        }
                    }


                }
                catch (Exception ex)
                {

                }

        }

我的screeshot:

1 个答案:

答案 0 :(得分:0)

哦!那是

您正在使用div隐藏子网格。相反,您可以直接设置子网格可见性属性。

所以你可以这样做

  1. 从子网格div中删除display:none

    <div id="div<%# Eval("menu_id") %>" style=" position: relative; left: 28px; overflow: auto" class="table-responsive">

  2. 直接向子网格提供visibilty = false

    <asp:GridView ID="gvChildGrid" runat="server" AutoGenerateColumns="false" BorderStyle="Solid" BorderColor="#000" BorderWidth="1px" GridLines="None" Width="70%" Visible="false">
    

    ...

  3. 现在您可以使用

    GridView gvChild = (GridView)gvParentMenu.Rows[index].FindControl("gvChildGrid"); gvChild.Visible = true;

  4. 用于从代码中看到子网格。

    (请管理你的JS隐藏显示div它不起作用,你可以使用简单的JQuery来切换div)