[asp.net]在编辑模式下的网格视图中的文本框是不可见的吗?

发布时间: 2016/11/6 12:16:39
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我在编辑网格视图中使用 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>
                     &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>

背后的代码

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();
    }

解决方法 1:

你只有一个 EditItemTemplate 第一元素 TemplateField ,你需要 EditItemTemplate 为每个元素 TemplateField 你想要编辑的像这样︰

<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>
官方微信
官方QQ群
31647020