7.15 dataset and OnTextChanged

本文介绍了一个基于ASP.NET的应用程序,该程序实现了从数据库中获取类及其属性,并将其展示在一个表格中供用户编辑的功能。文章详细解释了如何通过SQL查询填充数据集、建立数据关系以及更新数据库的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using BTK;
using System.Text;
public partial class Try : System.Web.UI.Page
{
    //private AttributeValueData newdata = new AttributeValueData("20", "51");
    private string WLId = "20";
    private string ClassId = "51";

    private DataSet mydataset = new DataSet();
    private DataRelation classRel;
    private DataRelation class_AttributeRel;
    private DataRelation wL_AttributeRel;
    private StringBuilder tempstring = new StringBuilder();
    // private string[] myString = new string[10];
    // private int myStringNum;
    private int trnum;
    private DataTable mytable = new DataTable("AttOfWL");
    private SqlDataAdapter sda = new SqlDataAdapter();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1.DataSource = NewDataTable();
            GridView1.DataBind();
            TextBox txtbox;
            Label templable;
            int gvrownum;
            for (gvrownum = 0; gvrownum < GridView1.Rows.Count; gvrownum++)
            {
                txtbox = (TextBox)GridView1.Rows[gvrownum].FindControl("ValueTextBox");
                templable = (Label)GridView1.Rows[gvrownum].FindControl("lblAttributeId");
                txtbox.ToolTip = templable.Text;
               // txtbox.TextChanged += new System.EventHandler(this.TextBox_TextChanged);
            }
        }
    }
    public void TextBox_TextChanged(object sender, System.EventArgs e)
    {
        TextBox txtBoxSender = (TextBox)sender;
        Label1.Text = txtBoxSender.ToolTip;
    }
    public void GetDate()
    {
        string sqlconntext = SqlHlper.GetSqlServeconn();
        SqlConnection conn = new SqlConnection(sqlconntext);
        string classsqltext = "select * from BTK_ClassTable";
        SqlDataAdapter sda1 = new SqlDataAdapter(classsqltext, sqlconntext);
        sda1.Fill(mydataset, "Class");
        string wlsqltext = "select * from BTK_Attribute";
        SqlDataAdapter sda2 = new SqlDataAdapter(wlsqltext, sqlconntext);
        sda2.Fill(mydataset, "Attribute");
        string wl_Attributesqltext = "select * from BTK_Attribute_WL where WLId=" + WLId;
        SqlDataAdapter sda3 = new SqlDataAdapter(wl_Attributesqltext, sqlconntext);
        sda3.Fill(mydataset, "WL_Attribute");
        conn.Close();
        classRel = mydataset.Relations.Add(mydataset.Tables["Class"].Columns["ClassId"], mydataset.Tables["Class"].Columns["ParentId"]);
        class_AttributeRel = mydataset.Relations.Add(mydataset.Tables["Class"].Columns["ClassId"], mydataset.Tables["Attribute"].Columns["ClassId"]);
        wL_AttributeRel = mydataset.Relations.Add(mydataset.Tables["Attribute"].Columns["Id"], mydataset.Tables["WL_Attribute"].Columns["AttributeId"]);
    }
    /// <summary>
    /// 获取当前类属性
    /// </summary>
    /// <param name="classrow"></param>
    /// <param name="class_attribtute"></param>
    /// <param name="dt"></param>
    public void GetAttribute(DataRow classrow, DataRelation class_attribtute, DataTable dt)
    {
        int id;
        string name;
        bool state;
        string type;
        bool isnull;
        bool ispublic;
        string description;
        string value;
        foreach (DataRow childAttrubuterow in classrow.GetChildRows(class_attribtute))
        {
            trnum++;
            id = int.Parse(childAttrubuterow["Id"].ToString());
            name = childAttrubuterow["Name"].ToString();
            state = bool.Parse(childAttrubuterow["State"].ToString());
            type = childAttrubuterow["Type"].ToString();
            isnull = bool.Parse(childAttrubuterow["IsNull"].ToString());
            ispublic = bool.Parse(childAttrubuterow["IsPublic"].ToString());
            description = childAttrubuterow["Description"].ToString();
            string tempvalue;
            //GetIsNull(bool.Parse(childAttrubuterow["IsNull"].ToString()));
            //GetIsPublic(bool.Parse(childAttrubuterow["IsPublic"].ToString()));
            //"<a href=EditAttribute.aspx?AttributeId=" + childAttrubuterow["Id"].ToString() + ">" + "修改" + "</a>";
            //GetState(bool.Parse(childAttrubuterow["State"].ToString()));
            DataRow[] coll = childAttrubuterow.GetChildRows(wL_AttributeRel);
            if (coll.Length != 0)
            {
                tempvalue = coll[0]["Value"].ToString();
                //"<a href=EditWLAttributeValue.aspx?AttributeId=" + childAttrubuterow["Id"].ToString() + "&WLId=" + WLId + ">" + "修改" + "</a>";

            }
            else
            {
                tempvalue = "没有值";
                //"<a href=AddWLAttributeValue.aspx?AttributeId=" + childAttrubuterow["Id"].ToString() + "&WLId=" + WLId + ">" + "添加" + "</a>";
            }
            value = tempvalue;
            object[] row1 ={ id, name, state, type, isnull, ispublic, description, value };
            dt.Rows.Add(row1);
        }
    }
    /// <summary>
    /// 获取父类属性
    /// </summary>
    /// <param name="classrow"></param>
    /// <param name="class_attribtute"></param>
    public void GetParentAttribute(DataRow classrow, DataRelation class_attribtute, DataTable dt)
    {
        int id;
        string name;
        bool state;
        string type;
        bool isnull;
        bool ispublic;
        string description;
        string value;
        foreach (DataRow childAttrubuterow in classrow.GetChildRows(class_attribtute))
        {
            trnum++;
            id = int.Parse(childAttrubuterow["Id"].ToString());
            name = childAttrubuterow["Name"].ToString();
            state = bool.Parse(childAttrubuterow["State"].ToString());
            type = childAttrubuterow["Type"].ToString();
            isnull = bool.Parse(childAttrubuterow["IsNull"].ToString());
            ispublic = bool.Parse(childAttrubuterow["IsPublic"].ToString());
            description = childAttrubuterow["Description"].ToString();
            string tempvalue;
            //GetIsNull(bool.Parse(childAttrubuterow["IsNull"].ToString()));
            //GetIsPublic(bool.Parse(childAttrubuterow["IsPublic"].ToString()));
            //"<a href=EditAttribute.aspx?AttributeId=" + childAttrubuterow["Id"].ToString() + ">" + "修改" + "</a>";
            //GetState(bool.Parse(childAttrubuterow["State"].ToString()));
            DataRow[] coll = childAttrubuterow.GetChildRows(wL_AttributeRel);
            if (coll.Length != 0)
            {
                tempvalue = coll[0]["Value"].ToString();
                //"<a href=EditWLAttributeValue.aspx?AttributeId=" + childAttrubuterow["Id"].ToString() + "&WLId=" + WLId + ">" + "修改" + "</a>";

            }
            else
            {
                tempvalue = "没有值";
                //"<a href=AddWLAttributeValue.aspx?AttributeId=" + childAttrubuterow["Id"].ToString() + "&WLId=" + WLId + ">" + "添加" + "</a>";
            }
            value = tempvalue;
            object[] row1 ={ id, name, state, type, isnull, ispublic, description, value };
            dt.Rows.Add(row1);
        }
    }
    /// <summary>
    /// 递归父属性
    /// </summary>
    /// <param name="classrow"></param>
    /// <param name="classrel"></param>
    /// <param name="classattributerel"></param>
    public void GetParentName(DataRow classrow, DataRelation classrel, DataRelation classattributerel, DataTable dt)
    {
        if (int.Parse(classrow["ClassId"].ToString()) != 1)
        {
            foreach (DataRow parentclassrow in classrow.GetParentRows(classrel))
            {
                GetParentAttribute(parentclassrow, classattributerel, dt);
                GetParentName(parentclassrow, classrel, classattributerel, dt);
            }
        }
    }
    public string GetColor()
    {
        if (trnum % 2 == 0)
        {
            return "shuang";
        }
        return "dan";
    }
    public string GetDelectOrRE(bool state)
    {
        if (state)
        {
            return "删除";
        }
        return "恢复";
    }
    public string GetState(bool state)
    {
        if (state)
        {
            return "正常";
        }
        return "已删除";
    }
    public string GetIsNull(bool state)
    {
        if (state)
        {
            return "可以空";
        }
        return "不能为空";
    }
    public string GetIsPublic(bool state)
    {
        if (state)
        {
            return "可继承";
        }
        return "不能继承";
    }
    public DataSet NewDataTable()
    {
        mytable.Columns.Add("AttributeId");//值编号
        mytable.Columns.Add("Name");//属性名称
        mytable.Columns.Add("State");//属性状态
        mytable.Columns.Add("Type");//属性类型
        mytable.Columns.Add("IsNull");//允许空
        mytable.Columns.Add("IsPublic");//允许继承
        mytable.Columns.Add("Description");//描述
        mytable.Columns.Add("Value");//值
        mydataset.Tables.Add(mytable);
        GetDate();
        trnum = 0;
        foreach (DataRow classrow in mydataset.Tables["Class"].Rows)
        {
            if (classrow["ClassId"].ToString() == ClassId)
            {
                GetAttribute(classrow, class_AttributeRel, mytable);
                GetParentName(classrow, classRel, class_AttributeRel, mytable);
            }
        }
        SqlConnection conn = new SqlConnection(SqlHlper.GetSqlServeconn());
        string sqlupdatetext = "update BTK_Attribute_WL set Value=@Value where AttributeId=@AttributeId and WLID=" + WLId;
        string sqlinserttext = "insert into BTK_Attribute_WL (WLId,AttributeId,UserId,Value) " +
    "values (@WLId,@AttributeId,@UserId,@Value)";
        string genbenmeiyou = "0";
        sda.SelectCommand = new SqlCommand("select *from BTK_Attribute_WL where WLID=" + genbenmeiyou, conn);
        sda.UpdateCommand = new SqlCommand(sqlupdatetext, conn);
        sda.InsertCommand = new SqlCommand(sqlinserttext, conn);
        SqlParameter state = new SqlParameter();
        state = sda.UpdateCommand.Parameters.Add("@Value", SqlDbType.NVarChar);
        state.SourceColumn = "Value";
        state.SourceVersion = DataRowVersion.Current;
        state = sda.UpdateCommand.Parameters.Add("@AttributeId", SqlDbType.Int);
        state.SourceColumn = "AttributeId";
        state.SourceVersion = DataRowVersion.Current;
        state = sda.InsertCommand.Parameters.Add("@WLId", SqlDbType.Int);
        state.SourceColumn = "WLId";
        state.SourceVersion = DataRowVersion.Current;
        state = sda.InsertCommand.Parameters.Add("@AttributeId", SqlDbType.Int);
        state.SourceColumn = "AttributeId";
        state.SourceVersion = DataRowVersion.Current;
        state = sda.InsertCommand.Parameters.Add("@UserId", SqlDbType.NVarChar);
        state.SourceColumn = "UserId";
        state.SourceVersion = DataRowVersion.Current;
        state = sda.InsertCommand.Parameters.Add("@Value", SqlDbType.NVarChar);
        state.SourceColumn = "Value";
        state.SourceVersion = DataRowVersion.Current;
        sda.Fill(mydataset, "AttOfWL");
        return mydataset;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        sda.Update(mydataset, "AttOfWL");
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值