开发者

Get binding column name of templatefield's texbox

In ASP.net, I'm using textbox in templatefield's itemtemplate. I got it data-bound with no problem. But my problem is, I'm trying to write a function to find column index by its data-bounding table's column name.

Something like this :

foreach (DataControlFieldCell cell in row.Cells)
{               
  if (cell.ContainingField is BoundField)
  {
    if (((BoundField)cell.ContainingField).DataField.Equals(SearchColumnName))
    {开发者_StackOverflow
      return columnIndex;
    }
  }
  else if (cell.ContainingField is TemplateField)
  {
    //Finding column name of data-bound textbox or dropdownlist ??
  }
}


will this helps you?

DataControlFieldCell fieldCell = HeaderRow.Cells[i] as DataControlFieldCell;

DataControlField field = fieldCell.ContainingField;

string strHdrTxt = field.HeaderText.ToString()

This one?

string colName = ds.Columns[0].ColumnName;


int GetColumIndex(string name)
{
    foreach (DataControlField field in _GridView.Columns)
    {
        if (field.SortExpression == name)
        {
            return _GridView.Columns.IndexOf(field);
        }
    }

    return -1;
}  


Here are two quick options:

Option 1

Put the column name in the SortExpression property of the TemplateField. You can then access that property to determine the column name.

if (((TemplateField)cell.ContainingField).SortExpression.Equals(SearchColumnName))
{ 
    return columnIndex; 
}

Option 2 Create a custom textbox control derived from the regular textbox control, which includes a DataField property. Set the DataField property to the column name when you declare the textbox. You can later retrieve it by using FindControl on the cell to get a reference to the textbox.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜