【问题标题】:pass ID from grid view in linq从 linq 中的网格视图传递 ID
【发布时间】:2016-10-16 04:55:46
【问题描述】:

我尝试从网格视图中获取ID。在网格视图中有来自另一个表的数据,linq 查询中的数据来自另一个表。所以两个表中都有ID ..table1 中的MID 和table2(which is in grid view e.g. <asp:BoundField DataField="ID" HeaderText="ID"/> ) 中的ID .. 我从网格视图中得到ID 现在我尝试代表这个ID 获取数据从另一个表,所以如何从linq 的网格视图中传递ID

我试试这个

protected void GridView1_select(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            int index = Convert.ToInt32(e.CommandArgument.ToString());

            string ID = GridView1.Rows[index].Cells[0].Text.TrimEnd();
          GetVoiliations(Convert.ToInt32(ID));

        }

    }

[WebMethod]
    public static string GetVo(int ID)
    {
        try
        {
            T1 DB = new T1();
            string data = "[";
            data+="ID:'"+ID+"',";
            var re = (from veh in DB.tblV
                      join reg in DB.tblr on veh .MID equals reg.ID  
                    where reg.ID==ID  && !(vehvoila.VName == "")
                      group veh by veh .VName into g
                      select new
                      {
                          Name = g.Key,
                          cnt = g.Select(t => t.Name).Count()
                      }).ToList();


            data += re.ToList().Select(x => "['" + x.Name + "'," + x.cnt + "]")
              .Aggregate((a, b) => a + "," + b);

            data += "]";


            return data;
        }
        catch (Exception)
        {
            throw new Exception();

        }

    }


   <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
        GridLines="None" onpageindexchanging="GridView1_PageIndexChanging" 
        onselectedindexchanged="GridView1_SelectedIndexChanged"  
        AutoGenerateColumns="False" AllowPaging="True"  OnRowCommand="GridView1_select"
        onrowdatabound="GridView1_RowDataBound">
        <Columns>
        <asp:BoundField DataField="ID" HeaderText="ID"/>          
        <asp:BoundField DataField="OwnerName" HeaderText="Owner Name"/> 
        <asp:ButtonField ButtonType="Link" CommandName="Select" Text="Select" />
        </Columns>
    </asp:GridView>

更新 JSON

   <script type="text/javascript">

    var strArray = "[['aasd', 9],['Kiasdwi', 3],['Grapasdes (bunch)', 1]]";

    $(function () {

        $.ajax({
            type: "POST",
            url: "WebForm1.aspx/GetVo", // the method we are calling
            data: JSON.stringify({ ID: $('#ID').val() }),

            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: true,
            cache: false,
            success: function (result) {

                alert(result.d);
                //start
                strArray = result.d;
                var myarray = eval(strArray);
                $('#container').highcharts({
                    chart: {
                        type: 'pie',
                        options3d: {
                            enabled: true,
                            alpha: 45
                        }
                    },
                    title: {
                        text: 'Contents of Highsoft\'s weekly fruit delivery'
                    },
                    subtitle: {
                        text: '3D donut in Highcharts'
                    },
                    plotOptions: {
                        pie: {
                            innerSize: 100,
                            depth: 45
                        }
                    },
                    series: [{
                        name: 'Delivered amount',
                        data: myarray
                    }]
                });

                //end
            }
        });

  });

        </script>

警告框中的数据是

[ID:'1',['SSAFTER',1],[' Belt',4]

当我单击从网格视图中选择时,我在警报框中得到了数据,我在 json 中写入警报框只是为了测试我是否得到正确的数据现在我得到了我想要的数据,但没有显示高图表?

有什么办法吗?

【问题讨论】:

  • 发生了什么错误
  • 你会在这一行得到准确的 ID 字符串 ID = GridView1.Rows[index].Cells[0].Text.TrimEnd();
  • 没有错误..实际上我尝试在 highchart 上获取数据..当我点击选择时,整个数据显示在图表上..我想要根据 highchart 上的 ID 的特定数据..我用饼图。
  • 是的,我知道我得到了准确的 ID。但我没有根据 ID 获取数据
  • GetVoiliations() 方法是做什么的?

标签: c# asp.net linq gridview


【解决方案1】:

好吧,您应该认真考虑一下代码的结构,因为“一体化”方法使事情变得复杂 => 一种检索数据的方法,一种将其解析为 Json 的方法,至少。

手动生成 Json 字符串是……有点难看,不是吗?你不能使用 Json.Net 或类似的东西吗?

无论如何,您可以创建一个新方法,在 id 上使用过滤器(我不知道 Id 是数据库中的 int 还是字符串,如果需要,请更改)。但这会重复大部分实际的方法代码,这是不好的。

 [WebMethod]
        public static string GetVoiliationsById(string id)
        {
            try
            {
           T1 DB = new T1();

           string data = "[";
           var re = (from ve in table1 i

                      where !(ve .VName == "") 
                      //add a new filter
                      && ve.Id == id
                      group ve by ve .VName into g
                      select new
                      {
                          Name = g.Key,
                          cnt = g.Select(t => t.Name).Count()
                      }).ToList();
            data += re.ToList().Select(x => "['" + x.Name + "'," + x.cnt + "]")
              .Aggregate((a, b) => a + "," + b);
            data += "]";
            return data;
        }
        catch (Exception)
        {
            throw new Exception();

        }

    }

或者你可以在你的方法中使用默认参数

[WebMethod]
    public static string GetVoiliations(string id = null)
    {
        try
        {
        T1 DB = new T1();

        string data = "[";
       var re = (from ve in table1 i

                  where !(ve .VName == "")
                  //add new filter
                  && (id == null || ve.Id == id)
                  group ve by ve .VName into g
                  select new
                  {
                      Name = g.Key,
                      cnt = g.Select(t => t.Name).Count()
                  }).ToList();
        data += re.ToList().Select(x => "['" + x.Name + "'," + x.cnt + "]")
          .Aggregate((a, b) => a + "," + b);
        data += "]";
        return data;
    }
    catch (Exception)
    {
        throw new Exception();

    }

}

然后您可以致电GetVoiliations(ID)GetVoiliationsById(ID),并过滤数据。

【讨论】:

  • 是的,我在你的帖子之前编辑了 getvol 方法。现在请检查更新问题
  • 我如何在 json 上传递这个 ID?因为这个ID来自gridview ..我粘贴json请检查
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-04
  • 1970-01-01
  • 2015-08-17
相关资源
最近更新 更多