Gridview编辑模式不会修改文本框属性

时间:2011-07-18 19:59:46

标签: c# asp.net gridview datasource

我的gridview绑定到SqlDataSource并填充dynamicaly。我在编辑模式下遇到修改文本框属性的问题。

protected void gvData_PreRender(object sender, EventArgs e)
{
    if (this.gvData.EditIndex != -1)
        for (int i = 1; i < gvData.Rows[gvData.EditIndex].Cells.Count; i++)
        {
            LinkButton lb = new LinkButton();
            TextBox tb = new TextBox();

            foreach (string pk in Settings.PK)
                if (lb.Text == pk)
                    tb.Enabled = false;
            try
            {
                lb = (LinkButton)gvData.HeaderRow.Cells[i].Controls[0];
                tb = (TextBox)
                    gvData.Rows[gvData.EditIndex].Cells[i].Controls[0];
            }
            catch { }

            if (lb.Text.Contains(Settings.AUTOEXP))
            {
                tb.TextMode = TextBoxMode.MultiLine;
                tb.Rows = 7;
            }

            tb.Text = Truncate.ToText(tb.Text);
            tb.CssClass = "input";
            tb.ID = lb.Text;
        }

    gvData.DataBind();
}

这里应用程序将sertain文本框设置为多行,并且所有文本框都有类“输入”。 使用gvData.DataBind();没有应用任何修改。如果我删除DataBind,它的工作原理。 但在这里,我面临另一个问题。我正在使用这些值来更新数据库。

protected void gvData_RowUpdating(object sender, GridViewUpdateEventArgs e)
{        
    string updateCommand = "UPDATE " + Settings.TABLE + "SET ";
    TextBox tb = new TextBox();

    for (int i = 1; i < gvData.HeaderRow.Cells.Count; i++)
    {
        LinkButton lb = new LinkButton();

        lb = (LinkButton)gvData.HeaderRow.Cells[i].Controls[0];

        try 
        {
            tb = (TextBox)gvData.Rows[e.RowIndex].Cells[i].Controls[0];

            if (tb.Text.Length > 0 && !Settings.PK.Contains(lb.Text))
                try
                {
                    Convert.ToDouble(tb.Text);
                    updateCommand += lb.Text + " = " + tb.Text + ", ";
                }
                catch { updateCommand += lb.Text + " = " + "'" + tb.Text + "', "; } 
        }
        catch { }
    }

    updateCommand = updateCommand.Remove(updateCommand.Length - 2, 2) + " WHERE";

    foreach (string pk in Settings.PK)
    {
        updateCommand += " " + pk + " = :" + pk + " AND";
        DS.UpdateParameters.Add(
            new Parameter(pk, TypeCode.String, e.Keys[pk].ToString()));
    }

    DS.UpdateCommand = updateCommand.Remove(updateCommand.Length - 4, 4);

    gvData.DataBind();
}

这里应用程序将UpdateCommand设置为数据源。值从文本框中收到。如果我使用gvData.DataBind();在PreRender中,我能够从文本框中获取值,但样式不会发生。如果我不使用DataBind,则样式wotks但文本框值为空。

你有什么建议吗?先谢谢你!


protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        for (int i = 1; i < e.Row.Cells.Count; i++)
        {
            try
            {
                LinkButton lb = (LinkButton)gvData.HeaderRow.Cells[i].Controls[0];
                lb.ToolTip = select.ToolTip(lb.Text);

                if (lb.Text.Contains(Settings.AUTOEXP))
                {
                    e.Row.Cells[i].ToolTip =
                        Truncate.ToText(e.Row.Cells[i].Text);
                    e.Row.Cells[i].Text =
                        Truncate.ToText(e.Row.Cells[i].Text
                            .Substring(0, 40)) + "...";
                }

                e.Row.DataItem = lb.Text;
                e.Row.Cells[i].Wrap = false;
            }
            catch { }

        }

        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            for (int i = 1; i < e.Row.Cells.Count; i++)
            {
                LinkButton lb = new LinkButton();
                TextBox tb = new TextBox();

                foreach (string pk in Settings.PK)
                    if (lb.Text == pk)
                        tb.Enabled = false;
                try
                {
                    lb = (LinkButton)gvData.HeaderRow.Cells[i].Controls[0];
                    tb = (TextBox)
                        e.Row.Cells[i].Controls[0];
                }
                catch { }

                if (lb.Text.Contains(Settings.AUTOEXP))
                {
                    tb.TextMode = TextBoxMode.MultiLine;
                    tb.Rows = 7;
                }

                tb.Text = Truncate.ToText(tb.Text);
                tb.CssClass = "input";
                tb.ID = lb.Text;
            }
        }
    }

    if (e.Row.RowType == DataControlRowType.Footer)
        PopulateFooter(e.Row);

}

2 个答案:

答案 0 :(得分:1)

我建议使用onrowdatabound事件处理程序来处理改变文本框的显示。

答案 1 :(得分:0)

想出来。

出于某种原因,问题出现在一行:

tb.ID = lb.Text; 

RowDataBounnd中的代码是不必要的。

解决方案是:

在Page_Load中添加

GridView.DataBind();

的RowDataBound:

if ((e.Row.RowState & DataControlRowState.Edit) > 0)
   for (int i = 1; i < e.Row.Cells.Count; i++)
       try
       {
          LinkButton lb = (LinkButton)gvData.HeaderRow.Cells[i].Controls[0];
          TextBox tb = (TextBox)e.Row.Cells[i].Controls[0];

          if (lb.Text.Contains(Settings.AUTOEXP))
          {
              tb.TextMode = TextBoxMode.MultiLine;
              tb.Rows = 7;
          }

          tb.CssClass = "input";
       }
       catch { }

在RowUpdating中,可以从TextBox访问该值。