首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

gridview隐藏了列后,要在什么事件中取得模板列中的控件值?该如何解决

2012-01-16 
gridview隐藏了列后,要在什么事件中取得模板列中的控件值?asp:TemplateFieldHeaderText 小图片 SortEx

gridview隐藏了列后,要在什么事件中取得模板列中的控件值?
<asp:TemplateField   HeaderText= "小图片 "   SortExpression= "ImgUrlSml ">
                                      <EditItemTemplate>
                                              <asp:Label   ID= "Label2 "   runat= "server "   Text= ' <%#   Eval( "ImgUrlSml ")   %> '> </asp:Label>
                                      </EditItemTemplate>
                                      <ItemTemplate>
                                              <asp:Label   ID= "Label1 "   runat= "server "   Text= ' <%#   Bind( "ImgUrlSml ")   %> '> </asp:Label>
                                      </ItemTemplate>
                              </asp:TemplateField>
++++++++++++++++++++++++++++++++++++++++

        Protected   Sub   GridView1_RowDataBound(ByVal   sender   As   Object,   ByVal   e   As   System.Web.UI.WebControls.GridViewRowEventArgs)   Handles   GridView1.RowDataBound
                If   e.Row.RowType   =   DataControlRowType.DataRow   Then
                        e.Row.Cells(8).Visible   =   False
                End   If
        End   Sub

问题一:怎样隐藏HeaderText?
问题二:如何取得Label1的值?

[解决办法]
问题一:怎样隐藏HeaderText?
======
你执行了
e.Row.Cells(8).Visible = False
还可以看到 HeaderText ???


问题二:如何取得Label1的值?
======
你准备在哪里获取?


// 外部 Button
protected void Button1_Click(object sender, EventArgs e)
{
int cellIndex = 0; // 单元格索引
foreach (GridViewRow row in GridView1.Rows) {
CheckBox chk = row.FindControl( "MyCheckBoxID ") as CheckBox;
Label lbl = (Label)row.FindControl( "MyLabelID ");
// OR
//int cellIndexOfCheckBox = 1; // 表示 CheckBox 所在列索引
//CheckBox chk = row.Cells[cellIndexOfCheckBox].Controls[0] as CheckBox;
if (chk.Checked) {
Label1.Text = row.Cells[cellIndex].Text;
}
}
}

// RowDataBound 事件
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
CheckBox chk = e.Row.FindControl( "MyCheckBoxID ") as CheckBox;
Label lbl = (Label)e.Row.FindControl( "MyLabelID ");
}

// RowCommand 事件
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{


Control cmdSource = e.CommandSource as Control;
GridViewRow row = cmdSource.NamingContainer as GridViewRow;
int rowIndex = row.RowIndex;
int cellIndex = 0;
Label1.Text = row.Cells[cellIndex].Text;
CheckBox chk = row.FindControl( "MyCheckBoxID ") as CheckBox;
Label lbl = (Label)row.FindControl( "MyLabelID ");
}


[解决办法]
GridView1.Columns[8].Visible=false;

热点排行