【C#】更新功能和数据传递的实现

本文介绍了使用C#进行网站后台开发的过程,包括利用GridView实现文章列表显示、修改及删除功能,通过Repeater控件展示文章列表,并解决了中文乱码等问题。

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

用C#写网站后台时的记录


文章列表显示的基础上,在GRIDVIEW 上添加选择列,将文本改成修改,编写修改的按钮,实现在另外一个页面内将数据库中的内容调出供用户修改,并在修改完毕后传回数据库。

选择(修改)事件的代码

 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)

    {

        string title = GridView1.Rows[GridView1.SelectedIndex].Cells[3].Text;

        string url = "正式的文章添加.aspx?title=" + Server.UrlEncode(title) + "&table=" + SelectColunm.SelectedValue.ToString()+ "&id=" + GridView1.Rows[GridView1.SelectedIndex].Cells[2].Text.ToString();

        Response.Redirect(url);

    }

GridView1.Rows[GridView1.SelectedIndex].Cells[3].Text是选取到GRIDVIEW中标题的语句,下面用GET方式明码传输参数,由于是中文参数,要在传输端用Server.UrlEncode(title)编码。

在正式的文章编辑页面中的page_load事件中加入以下代码,由于文章添加和修改时共用的同一个页面,需要用传递过来的参数个数判断是添加文章还是修改文章。

 string[] param = Request.Url.PathAndQuery.Split('=');用于获取URL字符串中的各个字段

        if (param.Length > 3)

        {

            string title = Request.QueryString["title"];解码并获取属性值

            string table = Request.QueryString["table"];

            string id = Request.QueryString["id"];

            SqlConnection mystr = new SqlConnection();

            mystr.ConnectionString = "Data Source=mike;Initial Catalog=jishengWebSite;User ID=sa;Password=123456;Pooling=False";

            mystr.Open();

            if (!IsPostBack)重要!!!不加的话添加无效

            {

                TextBox.Text = executeP(table, mystr, id, "Context");

通过函数取得数据库中的值,函数中使用了存储过程

                Title.Text = executeP(table, mystr, id, "title");

                SelectColunm.Value = Server.UrlDecode(table);

                if (Server.UrlDecode(table) == "XWDT" || Server.UrlDecode(table) == "RDJJ")

                {

                    Auther.Value = executeP(table, mystr, id, "origin");

                }

                else

                {

                    Auther.Value = executeP(table, mystr, id, "authername");

                }

                if (Server.UrlDecode(table) == "XGFLFG" || Server.UrlDecode(table) == "SJZC")

                {

                    DID.Value = executeP(table, mystr, id, "Documentid");

                }

            }

            mystr.Close();

            ischange = true;

        }

        Else为添加文章的代码

        {

            string table = Request.QueryString["table"];

            if (!IsPostBack)

            {

                SelectColunm.Value = Server.UrlDecode(table);

            }

        }

遇到的问题:

第一个是数据的传递,这里选择的是用URL传递,因为数据没有保密要求,而且数据量也不大,通过URL的字符拼接和分解可以实现,但是后来发现中文字符是乱码。要用ENCODEDECODE实现中文的传输

第二个问题是判断是否为修改,由于修改和添加新的内容是穿的参数不一样,所以用参数的个数既可以区别。

第三个问题是更新无法实现,是由于ISPOSTBACK的原理。如果在PAGE_LOAD中没有用 if (!IsPostBack)包住修改空间值的语句(因为修改的时候要加载数据库中的内容),在提交时,页面会再执行一次PAGE_LOAD事件,所有控件的值就会变成一开始从数据库中调出来的值,而不是用户新修改的值,无法实现修改,解决方法就是ISPOATBACK

删除功能的实现

删除功能也在GRIDVIEW中实现,由于选择使用了存储过程,所以删除也需要存储过程实现,但由于GRIDVIEW内部使用存储过程是无法传递参数,所以删除使用代码方式调用存储过程并传入参数,代码如下

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

    {

        SqlConnection mystr = new SqlConnection();

        mystr.ConnectionString = "Data Source=mike;Initial Catalog=jishengWebSite;User ID=sa;Password=123456;Pooling=False";

        mystr.Open();

        string command = "execute P_DeleteArticle '"+SelectColunm.Text.ToString()+"' ,'"+GridView1.Rows[e.RowIndex].Cells[2].Text+"'";

        SqlCommand strcommand = new SqlCommand(command, mystr);

        strcommand.ExecuteScalar();

        mystr.Close();

    }

但是在执行时页面会提示错误说没有DELETESQL语句不执行操作,但是数据已经从数据库中删除了(由于以上代码调用了存储过程),这个是由于GRIDVIEW的内部原理导致的,在GRIDVIEW的数据源配置中将EXEC('')写入DELETE语句中,就实现了功能了。

EXEC('')的功能是要GRIDVIEW去执行一条空的SQL语句,然后用我们自己写的方法去删除数据。

文章列表显示

在列表显示中使用REPEATER控件,在代码中加入

   protected void Page_Load(object sender, EventArgs e)

    {

        if(!IsPostBack )

        {

            SqlConnection mystr = new SqlConnection();

            mystr.ConnectionString = "Data Source=mike;Initial Catalog=jishengWebSite;User ID=sa;Password=123456;Pooling=False";

            mystr.Open();

            SqlDataAdapter sda = new SqlDataAdapter("select * from T_XWDT", mystr);

            DataSet ds = new DataSet();

            sda.Fill(ds);

            Repeater1.DataSource = ds.Tables[0].DefaultView;

            Repeater1.DataBind();

        }

    }

这个是为REPEATER提供数据源的语句,具体就是定义了一个DATASET,然后用ADAPTER的语句填充dataset,最后将REPEATER的数据源绑定在DATASET上。

在网页处用规定的格式调用

<table>

        <asp:Repeater ID="Repeater1" runat="server" >

        <ItemTemplate>

        <tr><td><a href ="正式文章查看.aspx?id=<%#DataBinder.Eval(Container.DataItem,"id")%>&table=T_XWDT"><%#DataBinder.Eval(Container.DataItem,"title")%></a></td><td><%#DataBinder.Eval(Container.DataItem,"publishtime") %></td></tr>

        </ItemTemplate>

        </asp:Repeater>

        </table>

具体的执行过程是不断地从DATASET中获取数据,在页面中重复表格的行,一条条显示数据。在页面中加入<A></A>并且设置其,HREF属性就可以实现连接到正式的文章查看页面查看文章,具体的参数可以通过URL传递,上述代码传递了文章的表格和ID


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值