简体   繁体   English

ASP.NET - 无法将更改从 GridView 保存到数据库

[英]ASP.NET - Can't save changes from GridView to Database

I have a GridView.我有一个 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();

When I click the "Edit" button, it adds textbox controls in the required cells, and put the cell.text on textbox.text:当我单击“编辑”按钮时,它会在所需的单元格中添加文本框控件,并将 cell.text 放在 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);
        }
    }
}

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 的单元格中没有任何文本框控件。

I've tried to save the GridView into a Session variable just after textboxes are created.在创建文本框后,我尝试将 GridView 保存到 Session 变量中。 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.它可以工作,但问题仍然存在......用户对这些文本框的输入没有保存到该 Session 变量中,因此当单击“保存”按钮时,它们上不存在任何 textbox.text 。

Any suggestions on how to do it please?请问有什么建议吗?

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. Save 按钮的代码与 Edit 按钮的代码几乎相同,只是不添加文本框,而是获取 textbox.text 并发送到数据库。

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) Create a hidden label in the.aspx 1)在.aspx中创建一个隐藏的label

2) Create a javascript function that stores every textbox.text into label.value (concatenated) 2)创建一个 javascript function 将每个 textbox.text 存储到 label.value 中(连接)

3) Change Save's button OnClick to OnClientClick="JavaScriptFunction" 3) 将保存按钮 OnClick 更改为 OnClientClick="JavaScriptFunction"

4) Create a hidden button in the.aspx with the old Save's button OnClick (the one that calls backend) 4)使用旧的保存按钮OnClick(调用后端的那个)在.aspx中创建一个隐藏按钮

5) Click the hidden button at the end of JavaScriptFunction 5)点击JavaScriptFunction末尾的隐藏按钮

6) Get data stored from the label in the backend 6)从后端的label获取存储的数据

That's just to avoid the postback.这只是为了避免回发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM