Swapna
Swapna

Reputation: 27

Use viewstate for Retaining the password in asp.net

I have a form in which i am using input field with name Password1 . after postback i need to maintain the value in password using viewstate . How can i do it in ASP.NET .My code is for a registration form in which i have given a password field .i have given auto postback for the dropdown list . when the auto postback occurs the password field is getting empty. instead of that i need to maintain the value of the password as it is.

<div>
    <%--<asp:ScriptManager runat="server"></asp:ScriptManager>--%>
    <ajaxToolkit:ToolkitScriptManager runat="server">
       </ajaxToolkit:ToolkitScriptManager>
   <h1 style="color:bisque;text-align:center;">USER FORM REGISTRATION</h1>

    <table class="auto-style1" style="color:crimson">
        <tr>
            <td class="auto-style2">First Name</td>
            <td class="auto-style3">
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>     
             </td>
        </tr>
        <tr>
            <td class="auto-style2">Last Name</td>
            <td class="auto-style3">
                <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
         </td>
        </tr>
             <tr>
            <td class="auto-style2">Password</td>
            <td class="auto-style3">
                <input id="Password1" type="password" runat="server"/></td>
        </tr>
        <tr>
            <td class="auto-style2">Date of Birth</td>
            <td class="auto-style3">
               <asp:TextBox ID="TextBox2" runat="server"  
             ClientIDMode="Static" ReadOnly="true"></asp:TextBox>                       
                <asp:Image  ID="imgCal" DescriptionUrl="#.jpg"  
             AlternateText="" runat="server" />
                 <ajaxToolkit:CalendarExtender ID="Calend" runat="server" 
          Animated="true" PopupButtonID="imgCal" TargetControlID="TextBox2"> 
           </ajaxToolkit:CalendarExtender>

            </td>
        </tr>
            <tr>
            <td class="auto-style2">Age</td>
            <td class="auto-style3">
               <asp:TextBox ID="TextBox7" runat="server" 
              ClientIDMode="Static" ReadOnly="true"></asp:TextBox>

            </td>
        </tr>
        <tr>
            <td class="auto-style2">Gender</td>
            <td class="auto-style3">
                <asp:RadioButtonList ID="RadioButtonList1" runat="server"  
              RepeatDirection="Horizontal">
                    <asp:ListItem>Male</asp:ListItem>
                    <asp:ListItem>Female</asp:ListItem>

                </asp:RadioButtonList></td>
        </tr>
        <tr>
            <td class="auto-style2">Nationality</td>
            <td class="auto-style3">
                <asp:DropDownList ID="DropDownList1" runat="server" 
          Width="145px" AutoPostBack="true" 
        OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                    <asp:ListItem Value="0">n1</asp:ListItem>
                    <asp:ListItem Value="1">loc2</asp:ListItem>
                    <asp:ListItem Value="2">loc3</asp:ListItem>
                    <asp:ListItem Value="3">other</asp:ListItem>
                    <asp:ListItem></asp:ListItem>

                </asp:DropDownList>
                <asp:TextBox ID="TextBox9" runat="server" Visible="false">
         </asp:TextBox>
            </td>

        </tr>
        <tr>
            <td class="auto-style2">Mobile Number</td>
            <td class="auto-style3">
                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            </td>
        </tr>
           <asp:UpdatePanel ID="Update1" runat="server">
         <ContentTemplate>
        <tr>



            <td class="auto-style2">Residential Adress</td>
            <td class="auto-style3">
                <textarea id="TextArea1" cols="20" name="TextArea1"  
          runat="server"></textarea></td>
        </tr>

        </ContentTemplate>
       </asp:UpdatePanel>

    </table>


</div>

Upvotes: 0

Views: 322

Answers (1)

Sandip
Sandip

Reputation: 481

by default password textbox will not retail value on postback.so you need following ways to preserve value based on this link

protected void Page_Load(object sender, EventArgs e)
{
   string pwd = txtPwd.Text;
   txtPwd.Attributes.Add("value", pwd);
}

Upvotes: 2

Related Questions