ASP.NET-无法将更改从GridView保存到数据库 [英] ASP.NET - Can't save changes from GridView to Database

查看:85
本文介绍了ASP.NET-无法将更改从GridView保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GridView.开头有一个复选框,其他列是自动生成的:

I have a GridView. It has a Checkbox at the beggining, and the other columns are generated automatically:

<asp:GridView ID="DailyData"
    EmptyDataText="No data."
    CssClass="data-grid"
    HeaderStyle-CssClass="HeaderText"
    Width="100%"
    Visible="true"
    runat="server">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox runat="server" />
            </ItemTemplate>
            <HeaderStyle Font-Bold="True" />
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:Button ID="btnEdit" OnClick="btnEdit_Click" Text="Editar" runat="server" />
<asp:Button ID="btnSave" OnClick="btnSave_Click" Text="Guardar" runat="server" />

DailyData.AutoGenerateColumns = true;
DailyData.DataSource = dataTable;
DailyData.DataBind();

当我单击编辑"按钮时,它将在所需的单元格中添加文本框控件,并将cell.text放在textbox.text上:

When I click the "Edit" button, it adds textbox controls in the required cells, and put the cell.text on textbox.text:

protected void btnEdit_Click(object sender, EventArgs e)
{
    if (DailyData.Rows.Count > 0)
    {
        int checks = 0;
        foreach (GridViewRow gvr in DailyData.Rows)
        {
            CheckBox check = gvr.Cells[0].Controls[1] as CheckBox;
            if (check.Checked == true)
            {
                foreach (DataControlFieldCell cell in gvr.Cells)
                {
                    if (!cell.ContainingField.HeaderText.Contains("DATE") &&
                        !cell.ContainingField.HeaderText.Contains("ID") &&
                        !cell.ContainingField.HeaderText.Contains("CP") &&
                        !cell.ContainingField.HeaderText.Equals(""))
                    {
                        string texto = cell.Text;
                        if (texto == "&nbsp;")
                            texto = "";
                        cell.Text = "";
                        TextBox txt = new TextBox();
                        txt.Text = texto;
                        cell.Controls.Add(txt);
                    }
                }
                checks++;
            }
        }
        if (checks == 0)
        {
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "popup", "alert('There isn't any row checked')", true);
        }
    }
}

当我尝试保存这些文本框(用户已对其进行编辑)的信息时,问题就来了.当我单击保存"按钮时,GridView的单元格中没有任何文本框控件.

The problem comes when I try to Save the info of those textboxes (which user have edited). When I click the Save button, there aren't any textbox controls in the cells of the GridView.

创建文本框后,我试图将GridView保存到Session变量中.并且它起作用了,但是问题仍然存在...这些文本框的用户输入没有保存到该Session变量中,因此,单击保存"按钮时,它们上不存在任何textbox.text.

I've tried to save the GridView into a Session variable just after textboxes are created. And it works, but the problem remains the same... the user's input of those textboxes isn't saved into that Session variable, so when clicking Save button there doesn't exist any textbox.text on them.

请问有什么建议吗?

保存"按钮的代码与编辑"按钮的代码几乎相同,只是不添加文本框,而是获取textbox.text并将其发送到数据库.

The Save button's code it's almost the same as Edit button's code, just instead of adding textbox, it would get textbox.text and send to database.

推荐答案

好吧,我需要在明天之前完成此操作,但找不到任何解决方案.所以这就是我要做的,即使它将来会对某人有所帮助.

Well, I need to get this done before tomorrow, and I ain't found any solution. So that's what I'm going to do, just if it helps somebody in the future.

1)在.aspx中创建一个隐藏标签

1) Create a hidden label in the .aspx

2)创建一个JavaScript函数,将每个textbox.text存储到label.value中(串联)

2) Create a javascript function that stores every textbox.text into label.value (concatenated)

3)将保存"按钮OnClick更改为OnClientClick ="JavaScriptFunction"

3) Change Save's button OnClick to OnClientClick="JavaScriptFunction"

4)使用旧的保存"按钮OnClick(调用后端)在.aspx中创建一个隐藏按钮

4) Create a hidden button in the .aspx with the old Save's button OnClick (the one that calls backend)

5)单击JavaScriptFunction末尾的隐藏按钮

5) Click the hidden button at the end of JavaScriptFunction

6)从后端的标签中获取存储的数据

6) Get data stored from the label in the backend

这只是为了避免回发.

这篇关于ASP.NET-无法将更改从GridView保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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