Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在RowUpdate事件GridView中获取单元格值?_C#_Asp.net - Fatal编程技术网

C# 如何在RowUpdate事件GridView中获取单元格值?

C# 如何在RowUpdate事件GridView中获取单元格值?,c#,asp.net,C#,Asp.net,我想在gridview行更新事件中获取单元格的新值: roles.RoleName = gvRoles.Rows[e.RowIndex].Cells[GetColumnIndexByName(row, "RoleName")].Text; 但该值为空 我已经检查了e.NewValues,它是一个有序字典。它包含新更改的值。如何检索要更新的新值 aspx设计是: <asp:GridView ID="gvRoles" DataKeyNames="RoleId" runat="server"

我想在gridview行更新事件中获取单元格的新值:

 roles.RoleName = gvRoles.Rows[e.RowIndex].Cells[GetColumnIndexByName(row, "RoleName")].Text;
但该值为空

我已经检查了e.NewValues,它是一个有序字典。它包含新更改的值。如何检索要更新的新值

aspx设计是:

<asp:GridView ID="gvRoles" DataKeyNames="RoleId" runat="server" 
    AutoGenerateColumns="False" GridLines="Vertical" CssClass="table 
    table-striped table-bordered" AutoGenerateEditButton="True" 
    OnRowCancelingEdit="gvRoles_RowCancelingEdit" OnRowUpdating="gvRoles_RowUpdating" 
    OnRowEditing="gvRoles_RowEditing">
    <Columns>
        <asp:BoundField DataField="RoleId" HeaderText="RoleId" ReadOnly="True" />
        <asp:BoundField DataField="RoleName" HeaderText="RoleName" ReadOnly="false" />
        <asp:BoundField DataField="Role_Description"  HeaderText="Role Description"  ReadOnly="false" />
        <asp:TemplateField HeaderText="RoleStatus">
            <EditItemTemplate>
                <asp:DropDownList ID="ddlStatus" runat="server" SelectedValue='<%# Bind("Role_Status") %>' >
                <asp:ListItem>True</asp:ListItem>
                <asp:ListItem>False</asp:ListItem>
                </asp:DropDownList>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="lblRoleStatus" runat="server" 
                    Text='<%# Bind("Role_Status") %>'>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

真的
假的
试试这个:

protected void gvRoles_RowUpdating(object sender, GridViewUpdateEventArgs e)
{    
  GridViewRow row = gvRoles.Rows[e.RowIndex];
  TextBox txtBox= (TextBox)(row.Cells[1].Controls[0]);
  if(txtBox!=null)
  {
   String str = txtBox.Text;
  }
}

我发现的最简单的方法是发现
e.NewValues
。正如我上面提到的,它是一个有序的字典,只有我需要管理它

我需要从这个字典中检索新的值,我是这样做的

if (!string.IsNullOrEmpty(e.NewValues["RoleName"].ToString()))
            {
                roles.RoleName = e.NewValues["RoleName"].ToString();
            }
如果您有模板字段,它也适用于它

  if (!string.IsNullOrEmpty(e.NewValues["Role_Status"].ToString()))
        {
            roles.Role_Status = Convert.ToBoolean(e.NewValues["Role_Status"].ToString());
        }
这是我发现的最简单的事情。在此之前,我只想使用Find控件和casting,然后检索所有批次代码


还有e.OldValues有序字典。每个人都可以用它来比较新值和旧值。如果值相同,它们可以通知用户更改值(给出新的单元格值)。

经过长时间的艰苦搜索,我找到了一篇很好的文章,解决了我的问题。如果您是在post上绑定的,请查看页面负载,然后在您能够访问这些值之前更新这些值


有关更多详细信息,请点击此链接-->

仅代码答案,这可能是一个可行的解决方案,通常会通过一些解释加以改进。请考虑评论或解释你建议的代码是如何工作的。当这个链接可以回答这个问题时,最好把答案的重要部分包含在这里,并提供链接以供参考。如果链接页面发生更改,则仅链接的答案可能无效
  if (!string.IsNullOrEmpty(e.NewValues["Role_Status"].ToString()))
        {
            roles.Role_Status = Convert.ToBoolean(e.NewValues["Role_Status"].ToString());
        }