AL3MS AL3MS - 2 months ago 12
ASP.NET (C#) Question

Text boxes in edit mode of grid view are not visible?

I have problem in edit grid view using asp.net C#

when I clicked on edit link in the last column it is not showing me textboxes. however it shows update and cancel options!

enter image description here

this is my table

CREATE TABLE [dbo].[TbStudent] (
[StuId] INT IDENTITY (218234581, 1) NOT NULL,
[StuFirstName] NCHAR (10) NOT NULL,
[StuLastName] NCHAR (10) NOT NULL,
PRIMARY KEY CLUSTERED ([StuId] ASC)
);


this is the code

<asp:GridView ID="GridView1" runat="server" CellPadding="3" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" AutoGenerateColumns="False" Width="549px" ForeColor="Black" CssClass="auto-style3" ShowFooter="True" AllowPaging="True" OnRowEditing="GridView1_OnRowEditing" DataKeyNames="StuId" OnRowUpdating="GridView1_RowUpdating" >
<AlternatingRowStyle BackColor="White" />
<Columns>

<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton ID="lnkBtnUpdate" runat="server" CausesValidation="True"
CommandName="Update" Text="Update"></asp:LinkButton>
&nbsp;<asp:LinkButton ID="lnkBtnCancel" runat="server"
CausesValidation="False"
CommandName="Cancel" Text="Cancel">
</asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkBtnInsert" runat="server"
CommandName="Insert">Add</asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="lnkBtnEdit" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit"></asp:LinkButton>
&nbsp;<asp:LinkButton ID="lnkBtnDelete" runat="server"
CausesValidation="False"
CommandName="Delete" Text="Delete">
</asp:LinkButton>
</ItemTemplate>




<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("StuLastNme") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtStuLName" placeholder="Last Name" runat="server" CssClass="auto-style16" Width="80px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("StuFirstNme") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtStuFName" placeholder="First Name" runat="server" CssClass="auto-style16" Width="80px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student Id">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("StuId") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtStuID" placeholder="Student Id" runat="server" CssClass="auto-style16" Width="80px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>


</Columns>
<FooterStyle BackColor="#E7E7FF" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle ForeColor="#4A3C8C" BackColor="#E7E7FF" />
<SelectedRowStyle BackColor="#E7E7FF" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#E7E7FF" />
<SortedAscendingHeaderStyle BackColor="#E7E7FF" />
<SortedDescendingCellStyle BackColor="#E7E7FF" />
<SortedDescendingHeaderStyle BackColor="#E7E7FF" />
</asp:GridView>


code behind

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Manage : System.Web.UI.Page
{
String myconnectionString = ConfigurationManager.ConnectionStrings["RigesterConnectionString1"].ConnectionString;
SqlConnection con;

SqlDataAdapter adapter;

DataSet ds;

SqlCommand cmd;


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ShowData();
}
}

private void insertStudent()//this function for insert new Student
{

}

protected void ShowData()
{
con = new SqlConnection(myconnectionString);

cmd = new SqlCommand("select * from TbStudent", con);

con.Open();

adapter = new SqlDataAdapter(cmd);


ds = new DataSet();

adapter.Fill(ds);



if (ds.Tables[0].Rows.Count == 0)
{
}
else
{


GridView1.DataSource = ds.Tables[0];

GridView1.DataBind();



con.Close();
}
}





protected void GridView1_OnRowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
ShowData();
}

Answer

You only have one EditItemTemplate element for the first TemplateField, you need an EditItemTemplate element for each TemplateField that you want to be editable, like this:

<Columns>
    <asp:TemplateField>
        <EditItemTemplate>
            <asp:LinkButton ID="lnkBtnUpdate" runat="server" 
                            CausesValidation="True"
                            CommandName="Update" Text="Update">       
            </asp:LinkButton>
            &nbsp;
            <asp:LinkButton ID="lnkBtnCancel" runat="server"
                            CausesValidation="False"
                            CommandName="Cancel" Text="Cancel">
            </asp:LinkButton>
        </EditItemTemplate>
        <FooterTemplate>
            <asp:LinkButton ID="lnkBtnInsert" runat="server"  
                            CommandName="Insert">Add</asp:LinkButton>
        </FooterTemplate>
        <ItemTemplate>
            <asp:LinkButton ID="lnkBtnEdit" runat="server" 
                            CausesValidation="False"
                            CommandName="Edit"  Text="Edit">
            </asp:LinkButton>
            &nbsp;
            <asp:LinkButton ID="lnkBtnDelete" runat="server"
                            CausesValidation="False"
                            CommandName="Delete" Text="Delete">
            </asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Last Name">
        <ItemTemplate>
            <asp:Label ID="Label4" runat="server" 
                       Text='<%#  Bind("StuLastNme") %>'>
            </asp:Label>
        </ItemTemplate>
        <FooterTemplate>
            <asp:TextBox ID="TxtStuLName" placeholder="Last Name" 
                         runat="server" 
                         CssClass="auto-style16" Width="80px"> 
            </asp:TextBox>
        </FooterTemplate>
        <EditItemTemplate>
            Put your textbox here...
        </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="First Name">
        <ItemTemplate>
            <asp:Label ID="Label3" runat="server" 
                       Text='<%# Bind("StuFirstNme") %>'>
            </asp:Label>
        </ItemTemplate>
        <FooterTemplate>
            <asp:TextBox ID="TxtStuFName" placeholder="First Name" 
                         runat="server" 
                         CssClass="auto-style16" Width="80px">
            </asp:TextBox>
        </FooterTemplate>
        <EditTemplate>
            Put your textbox here...
        </EditTemplate>
    </asp:TemplateField>
</Columns>