【问题标题】:How do I get the value from a hidden column?如何从隐藏列中获取值?
【发布时间】:2013-06-15 07:03:27
【问题描述】:

这是我拥有的代码,它与可见列完美配合。但是,我需要能够获取某些不可见列的值:

protected void btnSearchForSchedules_Click(object sender, EventArgs e)
        {
            var ds = new DataSet();
            var schedulesTable = new DataTable();

            foreach (GridViewRow gvr in gvShows.Rows)
            {
                if (gvr.RowType == DataControlRowType.DataRow)
                {
                    if (((CheckBox) gvr.FindControl("cbSelect")).Checked)
                    {
                        string baseURL = WebConfigurationManager.AppSettings["SchedulesAPI"];

                        string dataSource = gvr.Cells[1].Text;
                        string showId = gvr.Cells[2].Text; //this column is hidden so I'm getting a NULL here
                        string episodeId = gvr.Cells[4].Text;

                        string callingURL = baseURL + "?datasource=" + dataSource + "&showid=" + showId;

                        if (episodeId != " ")
                        {
                            callingURL = callingURL + "&episodeId=" + episodeId;
                        }

                        schedulesTable = Transporter.GetDataTableFromAPI(callingURL);

                        ds.Tables.Add(schedulesTable);
                    }
                }
            }

谁能给我解释一下我会怎么做:

字符串 showId = gvr.Cells[2].Text;

如果该列不可见?

【问题讨论】:

标签: c# asp.net gridview


【解决方案1】:

当您想要获取隐藏列值时,您可以在 aspx 页面中为该 GridView 指定 DataKeyNames 属性。

<asp:GridView ID="GridView1" runat="server" DataKeyNames="column1" ...>

然后您可以在后面的代码中检索该列值。

string showId = (string) GridView1.DataKeys[2].Value.ToString();

【讨论】:

    猜你喜欢
    • 2019-11-01
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-22
    • 2014-04-10
    • 2021-10-16
    • 2011-05-06
    相关资源
    最近更新 更多