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

时间:2015-09-22 10:57:15

标签: asp.net .net gridview webforms user-controls

这是我的.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; 
        }
    }

表格从实体中正确加载,复选框变为可用,如实体所说。

问题是在编辑GridView时,如启用/禁用复选框或更改文本字段上的文本,当我按“保存”按钮时,这些值将不会保存。它们与加载表时的值相同。

我的猜测是没有任何“更新”事件,但我尝试将“OnRowEditing”添加到gridview但它永远不会触发......

1 个答案:

答案 0 :(得分:0)

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

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

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

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

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

并使用此功能

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<link href="admin.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="jquery/jquery-1.11.3.js"></script>
<script type="text/javascript" src="tinymce/tinymce.min.js"></script>
<script type="text/javascript">
tinymce.init({
  selector: "#isi",
  plugins: [
    "advlist autolink lists link image charmap print preview anchor",
    "searchreplace visualblocks code fullscreen",
    "insertdatetime media table contextmenu paste"
],
 toolbar: "insertfile undo redo | styleselect | bold italic | alignleft  aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
});

 </script>
</head>