为什么我的gridview中的链接按钮不起作用?

时间:2016-12-11 18:40:18

标签: c# sql asp.net gridview

enter image description here

<%@ Page Title =“”Language =“C#”MasterPageFile =“〜/ Site.Master”AutoEventWireup =“true”CodeBehind =“View_remove_children.aspx.cs”Inherits =“Coursework.View_remove_children”%>          

        
    

    

                                                                 

    

    

EDIT - Screenshot of error!

我目前正在尝试使用Visual Studio在c#中创建一个asp.net Web应用程序。我有一个网格视图,显示数据库表中的一些字段,并添加了一个带有“删除”链接按钮的列,以使用户能够删除表条目。尝试各种解决方案后,我在运行我的应用程序时单击任何删除链接按钮时收到错误。我觉得问题出在源代码中,但我在网上找到的解决方案都没有。我已经包含了我的.cs代码以及我的源代码。问题似乎是它没有意识到我正在使用链接按钮,即使我是。如果有人能够发现我出错的地方,我将非常感谢他们的帮助。提前谢谢!

编辑 - I should add that I am using this guide to try to implement a delete feature in my gridview, if that helps at all.

Error when clicking any of the delete link buttons

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


namespace Coursework
{
public partial class View_remove_children : System.Web.UI.Page
{
    private String strConnString = ConfigurationManager.ConnectionStrings["newregDBConnectionString"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    public void BindData()
    {
        string strQuery = "select firstname, dob, childID" +
                           " from children";
        SqlCommand cmd = new SqlCommand(strQuery);
        GridView1.DataSource = GetData(cmd);
        GridView1.DataBind();
    }

    public DataTable GetData(SqlCommand cmd)
    {
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection(strConnString);
        SqlDataAdapter sda = new SqlDataAdapter();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        con.Open();
        sda.SelectCommand = cmd;
        sda.Fill(dt);
        return dt;
    }
    protected void OnPaging(object sender, GridViewPageEventArgs e)
    {
        BindData();
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }     
    protected void UpdateCustomer(object sender, GridViewUpdateEventArgs e)
    {
        string firstname = ((Label)GridView1.Rows[e.RowIndex].FindControl("firstnameLbl")).Text;
        string dob = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("dobLbl")).Text;
        string childID = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("childIDlbl")).Text;
        SqlConnection con = new SqlConnection(strConnString);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "update children set childID=@childID,firstname=@firstname " +
         "where childID=@childID;" +
         "select firstname, dob, childID from children";
        cmd.Parameters.Add("@firstname", SqlDbType.VarChar).Value = firstname;
        cmd.Parameters.Add("@dob", SqlDbType.VarChar).Value = dob;
        cmd.Parameters.Add("@childID", SqlDbType.VarChar).Value = childID;
        GridView1.EditIndex = -1;
        GridView1.DataSource = GetData(cmd);
        GridView1.DataBind();
    }

    protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
    {

    }

    public void Gridview1_RowDeleting(Object sender, GridViewDeleteEventArgs e)
    {
        LinkButton lnkRemove = (LinkButton)sender;
        SqlConnection con = new SqlConnection(strConnString);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "delete from  children where " +
        "childID=@childID;" +
         "select firstname, dob, childID from children";
        cmd.Parameters.Add("@childID", SqlDbType.VarChar).Value = lnkRemove.CommandArgument;
        GridView1.DataSource = GetData(cmd);
        GridView1.DataBind();
    }

}

}

0 个答案:

没有答案