日期:2014-05-18  浏览次数:20529 次

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 GridVi