GridView RowUpdating在第二页中不起作用

时间:2017-01-27 08:48:38

标签: javascript c# html asp.net gridview

我在 <xsl:template name="style-attributes"> <xsl:choose> <xsl:when test="@style | @class"> <xsl:copy-of select="@style | @class"/> </xsl:when> <xsl:when test="@*[f:is-style-attribute(.)]"> <xsl:attribute name="style"> <xsl:for-each select="@*[f:is-style-attribute(.)]"> <xsl:if test="position() gt 1">; </xsl:if> <xsl:apply-templates select="." mode="style-attribute"/> </xsl:for-each> </xsl:attribute> </xsl:when> </xsl:choose> </xsl:template> <xsl:function name="f:is-style-attribute" as="xs:boolean"> <xsl:param name="att" as="attribute(*)"/> <xsl:variable name="n" select="local-name($att)"/> <xsl:sequence select="namespace-uri($att) = '' and $n = ('class', 'style', 'border', 'width', 'cellspacing', 'padding', 'cellpadding', 'align', 'valign')"/> </xsl:function> <xsl:function name="f:units" as="xs:string"> <xsl:param name="value" as="xs:string"/> <xsl:sequence select="if ($value castable as xs:integer) then concat($value, 'px') else $value"/> </xsl:function> <xsl:template match="@border" mode="style-attribute">border:<xsl:value-of select="f:units(.)"/> solid</xsl:template> <xsl:template match="@width" mode="style-attribute">width:<xsl:value-of select="f:units(.)"/></xsl:template> <xsl:template match="@align" mode="style-attribute">text-align:<xsl:value-of select="."/></xsl:template> <xsl:template match="@valign" mode="style-attribute">vertical-align:<xsl:value-of select="."/></xsl:template> <xsl:template match="@cellspacing" mode="style-attribute">border-spacing:<xsl:value-of select="."/></xsl:template> <xsl:template match="@padding" mode="style-attribute">padding:<xsl:value-of select="f:units(.)"/></xsl:template> <xsl:template match="@cellpadding" mode="style-attribute">padding:<xsl:value-of select="f:units(.)"/></xsl:template> ... etc (as required) 命令

期间使用以下代码更新gridview中的值
OnUpdating

此代码在网格视图的第一页中正常工作,但是当我尝试编辑第二页时它不起作用。当我单击编辑按钮时,正在加载相应的行编辑模板。当我单击保存而不是保存到第二页时,第一页行被修改。例如,如果我在第二页的第二行中单击编辑,并且在我单击更新后,第一页的第三行被修改为新值而不是第二页的第二行。

我的RowEditing和CancelEditing代码如下,

protected void grid_view_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = (GridViewRow)grid_view.Rows[e.RowIndex];
    int number = Convert.ToInt32(((TextBox)row.Cells[3].FindControl("flatnumber")).Text);
    string type = ((DropDownList)row.Cells[4].FindControl("flattype")).Text;
    int max = Convert.ToInt32(((DropDownList)row.Cells[5].FindControl("flatvacancy")).Text);
    string flatID = ((Label)row.Cells[0].FindControl("flatid")).Text;
    DataTable dt = (DataTable)ViewState["Table"];
    DataRow[] datarow = dt.Select("ID='" + flatID + "'");
    dt.Rows[e.RowIndex].BeginEdit();
    dt.Rows[e.RowIndex]["Number"] = number;
    dt.Rows[e.RowIndex]["Type"] = type;
    dt.Rows[e.RowIndex]["Vacancy"] = max;
    dt.Rows[e.RowIndex].EndEdit();
    dt.AcceptChanges();
    ViewState["Table"] = dt;
    grid_view.EditIndex = -1;
    grid_view.DataSource = dt;
    grid_view.DataBind();
}

1 个答案:

答案 0 :(得分:1)

您可以仅使用e.RowIndex访问更新的值,但要在Datatable中进行更改,您必须将grid_view.PageIndexgrid_view.PageSize的产品添加到e.RowIndex以进行更改在实际的行中。

protected void grid_view_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = (GridViewRow)grid_view.Rows[e.RowIndex];
    int number = Convert.ToInt32(((TextBox)row.Cells[3].FindControl("flatnumber")).Text);
    string type = ((DropDownList)row.Cells[4].FindControl("flattype")).Text;
    int max = Convert.ToInt32(((DropDownList)row.Cells[5].FindControl("flatvacancy")).Text);
    string flatID = ((Label)row.Cells[0].FindControl("flatid")).Text;
    DataTable dt = (DataTable)ViewState["Table"];
    int row1=e.RowIndex+(grid_view.PageIndex*grid_view.PageSize)
    dt.Rows[row1].BeginEdit();
    dt.Rows[row1]["Number"] = number;
    dt.Rows[row1]["Type"] = type;
    dt.Rows[row1]["Vacancy"] = max;
    dt.Rows[row1].EndEdit();
    dt.AcceptChanges();
    ViewState["Table"] = dt;
    grid_view.EditIndex = -1;
    grid_view.DataSource = dt;
    grid_view.DataBind();
}