通过selectedIndexChanged事件隐藏文本框

时间:2014-03-31 08:23:23

标签: c# asp.net updatepanel

在我的网站上,我有一个包含2个值的下拉列表。我正在尝试编写此代码以通过onselectIndexChange事件隐藏文本框而不刷新页面。 为了这个目标我使用了Update面板但是在dropDownList上选择文本框可见性没有变化。 我的代码:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<table dir="rtl">
    <tr>
        <td>
            <asp:Label ID="Label1" runat="server" Text="شخص :"></asp:Label></td>
        <td>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:DropDownList ID="DropDownList1" EnableViewState="true" AutoPostBack="true" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                        <asp:ListItem Selected="True">حقیقی</asp:ListItem>
                        <asp:ListItem>حقوقی</asp:ListItem>
                    </asp:DropDownList>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
                </Triggers>
            </asp:UpdatePanel>
        </td>
    </tr>
    <asp:Panel ID="pnlname" runat="server">
        <tr>
            <td>
                <asp:Label ID="Label2" runat="server" Text="نام و نام خانوادگی : "></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtname" runat="server" Width="415px"></asp:TextBox>
            </td>
        </tr>
    </asp:Panel>
    <asp:Panel ID="pnlMname" runat="server">
        <tr>
            <td>
                <asp:Label ID="Label3" runat="server" Text="نام و نام خانوادگی مسئول : "></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtmname" Width="415px" runat="server"></asp:TextBox>
            </td>
        </tr>
    </asp:Panel>
    <tr>
        <td>
            <asp:Label ID="Label4" runat="server" Text="شماره قرارداد : "></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="txtIdgharardad" Width="415px" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="Label7" runat="server" Text="علت درخواست اعزام کارشناس : "></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="txtellat" Width="415px" runat="server" Height="194px" TextMode="MultiLine"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="Label5" runat="server" Text="شماره همراه : "></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="txtNumber" Width="415px" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="Label6" runat="server" Text="شماره ثابت : "></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="txtSnumber" Width="415px" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="ثبت درخواست" />
        </td>
        <td>&nbsp;</td>
    </tr>
</table>

.cs文件:

protected void Page_Load(object sender, EventArgs e)
{
    pnlMname.Visible = false;
}

protected void Button1_Click(object sender, EventArgs e)
{

}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (DropDownList1.SelectedValue == "حقیقی")
    {
        pnlMname.Visible = false;
        pnlname.Visible = true;
    }
    else if (DropDownList1.SelectedValue == "حقوقی")
    {
        pnlname.Visible = false;
        pnlMname.Visible = true;
    }
}

1 个答案:

答案 0 :(得分:1)

尝试这样:

使用“更新面板内的所有代码”

  <asp:UpdatePanel ID="UpdatePanel1" runat="server">
               <ContentTemplate>

// All Controls Placed Here

</ContentTemplate><Triggers>

</Triggers>  </asp:UpdatePanel>

默认情况下,它将使用异步回发 码:                                                                           

                <asp:Label ID="Label1" runat="server" Text="شخص :"></asp:Label></td>
            <td> 
                <asp:DropDownList ID="DropDownList1" EnableViewState="true" AutoPostBack="true" runat="server"  OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                    <asp:ListItem Selected="True">حقیقی</asp:ListItem>
                    <asp:ListItem>حقوقی</asp:ListItem>
             </asp:DropDownList>
          </td>

        </tr>

                <asp:Panel ID="pnlname" runat="server">
        <tr>
            <td>
                <asp:Label ID="Label2" runat="server" Text="نام و نام خانوادگی : "></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtname" runat="server" Width="415px"></asp:TextBox>
            </td>
        </tr></asp:Panel>
        <asp:Panel ID="pnlMname" runat="server">
        <tr>
            <td>
                <asp:Label ID="Label3" runat="server"  Text="نام و نام خانوادگی مسئول : "></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtmname" Width="415px" runat="server"></asp:TextBox>
            </td>
        </tr></asp:Panel>
        <tr>
            <td>
                <asp:Label ID="Label4" runat="server" Text="شماره قرارداد : "></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtIdgharardad" Width="415px" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label7" runat="server" Text="علت درخواست اعزام کارشناس : "></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtellat" Width="415px" runat="server" Height="194px" TextMode="MultiLine"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label5" runat="server" Text="شماره همراه : "></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtNumber" Width="415px" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label6" runat="server" Text="شماره ثابت : "></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtSnumber" Width="415px" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="ثبت درخواست" />
            </td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
     </ContentTemplate><Triggers>

                            </Triggers>  </asp:UpdatePanel>