从GridViewRow ASP.NET获取项目 [英] Get Item from GridViewRow ASP.NET

查看:115
本文介绍了从GridViewRow ASP.NET获取项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GridView,它将一个DataSource绑定到SQL数据库中,网格有一个带有复选框的列,因此我可以选择其中的几个项目(实际上是行)。我想在这里做的是对每个选定行中的项目进行一些更新,但是经过一些搜索后,我无法找到如何访问该行中的项目,但我通过DataItem工作,但它给了我一个空值。



编辑:为了简短起见,我有一个从DataSource构建的GridView,所以基本上每行都代表一个Object,在其中一行中选中一个复选框,我希望能够获取与该Row相关的对象,实现该目标的最简单方法是什么?



DataBinding on Page_Load:
$ b


  if(!Page.IsPostBack)
{
gvUnusedAccessories .DataSource = currentContext.Items.OfType< Accessory>()。其中​​(ac => ac.Item_Parent_Id ==((PhoneLine)currentItem).Parent.Item_Id);
gvUnusedAccessories.AutoGenerateColumns = false;
gvUnusedAccessories.DataBind();
}


当我按下更新按钮,它实际上浏览行,如果该行有一个复选框,它会进行更新:


  protected void btnAddToMobile_Click(object sender,EventArgs e)
{
foreach(GridViewRow row in gvUnusedAccessories.Rows)
{
if(((CheckBox)row.FindControl(chkSelect ))。Checked)
{
((Accessory)row.DataItem).PhoneLine_Id = currentItem.Item_Id;






$ b <

 < asp:GridView ID =gvUnusedAccessoriesrunat =server > 
<列>
< asp:CommandField ShowSelectButton =True/>
< asp:TemplateField HeaderText =Select>
< ItemTemplate>
< asp:CheckBox ID =chkSelectrunat =serverAutoPostBack =True/>
< / ItemTemplate>
< / asp:TemplateField>
< asp:BoundField DataField =Item_IdHeaderText =IDReadOnly =True/>
< asp:BoundField DataField =Item_NameHeaderText =NameReadOnly =True/>
< asp:BoundField DataField =AccessoryModelHeaderText =ModèleReadOnly =True/>
< asp:BoundField DataField =AccessoryBrandHeaderText =MarqueReadOnly =True/>
< /列>
< / asp:GridView>

有关如何访问Row中包含的Object的任何提示?
我知道我实际上可以获得ID,然后对我的数据库执行SQL请求,但似乎有点沉重,必须有更好的解决方案。

除非在数据绑定完成后将数据源存储在ViewState或会话中,否则会丢失数据源。 DataItem将始终为空,除非您这样做。正因为如此,我经常发现自己将数据源存储在视图状态中。当然这意味着你的页面尺寸会变得更大。您所陈述的另一个选项是使用某种主键重新查询数据源。根据您的需求,我不能说哪个选项更好。我倾向于倾向于视图状态,而不是第二次数据库调用,因为这可能是一个昂贵的调用


I have a GridView which I Bind a DataSource to it from a SQL Database, the grid has a column with a checkbox in it so I can "select" a few Item in it (Rows actually). What I want here is to do some updates on the Item in each selected rows, but after some search I can't find how to access the Item in a Row, I though DataItem would work, but it's giving me a Null.

Edit: To make it short, I have a GridView which is built from a DataSource, so basically, each rows represent an Object, when I have a checkbox checked in one of the rows, I want to be able to grab the Object related to that Row, what is the easiest way to achieve that?

The DataBinding on Page_Load:

  if (!Page.IsPostBack)
    {
        gvUnusedAccessories.DataSource = currentContext.Items.OfType<Accessory>().Where(ac => ac.Item_Parent_Id == ((PhoneLine)currentItem).Parent.Item_Id);
        gvUnusedAccessories.AutoGenerateColumns = false;
        gvUnusedAccessories.DataBind();
    }

The event when I press the Update Button, It actually browse the rows and if the row has a checked box it's gonna do the update:

protected void btnAddToMobile_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in gvUnusedAccessories.Rows)
    {
        if(((CheckBox)row.FindControl("chkSelect")).Checked)
        {
            ((Accessory)row.DataItem).PhoneLine_Id = currentItem.Item_Id;
        }
    }
}

And here's my GridView in the .aspx :

 <asp:GridView ID="gvUnusedAccessories" runat="server">
    <Columns>
        <asp:CommandField ShowSelectButton="True" />
        <asp:TemplateField HeaderText="Select">
            <ItemTemplate >
                <asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="True"/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Item_Id" HeaderText="ID" ReadOnly="True"/>
        <asp:BoundField DataField="Item_Name" HeaderText="Name" ReadOnly="True"/>  
        <asp:BoundField DataField="AccessoryModel" HeaderText="Modèle" ReadOnly="True"/>
        <asp:BoundField DataField="AccessoryBrand" HeaderText="Marque" ReadOnly="True"/>
    </Columns>
</asp:GridView>

Any hint on how to access the Object contained inside a Row? I know I could actually get the ID and then do a SQL request to my DB, but it seems to be a bit heavy and there must be a better solution.

解决方案

Unless you're storing the DataSource in ViewState or session once the databinding is done you lose the data source. The DataItem will always be null unless you do this. Because of this I often find myself storing the data source in view state. Of course this means your page size is going to get bigger. The other option as you stated is to requery the data source with some sort of primary key. Depending on your needs I can't say which option is better. I tend to lean towards view state rather than a second DB call since that can be an expensive call to make

这篇关于从GridViewRow ASP.NET获取项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆