c#在asp.net中的代码后面编辑我的数据列表的问题 [英] problem with editting my datalist in code behind in asp.net with c#

查看:94
本文介绍了c#在asp.net中的代码后面编辑我的数据列表的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的asp页面中有这样的数据列表

i have datalist in my asp page like this

<asp:DataList ID="DataList1" runat="server"  DataKeyField="memberID"  EditItemStyle-CssClass="EditItemStyle" 

        ItemStyle-CssClass="view" CssClass="datalist" 

        oneditcommand="DataList1_EditCommand" 

        oncancelcommand="DataList1_CancelCommand" 

        onupdatecommand="DataList1_UpdateCommand" 

        ondeletecommand="DataList1_DeleteCommand">


然后我添加


and i add

<EditItemTemplate>


并且我添加了一些像这样的字段


and i add some field like this

<asp:TextBox ID="TextName" Text='<%# Eval("Name") %>' runat="server">


在后面的代码中,我想编辑fileds

代码:


in code behind i want to edit the fileds

code:

protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        // Get form fields
        TextBox txtName = (TextBox)e.Item.FindControl("TextName");
        TextBox txtFamily = (TextBox)e.Item.FindControl("TextFamily");
        .
        .
         dal db = new dal();
        if (Page.IsValid)
        {
            db.run_dr("update members set Name=N'" + txtName.Text +
                                          "',Family=N'"+txtFamily.Text+
                     "' where memberID=" + DataList1.DataKeys[e.Item.ItemIndex].ToString());
            DataList1.DataBind();
            DataList1.EditItemIndex = -1;
        }



问题:::
例如,我想将字段(名称)从"a"更改为"b"
但txtName始终为"a",而不会获得值"b"



proble:::
for example i want to change Fields (name) from "a" to "b"
but txtName always is "a" and don''t get value "b"

TextBox txtName = (TextBox)e.Item.FindControl("TextName");

推荐答案

Its may be work:
// sql command object initialized with update command text  
        cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = txtName.Text;
        cmd.Parameters.Add("@Family", SqlDbType.VarChar).Value = txtFamily.Text;
cmd.Parameters.Add("@memberId", SqlDbType.Int).Value = DataList1.DataKeys[e.Item.ItemIndex].ToString();
        cmd.CommandText = "Update members set       Name=@Name,Family=@Family where StId=" + memberId;
 cmd.Connection = con;
        cmd.Connection.Open();
        cmd.ExecuteNonQuery();
        // reset the DataList mode back to its initial state  
        DataList1.EditItemIndex = -1;
        GetLatestBlogs();


这篇关于c#在asp.net中的代码后面编辑我的数据列表的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆