【问题标题】:GridView getting the data trough the id asp.netGridView通过id asp.net获取数据
【发布时间】:2012-04-28 01:27:36
【问题描述】:

问题确实是我确实可以通过 gridview 上的选择通过 id 获取数据,但随后我使用我在页面上实现的搜索选项,gridview 显示它获得的与结果匹配的选项,但如果我按选择它会重定向到具有错误 id 的页面,而不是获取我选择的那个的 id,它会获取位于单元格第一个位置的字段的 id。

代码如下:

protected void Page_Load(object sender, EventArgs e)
    {

        TeamGest.DBLayer.DBLTeams dbl = new TeamGest.DBLayer.DBLTeams();
        GridView1.DataSource = dbl.List();
        GridView1.DataBind();

        TeamGest.DBLayer.DBLPlayers dbl1 = new TeamGest.DBLayer.DBLPlayers();
        GridView2.DataSource = dbl1.List();
        GridView2.DataBind();
    }

    protected void MyMenu_MenuItemClick(object sender, MenuEventArgs e)
    {
        {
            MyMultiView.ActiveViewIndex = Int32.Parse(e.Item.Value);
            int i;
            for (i = 0; i <= MyMenu.Items.Count - 1; i++)
            {
                if (i == Convert.ToInt32(e.Item.Value))
                {
                    MyMenu.Items[i].Text = MyMenu.Items[i].Text;
                }
                else
                {
                    MyMenu.Items[i].Text = MyMenu.Items[i].Text;
                }
            }
        }
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = GridView1.SelectedRow;

        Response.Redirect("DetalhesClube.aspx?Id="+row.Cells[0].Text);

    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        string searchStringTeam = TextBox1.Text;
        GetTeamResults(searchStringTeam);
    }
    protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = GridView2.SelectedRow;
        Response.Redirect("DetalhesJogador.aspx?Id=" + row.Cells[0].Text);
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        string searchStringPlayer = TextBox2.Text;
        GetPlayerResults(searchStringPlayer);    
    }

【问题讨论】:

    标签: c# asp.net select gridview


    【解决方案1】:

    不要使用单元格值。这就是 DataKeys 集合的用途:

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

    在代码隐藏中,您只需要行索引:

    var rowIndex = 0;
    var ID = (int)GridView1.Rows[rowIndex]["ID"];
    

    【讨论】:

      猜你喜欢
      • 2020-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-04
      • 1970-01-01
      相关资源
      最近更新 更多