C# Gridview编辑模式赢得';t修改文本框属性

C# Gridview编辑模式赢得';t修改文本框属性,c#,asp.net,gridview,datasource,C#,Asp.net,Gridview,Datasource,我的gridview绑定到一个SqlDataSource并填充了DynamicCaly。我在编辑模式下面临修改文本框属性的问题 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++)

我的gridview绑定到一个SqlDataSource并填充了DynamicCaly。我在编辑模式下面临修改文本框属性的问题

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();
}
protectedvoid gvData\u预渲染(对象发送方,事件参数e)
{
如果(this.gvData.EditIndex!=-1)
对于(int i=1;i
在这里,应用程序将sertain文本框设置为multiline,并且所有这些文本框都具有类“input”。 使用gvData.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();
}
protectedvoid gvData\u行更新(对象发送方,GridViewUpdateEventArgs e)
{        
字符串updateCommand=“UPDATE”+Settings.TABLE+“SET”;
TextBox tb=新的TextBox();
对于(int i=1;i0&&!Settings.PK.Contains(lb.Text))
尝试
{
Convert.ToDouble(tb.Text);
updateCommand+=lb.Text+“=”+tb.Text+”,”;
}
catch{updateCommand+=lb.Text+“=”+“+tb.Text+”,“;}
}
捕获{}
}
updateCommand=updateCommand.Remove(updateCommand.Length-2,2)+“WHERE”;
foreach(Settings.pk中的字符串pk)
{
updateCommand+=“+pk+”=:“+pk+”和”;
DS.UpdateParameters.Add(
新参数(pk,TypeCode.String,e.Keys[pk].ToString());
}
DS.UpdateCommand=UpdateCommand.Remove(UpdateCommand.Length-4,4);
gvData.DataBind();
}
在这里,应用程序将UpdateCommand设置为数据源。从文本框接收值。如果我使用gvData.DataBind();在预渲染中,我可以从文本框中获取值,但样式设置无法实现。如果我不使用数据绑定,样式设置工作,但文本框值为空

你有什么建议吗?谢谢大家


受保护的void gvData\u RowDataBound(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
for(int i=1;i0)
{
for(int i=1;i
我建议使用onrowdatabound事件处理程序来处理更改文本框的显示。

解决了这个问题

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

tb.ID = lb.Text; 
rowdatabound中的代码是不必要的

解决方案是:

在第_页中加载添加

GridView.DataBind();
行数据绑定:

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 { }
if((e.Row.RowState和DataControlRowState.Edit)>0)
for(int i=1;i

在行中,通过文本框访问值。

请验证您的输入以防止sql注入攻击,并使用
Double.TryParse()
而不是try-catch块。感谢您的评论。不过,你对这个问题有什么建议吗
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 { }