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. Currently it is a label there.
Any suggestion?
Should I change the label to text box?
I appreciate a piece of code.
<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>
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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 :
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.
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>
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.
ASKER
If you don't want to use GridView then follow the steps which I had told you in first comment
ASKER
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.