DropDownList回发问题

时间:2011-12-21 12:36:58

标签: javascript asp.net autopostback

我在usercontrol中有一个DropDownList,它本身就在一个usercontrol中。

我遇到的问题是,当有人从DDL中选择一个项目时,我需要触发一个回发,这样我就可以在内部用户控件上更新一些文本框。

但是如果我使用autopostback,我会得到一个JS错误“无法将焦点移动到控件上,因为它是不可见的,未启用的,或者是不接受焦点的类型。”

有人对此有任何想法吗?驱使我循环......

       <asp:MultiView ID="mvInvoiceItem" runat="server" ActiveViewIndex="0">
        <asp:View ID="vwInvoiceItemList" runat="server">
            <asp:LinkButton ID="btn_AddInvoiceItem" runat="server" 
                onclick="btn_AddInvoiceItem_Click">Add Part</asp:LinkButton>
            <asp:GridView ID="gvwInvoiceItems" runat="server" AutoGenerateColumns="False" 
                EnableModelValidation="True" 
                OnRowCommand="gvwInvoiceItems_RowCommand"
                DataKeyNames="Id" 
                AllowPaging="True" Width="750px" PageSize="3" >
                <Columns>
                    <asp:BoundField DataField="Id" HeaderText="1" itemstyle-cssclass="invisibleColumn" >
                        <HeaderStyle CssClass="invisibleColumn" />
                        <ItemStyle CssClass="invisibleColumn" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                    <asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="Quantity" />
                    <asp:BoundField DataField="IsSupplied" HeaderText="IsSupplied" SortExpression="IsSupplied" />
                    <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
                    <asp:BoundField DataField="Cost" HeaderText="Cost" SortExpression="Cost" />
                    <asp:ButtonField ButtonType="Image" ImageUrl="~/Images/btn_select.png" CommandName="SelectRow" Text="Select" />
                    <asp:ButtonField ButtonType="Image" ImageUrl="~/Images/btn_remove.png" CommandName="RemoveRow" Text="Remove" />
                </Columns>
            </asp:GridView>
        </asp:View>
        <asp:View ID="vwInvoiceItemEdit" runat="server">
            <table>
                <tr>
                    <td class="style2">
                        <asp:Label ID="lbl_InvoiceItem" runat="server" 
                            Text="Part.."></asp:Label>
                    </td>
                    <td>
                        <asp:DropDownList ID="dd_InvoiceItem" runat="server" 
                            DataTextField="Name" DataValueField="Id" Width="215px" AutoPostBack="true"
                            OnSelectedIndexChanged="dd_InvoiceItem_SelectedIndexChanged">
                        </asp:DropDownList>
                    </td>
                </tr>
                <tr>
                    <td class="style2">
                        <asp:Label ID="lbl_InvoiceItemQuantity" runat="server" Text="Quantity.."></asp:Label>
                    </td>
                    <td width="250px">
                        <asp:TextBox ID="txt_InvoiceItemQuantity" runat="server" MaxLength="100" 
                            Width="190px" autocomplete="off"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="style2">
                        <asp:Label ID="lbl_InvoiceItemPrice" runat="server" Text="Price.."></asp:Label>
                    </td>
                    <td width="250px">
                        <asp:TextBox ID="txt_InvoiceItemPrice" runat="server" MaxLength="100" 
                            Width="190px" autocomplete="off"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="style2">
                        <asp:Label ID="lbl_InvoiceItemIsSupplied" runat="server" Text="Supplied.."></asp:Label>
                    </td>
                    <td>
                        <asp:CheckBox ID="chk_InvoiceItemIsSupplied" runat="server"/>
                    </td>
                </tr>
                <tr>
                    <td class="style2">
                        <asp:Label ID="lbl_InvoiceItemCost" runat="server" Text="Cost.."></asp:Label>
                    </td>
                    <td width="250px">
                        <asp:TextBox ID="txt_InvoiceItemCost" runat="server" MaxLength="100" 
                            Width="190px" autocomplete="off"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="style2">
                        &nbsp;</td>
                    <td align="right">
                        <asp:ImageButton ID="btn_InvoiceItemSave" runat="server" 
                            ImageUrl="~/Images/btn_save.png" onclick="btn_InvoiceItemSave_Click" />
                        &nbsp;<asp:ImageButton ID="btn_InvoiceItemCancel" runat="server" 
                            ImageUrl="~/Images/btn_cancel.png" onclick="btn_InvoiceItemCancel_Click" CausesValidation="false" />
                    </td>
                </tr>
            </table>
        </asp:View>
    </asp:MultiView>

内部用户控件的Page_Load中没有任何内容,它通过调用InvoiceId和PopulateGrid从外部初始化。

1 个答案:

答案 0 :(得分:2)

这很奇怪。经过进一步调查,我发现有人已将此解决方案应用于焦点问题http://couldbedone.blogspot.com/2007/08/restoring-lost-focus-in-update-panel.html

<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
    <Scripts>
        <asp:ScriptReference Path="~/Resources/Focus.js" />
    </Scripts>
</asp:ToolkitScriptManager>

我的问题的解决方案是从托管ASPX页面上的ScriptManager中删除对此JS的引用。