【问题标题】:Details View Insert/update data through Coding详细信息通过编码查看插入/更新数据
【发布时间】:2012-07-21 06:07:06
【问题描述】:

我正在尝试通过编码从详细信息视图将数据插入或更新到数据库中。无需为详细信息视图提供数据源,因为它非常简单。我想以编程方式进行。

正如我所说,我没有直接提供数据源。我在页面加载时做到了。

static string conn = ConfigurationManager.ConnectionStrings["AptechConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(conn);

protected void Page_Load(object sender, EventArgs e)
{
    DetailsView1.AutoGenerateInsertButton = true;
    DetailsView1.AutoGenerateEditButton = true;
    DetailsView1.AutoGenerateDeleteButton = true;
    Data_Bind();
}

public void Data_Bind()
{
    string query = "select * from students";
    SqlCommand cmd = new SqlCommand(query, con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);

    DetailsView1.DataSource = dt;
    DetailsView1.DataBind();
    ViewState["Data"] = dt;
}

我试着像在 gridview 中那样做

protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
    try
    {
        id = (TextBox)DetailsView1.Rows[0].Cells[1].FindControl("TextBox1");
        name = (TextBox)DetailsView1.Rows[0].Cells[1].FindControl("TextBox2");
        batch = (TextBox)DetailsView1.Rows[0].Cells[1].FindControl("TextBox3");
        enrolled = (TextBox)DetailsView1.Rows[0].Cells[1].FindControl("TextBox4");
        teacher = (TextBox)DetailsView1.Rows[0].Cells[1].FindControl("TextBox5");
    }
    catch (Exception ex)
    {
        Label1.Text = ex.Message;
        Label1.Visible = true;
    }
}

protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
    switch (e.CommandName.ToString())
    {
        case "New":
            DetailsView1.ChangeMode(DetailsViewMode.Insert);
            Data_Bind();
            break;
    }
}

但是,当我点击编辑或插入链接按钮时,它给了我这个错误

DetailsView 'DetailsView1' 触发了未处理的事件 ModeChanging。"

我错过了什么?

【问题讨论】:

  • 您能否显示您的详细信息视图的标记部分

标签: c# asp.net detailsview insert-update


【解决方案1】:

尝试添加ModeChanging 事件处理程序:

protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
    DetailsView1.ChangeMode(e.NewMode);
    Data_Bind();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-26
    相关资源
    最近更新 更多