从“查看全部”更改为单个视图

时间:2016-03-30 01:44:45

标签: c# asp.net gridview

试图将其从(拉动所有记录)“全部”OnClick转换为“单个”OnClick。 这是从gridview中提取数据所以我在每一行都添加了一个按钮,我希望从行记录中提取数据并查询特定行。任何帮助将不胜感激,因为它没有运行foreach而是获取行特定数据。感谢。

protected void btnViewAll_Click(object sender, EventArgs e)
{
    string id = HttpContext.Current.Request.Headers["id"];
#if DEBUG
    if (id == null)
        id = "111"; // Use my own id for testing locally
#endif
    using (aDataContext dc = new aDataContext())
    {
        var query = (from a in dc.aLists
                     where a.ID == id
                     select new
                     {
                         a.aNumber,
                         a.bNumber,
                         a.cNumber,
                         a.dNumber,
                     }
                         );

        List<aListItem> List = new List<aListItem>();
        foreach (var queryItem in query)
        {
            aList.Add(new aListItem()
            {
                aNumber = queryItem.aNumber,
                bNumber = queryItem.bNumber,
                cNumber = queryItem.cNumber,
                dNumber = queryItem.dNumber
            });
        }
        Session["aList"] = aList;
    }
    Session["Database"] = null;
    Response.Redirect("Viewer.aspx" + "?UseList=true");
}

2 个答案:

答案 0 :(得分:0)

使用RowDatabound进行gridview控件,如:

protected void OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex);
        e.Row.ToolTip = "Click to select this row.";
    }
}

参考:

  

http://www.aspsnippets.com/Articles/Selecting-GridView-Row-by-clicking-anywhere-on-the-Row.aspx

答案 1 :(得分:0)

我们可以使用GridView的编辑模板,当单击一行按钮时,我们可以轻松地从您获取ID的行索引中获取,然后根据需要查询数据

 protected void GridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView.EditIndex = e.NewEditIndex;
    }
protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {


       string id = GridView.DataKeys[e.RowIndex].Value.ToString();
    }

我希望它会有所帮助