Link to home
Start Free TrialLog in
Avatar of zhshqzyc
zhshqzyc

asked on

Edit a field on the page

Hello, suppose I want to edit user name on the page. The attached image is a snapshot. User generated imageCurrently it is a label there.
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div>General user information</div>
<p></p>
<table cellpadding="2">
   <tr>
      <td width="130">UserName:</td>
      <td width="300"><asp:Literal runat="server" ID="lblUserName" /></td>
   </tr>
   <tr>
      <td>E-mail address:</td>
      <td><asp:HyperLink ID="lnkEmailAddress" runat="server" /></td>
   </tr>
   <tr>
      <td>Registered:</td>
      <td><asp:Literal  ID="lblRegistered" runat="server" /></td>
   </tr>

Open in new window

I want that I can control the user name then save it to the database.
Any suggestion?
Should I change the label to text box?
I appreciate a piece of code.
ASKER CERTIFIED SOLUTION
Avatar of Manoj Patil
Manoj Patil
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zhshqzyc
zhshqzyc

ASKER

Probably I need edit  and save it in the database.
Can I use a gridview to display plus edit function?
Like this,
but I don't want to edit the entire table, I just want to implement a specific user.
Ignore the above one. Please see the attached file. User generated imageWhat I want is that I can edit the user name and click appove box then save it. The user will received an confirmation email.

Thanks for help.
Yes,  you can use GridView to Edit and save the Data in Database.
Just Enable Edit, Enable Update, Enable Delete Properties of GridView to do so.

If you want to edit only specific column then you can achieve it as follows. When you enable Edit, Update, Delete properties then GridView looks like following.

SAMPLE GRID VIEW :

<asp:GridView ID="GridViewEmployee" runat="server" AutoGenerateColumns="False"

        ShowFooter="True" onrowcancelingedit="GridViewEmployee_RowCancelingEdit"

        onrowediting="GridViewEmployee_RowEditing"

        onrowupdating="GridViewEmployee_RowUpdating"

        onrowdeleting="GridViewEmployee_RowDeleting">

    <Columns>

        <asp:TemplateField HeaderText="Employee Name">

            <EditItemTemplate>

                <asp:TextBox ID="TextBoxEditEmployee" runat="server" Text='<%# Bind("Employees") %>'/>

            </EditItemTemplate>

            <ItemTemplate>

                <asp:Label ID="LabelEmployee" runat="server" Text='<%# Bind("Employees") %>'/>

            </ItemTemplate>

            <FooterTemplate>

                <asp:TextBox ID="TextBoxEmployee" runat="server"/>

            </FooterTemplate>

        </asp:TemplateField >

        <asp:TemplateField HeaderText="Position">

            <EditItemTemplate>

                <asp:TextBox ID="TextBoxEditPosition" runat="server" Text='<%# Bind("Position") %>'/>

            </EditItemTemplate>

            <ItemTemplate>

                <asp:Label ID="LabelPosition" runat="server" Text='<%# Bind("Position") %>'/>

            </ItemTemplate>

            <FooterTemplate>

                    <asp:TextBox ID="TextBoxPosition" runat="server"/>

            </FooterTemplate>

        </asp:TemplateField>

        <asp:TemplateField HeaderText="Team Name">

            <EditItemTemplate>

                <asp:TextBox ID="TextBoxEditTeam" runat="server" Text='<%# Bind("Team") %>'/>

            </EditItemTemplate>

            <ItemTemplate>

                <asp:Label ID="LabelTeam" runat="server" Text='<%# Bind("Team") %>'/>

            </ItemTemplate>

            <FooterTemplate>

                    <asp:TextBox ID="TextBoxTeam" runat="server"/>

            </FooterTemplate>

        </asp:TemplateField>

        <asp:TemplateField HeaderText="Employee ID">

            <ItemTemplate>

                <asp:Label ID="LabelID" runat="server" Text='<%# Bind("Id") %>'/>

            </ItemTemplate>

            <FooterTemplate>

                <asp:Button ID="Button1" runat="server" Text="Add New" OnClick="Button1_Click" />

            </FooterTemplate>

        </asp:TemplateField>

        <asp:CommandField ShowEditButton="True" ShowDeleteButton />

    </Columns>

    </asp:GridView>

Open in new window



If you want to update only username then in <EditItemTemplate> tab, there is an TextBox field with Text property like        
 Text='<%# Bind("UserName") %>'  

The fields which you don't want to update, in this case
In <EditItemTemplate> for that specific column  just replace TextBox with Label. That's it !!!

When you click on Edit button then it show the TextBox for which you need to update the Data and the fields which you have set to Labels will not allow to change the Data
Try this.
If using GridView, it will display the whole table and there are Edit buttons on the left side.
However if I only want to display a specific user information as the image below. I don't want Edit butthon showing up, in another word, I do not use GridView.
 User generated imageThen how?
If you don't want to use GridView then follow the steps which I had told you in first comment