【问题标题】:Get BoundField value in GridView RowDataBound event在 GridView RowDataBound 事件中获取 BoundField 值
【发布时间】:2020-12-24 20:54:27
【问题描述】:

我想在 RowDataBound 函数中取 LangId 值。如何做到这一点?

<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;
    }
}

【问题讨论】:

    标签: c# asp.net gridview boundfield


    【解决方案1】:

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

    <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
        }
    }
    

    【讨论】:

      【解决方案2】:

      你可以通过这个得到它:

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

      ColumnIndexOfYourBoundField 表示如果你的列是第一列而不是它的索引是 0,如果它是第二列而不是它的 1,依此类推。

      【讨论】:

        【解决方案3】:

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

        var myItem = (MyType)e.Row.DataItem;
        // myItem.LangId now available
        

        【讨论】:

          【解决方案4】:

          .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;          
              }
          }
          

          【讨论】:

            【解决方案5】:

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

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                dynamic data = e.Row.DataItem;
                int LangId = data.LangId;
                // do your code here
            }
            

            【讨论】:

              【解决方案6】:
               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");
              }
              }
              

              【讨论】:

                猜你喜欢
                • 2011-03-06
                • 1970-01-01
                • 2010-09-12
                • 1970-01-01
                • 2014-08-30
                • 1970-01-01
                • 2011-05-24
                • 1970-01-01
                • 2018-08-13
                相关资源
                最近更新 更多