2

我想LangId在 RowDataBound 函数中取值。这个怎么做?

<asp:BoundField DataField="LangId" HeaderText="LangId" Visible="false" />

protected void grdList_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // need LangId
        ImageButton imgBtn = (ImageButton)e.Row.FindControl("imgBtnDelete");
        imgBtn.Visible = false;
    }
}
4

6 回答 6

4

有几种方法可以做到这一点。也许更多。

<asp:BoundField DataField="LangId" HeaderText="LangId" Visible="false" />

protected void grdList_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
       string langId = e.Row.Cells[columnIndex].Text; // one of the ways

       string langId2 = DataBinder.Eval(e.Row.DataItem, "LangId").ToString(); // one of the other ways
    }
}
于 2013-04-11T07:55:01.883 回答
1

您可以通过以下方式获得它:

string str = e.Row.Cells[CloumnIndexOfYourBoundField].Text;

ColumnIndexOfYourBoundField表示如果您的列是第一列而不是它的索引是 0,如果它的第二列比它的 1 等等。

于 2013-04-11T07:45:18.747 回答
0

使用动态类型,您可以访问记录中的字段:

if (e.Row.RowType == DataControlRowType.DataRow)
{
    dynamic data = e.Row.DataItem;
    int LangId = data.LangId;
    // do your code here
}
于 2014-06-14T18:50:11.190 回答
0

.ASPX 文件:

<ItemTemplate>
    <asp:ImageButton ID="imgEdit" runat="server" AlternateText="Edit"
        CommandArgument='<%# Eval("LangId") %>' CommandName="DeleteLedger" ToolTip="Delete"
        ImageUrl="~/App_Themes/DefaultClient/images/Delete.png" />
</ItemTemplate>

.CS 文件:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    DataRowView dataRow = ( DataRowView ) e.Item.DataItem;
    string strLangId = dataRow["LangId"].ToString();
    DataTable dtData1 = 
    objAccountTypeBAL.ChkLedgerRelation(Convert.ToInt64(strLangId ), objSession.BranchId);

    if (dtData1.Rows.Count > 0)
    {
        ImageButton img = (ImageButton)item["Delete"].Controls[0];
        img.Visible = false;          
    }
}
于 2013-04-11T07:42:49.547 回答
0

数据对象e.Row.DataItem在当时可用。您只需要将其转换为适当的类型。

var myItem = (MyType)e.Row.DataItem;
// myItem.LangId now available
于 2013-04-11T07:33:06.407 回答
0
 protected void GrdEmplistFromAtt_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Attributes.Add("onmouseover", "MouseEvents(this, event)");
            e.Row.Cells[0].Attributes.Add("onmouseout", "MouseEvents(this, event)");
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lbl_GrdCode = (Label)e.Row.FindControl("lblGrdCode");
}
}
于 2020-12-24T12:54:27.850 回答