【问题标题】:Gridview user-controls cant get row dataGridview 用户控件无法获取行数据
【发布时间】:2015-09-22 10:57:15
【问题描述】:

这是我的 .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 但它永远不会触发......

【问题讨论】:

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


    【解决方案1】:

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

    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();
         }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-08
      相关资源
      最近更新 更多