ASP.NET - hiding BoundField input

863 views Asked by At

I have a simple connection to a database achieved through a DetailsView control in ASP.NET. One of the fields is a password so I'd like its content to be hidden while typing. My first thought was TextBox's TextMode="Password" but that failed miserably since BoundField class doesn't implement it. I'm guessing it must be done programmatically through an event-handler but I simply don't know how.

Here's the markup I've written so far, if needed:

<body>
    <form id="form1" runat="server"> 
    <div class="register" runat="server">
        <asp:DetailsView ID="InsertData" runat="server" HorizontalAlign="Center" 
            Height="100px" Width="170px" 
            AutoGenerateRows="False" DataSourceID="usersConnectionString"
            DefaultMode="Insert" OnItemCommand="Button_click" OnItemInserted="Insert_click">
            <Fields>
                <asp:BoundField DataField="userName" HeaderText="Name" 
                    SortExpression="userName" />
                <asp:BoundField DataField="userPassword" HeaderText="Password"
                    SortExpression="userPassword" />
                <asp:BoundField DataField="userEmail" HeaderText="Email" 
                    SortExpression="userEmail" />
                <asp:CommandField ShowInsertButton="True" />
            </Fields>
        </asp:DetailsView>
        <asp:SqlDataSource ID="usersConnectionString" runat="server" 
            ConnectionString="<%$ ConnectionStrings:usersConnectionString %>" 
            InsertCommand="INSERT INTO user(userName, userPassword, userEmail) VALUES (@userName, @userPassword, @userEmail)">
            <InsertParameters>
                <asp:Parameter Name="userName" Type="String" />
                <asp:Parameter Name="userPassword" Type="String" />
                <asp:Parameter Name="userEmail" Type="String" />
            </InsertParameters>
        </asp:SqlDataSource>
        <asp:Label ID="Message_label" ForeColor="red" Visible="false" runat="server" Text="[messageLabel]"></asp:Label>
    </div>
    </form>
</body>
1

There are 1 answers

0
Ahmed ilyas On BEST ANSWER

The best thing to do in this is to make an ItemTemplate and within that, have a textbox control and set its input mode/type to be Password. The ItemTemplate allows you to add normal/custom user controls to your liking outside of the standard BoundField columns