我在使用asp.net C#
编辑网格视图时遇到问题当我点击最后一栏中的编辑链接时,它没有显示我的文本框。但它显示更新和取消选项!
这是我的表
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)
);
这是代码
<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>
<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>
<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>
背后的代码
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();
}
答案 0 :(得分:0)
第一个EditItemTemplate
只有一个TemplateField
元素,您需要为每个EditItemTemplate
添加一个TemplateField
元素,您想要编辑它,如下所示:
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton ID="lnkBtnUpdate" runat="server"
CausesValidation="True"
CommandName="Update" Text="Update">
</asp:LinkButton>
<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>
<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>