Question

In my website I have a dropdown list with 2 values. I am trying to write this code to hide textbox by onselectIndexChange event without refreshing the page. for this goal I used Update panel but on dropDownList select there is no change in textboxs visibility. my codes:

<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 file:

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;
    }
}
Was it helpful?

Solution

Try Like This:

Use your All code Inside Update Panels

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

// All Controls Placed Here

</ContentTemplate><Triggers>

</Triggers>  </asp:UpdatePanel>

By default it will use Asynchronous postback Code:

                <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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top