Gridview 用户控件无法获取行数据

Gridview user-controls cant get row data

这是我的 .ascx 文件:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" Width="905px" 
OnRowDataBound="OnRowDataBound" OnRowEditing="GridView1_RowEditing" style="margin-right: 4px" >
<AlternatingRowStyle BackColor="White" />
<Columns>
    <asp:CheckBoxField DataField="IsChecked" HeaderText="" SortExpression="IsChecked" />
    <asp:BoundField DataField="Description" HeaderText="Benefit"  ><ItemStyle HorizontalAlign="Left" /></asp:BoundField>

    <asp:TemplateField HeaderText="Description">
    <ItemTemplate>
    <asp:TextBox runat="server" Text='<%# Eval("Detail") %>' Width="70px" ID="Description"  OnTextChanged="DescriptionTextChanged" />
    </ItemTemplate>
    </asp:TemplateField> 

    <asp:TemplateField HeaderText="Ammount">
    <ItemTemplate>
    <asp:TextBox runat="server" Text='<%# Eval("Ammount") %>' Width="50px" ID="Ammount"  />
    </ItemTemplate>
    </asp:TemplateField> 

    <asp:CheckBoxField DataField="Optional" HeaderText="Optional" SortExpression="Optional" />

    <asp:TemplateField HeaderText="Copayment">
    <ItemTemplate>
    <asp:TextBox runat="server" Text='<%# Eval("Copayment") %>' Width="40px" ID="Copayment" />
    </ItemTemplate>
    </asp:TemplateField>

    <asp:CheckBoxField DataField="Complementary" HeaderText="Complementary" SortExpression="Complementary" />
    <asp:CheckBoxField DataField="Covered" HeaderText="Covered" SortExpression="Covered" />
</Columns>

这是保存按钮。

<asp:Button ID="myButton" Text="Save" OnClick="Save_OnClick" runat="server" />

共8个字段。这是 C# 代码。

protected void Page_Load(object sender, EventArgs e)
    {
     ... //get data etc etc
         GridView1.DataSource = myResults;
         GridView1.DataBind();
    }

默认情况下,我在网格上的所有字段都被禁用,所以我这样做是为了启用我需要的字段:

    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            CheckBox checkBox = e.Row.Cells[0].Controls[0] as CheckBox;
            checkBox.Enabled = true;

            checkBox = e.Row.Cells[4].Controls[0] as CheckBox;
            checkBox.Enabled = true;

            checkBox = e.Row.Cells[6].Controls[0] as CheckBox;
            checkBox.Enabled = true;

            checkBox = e.Row.Cells[7].Controls[0] as CheckBox;
            checkBox.Enabled = true;
        }
    }

这是循环遍历 GridView 并收集信息的按钮事件。

    public void Save_OnClick(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox chk = row.Cells[0].Controls[0] as CheckBox;
            bool isChecked = chk.Checked;
            string benefit = row.Cells[1].Text;
            string description = ((TextBox)row.FindControl("Description")).Text;
            string ammount = ((TextBox)row.FindControl("Ammount")).Text;
            chk = row.Cells[4].Controls[0] as CheckBox;
            bool optional = chk.Checked;
            string copayment = ((TextBox)row.FindControl("Copayment")).Text;
            chk = row.Cells[6].Controls[0] as CheckBox;
            bool complementary = chk.Checked; 
            chk = row.Cells[7].Controls[0] as CheckBox;
            bool covered = chk.Checked; 
        }
    }

Table 从实体中正确加载,并且复选框变得可用,正如实体所说。

问题 是当像 enabling/disabling 复选框一样编辑 GridView 或更改文本字段上的文本时,当我按下 "Save" 按钮时,这些值不会被保存.它们与加载 table 时的值相同。

我的猜测 是没有任何 "update" 事件,但我尝试将 'OnRowEditing' 添加到 gridview 但它从未触发...

问题出在您的页面加载事件中。您需要绑定数据如下

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    { 
         GridView1.DataSource = myResults;
         GridView1.DataBind();
     }
}

因为当您单击按钮时,系统会自动加载并覆盖已编辑的值。

创建一个方法来绑定 gridview 中的数据并在需要时调用它。 喜欢

public void BindDatatoGrid
{
    ... //get data etc etc
     GridView1.DataSource = myResults;
     GridView1.DataBind();
}

并使用这个功能

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    { 
         BindDatatoGrid();
     }
}