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

本文关键字:文本 属性 修改 不能 编辑 模式 Gridview | 更新日期: 2023-09-27 18:02:55

我的gridview绑定到SqlDataSource并动态填充。我正面临着在编辑模式下修改文本框属性的问题。

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

这里,应用程序将某些文本框设置为multiline,并且所有文本框都有"input"类。与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中,我能够从文本框中获取值,但样式不起作用。如果我不使用数据绑定,样式工作,但文本框的值是空的。

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


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

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

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

我明白了

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

tb.ID = lb.Text; 

RowDataBounnd中的代码是不必要的。

解决方案为:

添加

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 { }

在row update中,从TextBox中访问值