异步回发后控制位置发生变化

时间:2016-08-11 19:45:00

标签: c# asp.net asynchronous updatepanel

我的页面中的更新面板中有一个面板 默认情况下,面板的可见性为false,异步回发后更改为true 我的问题是在面板的回发位置更改到页面顶部之后 这是我的代码

<asp:Label runat="server" ID="lbl_caption_upload" Text="caption" CssClass="label"></asp:Label>
<asp:TextBox runat="server" ID="txt_caption_upload" TextMode="MultiLine" CssClass="textbox"></asp:TextBox>
<asp:RadioButtonList runat="server" ID="radio_view_upload" OnSelectedIndexChanged="radio_view_upload_SelectedIndexChanged" AutoPostBack="True">
   <asp:ListItem text="custom"></asp:ListItem>
   <asp:ListItem text="public"></asp:ListItem></asp:RadioButtonList>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="radio_view_upload" />
                </Triggers>
                <ContentTemplate>
                    <asp:Panel runat="server" ID="placeholder_panel" Visible="false" CssClass="hide">
                        <tr>
                            <td>
                                <asp:Label runat="server" ID="lbl_genre_upload" Text="genre" CssClass="label"></asp:Label>
                            </td>
                            <td>
                                <asp:CheckBoxList runat="server" ID="check_genre_upload" DataTextField="Title" DataValueField="ID" RepeatColumns="7" RepeatDirection="Horizontal" DataSourceID="datasource_genre_upload"></asp:CheckBoxList>
                                <asp:LinqDataSource ID="datasource_genre_upload" runat="server" ContextTypeName="Video_Hosting.L2SDataContext" EntityTypeName="" Select="new (Title, ID)" TableName="Genres" OrderBy="ID">
                                </asp:LinqDataSource>
                            </td>
                        </tr>
                    </asp:Panel>
                </ContentTemplate>
            </asp:UpdatePanel>

这里是slectedindex change的代码

protected void radio_view_upload_SelectedIndexChanged(object sender, EventArgs e)
    { 
        if (radio_view_upload.SelectedIndex == 1)
        {
            placeholder_panel.Visible = true;
        } 
        else
        {
            placeholder_panel.Visible = false;
        }
    }

在面板的回发位置更改为第一个标签之前

感谢您的所有答案

1 个答案:

答案 0 :(得分:0)

这一行是问题所在:

if (radio_view_upload.SelectedIndex == 2)

您的radio_view_upload RadioButtonList只有2个选项,因此您的if语句将始终转到Else部分。只有SelectedIndex 0和1可用。

在大多数编程语言中,数组,集合,列表等中的第一项将以0作为起始索引。

您也可以使用if (radio_view_upload.SelectedValue == "myValue"),但要实现此功能,您必须向ListItems添加value="myValue"

<强>更新

<ContentTemplate>内,您有一个<tr> <td>和一个</td> </tr>没有匹配的<table> </table>标记。也许这些标签位于UpdatePanel之外,并且在您的代码段中不可见。您应该在UpdatePanel内添加/移动它们。

更新2

    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <br />
    <br />
    <asp:Label runat="server" ID="lbl_caption_upload" Text="caption" CssClass="label"></asp:Label>
    <br />
    <asp:TextBox runat="server" ID="txt_caption_upload" TextMode="MultiLine" CssClass="textbox"></asp:TextBox>
    <br />
    <asp:RadioButtonList runat="server" ID="radio_view_upload" OnSelectedIndexChanged="radio_view_upload_SelectedIndexChanged" AutoPostBack="True">
        <asp:ListItem Text="custom"></asp:ListItem>
        <asp:ListItem Text="public"></asp:ListItem>
    </asp:RadioButtonList>
    <br />

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="radio_view_upload" />
        </Triggers>
        <ContentTemplate>
            <asp:Panel runat="server" ID="placeholder_panel" Visible="true" CssClass="hide">
                <asp:Label runat="server" ID="lbl_genre_upload" Text="genre" CssClass="label"></asp:Label>
                <br />
                <asp:CheckBoxList runat="server" ID="check_genre_upload" DataTextField="Title" DataValueField="ID" RepeatColumns="7" RepeatDirection="Horizontal"></asp:CheckBoxList>
            </asp:Panel>
        </ContentTemplate>
    </asp:UpdatePanel>