Asp.net无法识别我的用户控件

时间:2018-05-08 16:29:17

标签: c# .net user-controls

我已经为用户控件编写了一个可编辑的GridView。以下是设计中的控件代码(文件:editableGridView.ascx)

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="editableGridView.ascx.cs" Inherits="WebApplication1.EditableGridView" %>


<asp:GridView ID="EditableGrid" runat="server" Width="500px" Height="500px" AllowSorting="True"
                AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"
                OnRowEditing="EditableGrid_RowEditing" 
                OnRowCancelingEdit="EditableGrid_RowCancelingEdit"
                OnRowUpdating="EditableGrid_RowUpdating"
                OnRowDeleting="EditableGrid_RowDeleting"
                ></asp:GridView> 

以下是代码:(文件:editableGridView.ascx.cs)

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

namespace WebApplication1
{


    public partial class EditableGridView : System.Web.UI.UserControl
    {
        public string connstr { get; set; }
        public string tablename { get; set; }
        public string selectcmd { get; set; }
        private List<EdField> fields;

        private SqlConnection con;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void InitGrid(string theconnstr, string tablename)
        {
            con = new SqlConnection(connstr); 
            con.Open();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.Connection = con;
                cmd.CommandText = "SELECT * FROM " + tablename;
                using (SqlDataReader rd = cmd.ExecuteReader())
                {
                    if (!rd.HasRows) return;
                    fields = new List<EdField>();
                    for (int i =0; i < rd.FieldCount; i++)
                    {
                        fields.Add(new EdField(rd.GetName(i), rd.GetDataTypeName(i)));
                    }
                }
            }
        }

        public void Bind()
        {
            con.Open();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.Connection = con;
                cmd.CommandText = selectcmd;
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    using (DataTable dt = new DataTable())
                    {
                        da.Fill(dt);
                        EditableGrid.DataSource = dt;
                        EditableGrid.DataBind();
                        EditableGrid.Visible = true;
                    }
                }
            }
            con.Close();
        }


        protected void EditableGrid_RowEditing(object sender, GridViewEditEventArgs e)
        {
            EditableGrid.EditIndex = e.NewEditIndex;
            Bind();
        }

        protected void EditableGrid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            EditableGrid.EditIndex = -1;
            Bind();
        }

        protected void EditableGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

               GridViewRow row = (GridViewRow)EditableGrid.Rows[e.RowIndex];
            List<TextBox> tbs = new List<TextBox>();
            for (int i = 1; i < fields.Count; i++)
            {
                tbs.Add((TextBox)row.FindControl("textbox" + i.ToString()));
            }


            EditableGrid.EditIndex = -1;


            String sql = "UPDATE " + tablename + "SET " ;
            for (int i = 1; i < fields.Count; i++)
            {
                sql += fields[i].Name;
                sql += " = ";
                sql += ProperFormatField(fields[i].DataTypeName, tbs[i].Text);
                if (i < fields.Count - 1)
                {

                    sql += " , ";
                }
            }

            Label lbl = (Label)row.FindControl("lblid");
            sql += " WHERE " + fields[0].Name + " = " + lbl.Text;

            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);

            cmd.ExecuteNonQuery();
            con.Close();


            Bind();

        }

        protected void EditableGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow row = (GridViewRow)EditableGrid.Rows[e.RowIndex];
            Label lbl = (Label)row.FindControl("lblid");


            String sql = "DELETE " + tablename;
            sql += " WHERE " + fields[0].Name + " = " + lbl.Text;

            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.ExecuteNonQuery();
            con.Close();

            Bind();
           /* */
        }

        private String ProperFormatField(String dtf, String s)
        {
            if (
                (dtf == "CHAR") ||
                (dtf == "VARCHAR") ||
                (dtf == "NVARCHAR") ||
                (dtf == "NCHAR") ||
                (dtf == "DATE") ||
                (dtf == "DATETIME") ||
                (dtf == "DATETIME2")

                )
            {
                return "'" + s + "'";
            }
            else
                return s;

        }
    }

    public class EdField
    {
        private String myName;
        private String myDataTypeName;

        public String Name
        {
            get { return myName; }
            set { myName = value; }
        }

        public String DataTypeName
        {
            get { return myDataTypeName; }
            set { myDataTypeName = value; }
        }





        public EdField(String theName, String theDataTypename)
        {
            myName = theName;
            myDataTypeName = theDataTypename.ToUpper();
        }
    }
}

现在,在我想要放置控制权的页面中,我写道:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %>
<%@ Register TagPrefix="mygrids" TagName="EditableGridView" Src="editableGridView.ascx"%>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
           <mygrids:EditableGridView ID="edgv1"></mygrids:EditableGridView>
        </div>
    </form>
</body>
</html>

但是在EditableGridView下有一个绿色标记,提示说:“元素EditableGridView未知。问题可能是网站的编译错误或文件web.config丢失”

这里有任何帮助或想法吗?

0 个答案:

没有答案