在向嵌套网格视图添加行时,如何在网格视图中获取父行的ID [英] How do I get the ID of a parent row in in a gridview when adding a row to the nestedgridview

查看:72
本文介绍了在向嵌套网格视图添加行时,如何在网格视图中获取父行的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,



当我向嵌套的gridview(gvBulletItems)添加一个新行并且有现有的行我可以用
获得父ID


Ok,

when I am adding a new row to the nested gridview(gvBulletItems) and there are existing rows I can get parent ID with

gvBulletItems.DataKeys[0].Value



(BTW我讨厌魔术数字)



但是当我有一个父网格视图时,嵌套网格视图中没有项目DataKeys返回为空



我因此定义了我的EmptyDataTemplate:


(BTW I hate magic numbers)

however when I a have parent gridview with no Items in the nested gridview DataKeys comes back as null

I have defined my EmptyDataTemplate thusly:

 <EmptyDataTemplate>
  There are no Bullets for this bullet group. 
  <table id="tblnewbullets"  class="tbl-newItems" runat="server">
      <tr class="tbl-newItems">
         <td class="tbl-newItems" > List Order</td>
         <td> Display Text</td>
      </tr>
      <tr>
          <td>
             <asp:TextBox ID="txtbxNewListOrder" runat="server"></asp:TextBox>
          </td>
          <td>
             <asp:TextBox ID="txtbxNewDisplayText" runat="server"></asp:TextBox>
          </td>
          <td>
             <asp:LinkButton ID="linkAddBullet" CommandName="AddNewBullet" runat="server">Add</asp:LinkButton>
          </td>
       </tr>
    </table>
</EmptyDataTemplate>





此前定义





and this defined before

<Columns>
.....
</Columns>





我想到的一件事是我可以从嵌套gridview的div的ID因为它的ID定义为



One of the things I thought of was that I could it from is the ID of the div that the nested gridview as its ID is defined as

"div<%# Eval("BulletGroupID") %>"



但是当我看起来不是身份证。



这里是绑定两个网格视图的代码




but when I look that is not the ID.

here is the code that binds both gridviews

if (row.RowType == DataControlRowType.DataRow)
     {
        InfoCenterParent icParent = (InfoCenterParent)Session[PARENT];
        List<InfoCenterBulletGroup> bulletGroupsData = icParent.bulletGroups;
        Label lblGroupBulletID = (Label)row.FindControl("lblBulletGroupID");
        int BulletGroupID = int.Parse(lblGroupBulletID.Text);
        InfoCenterBulletGroup bg = bulletGroupsData.Single(b => b.BulletGroupID == BulletGroupID);
        GridView gvBulletItems = new GridView();
        gvBulletItems = (GridView)row.FindControl("gvBulletItems");
        gvBulletItems.DataSource = bg.bulletItems;
        gvBulletItems.DataBind();
     }





以下是我在嵌套gridview的OnRowCommand中检索BulletgroupID的方法



and here is how I am retrieving the BulletgroupID in the OnRowCommand of the nested gridview

gvBulletItems.DataKeys[0].Value.ToString()



如果我添加了一个,那么这个对象(gvBulletItems.DataKeys)不为空bulletitem到有现有项目的gridview

但是如果我添加到没有项目的嵌套gridview,那么它将以null返回



以下是绑定到gridviews的对象


this object(gvBulletItems.DataKeys) is not null if I am added a bulletitem to a gridview that has existing items
however if I am adding to a the nested gridview that has no items then it comeback as null

Here is the object that gets bound to the gridviews

public class InfoCenterBulletGroup  
    {  
        public int BulletGroupID { get; set; }
        public int InfoCenterID { get; set; }
        public string DisplayText { get; set; }
        public int ListOrder { get; set; }
        public List<InfoCenterBulletItem> bulletItems { get; set; }
        public string LastUpdatedBy { get; set; }
        public DateTime LastUpdatedDate { get; set; }
    }
    public class InfoCenterBulletItem
    {
        public int BulletID { get; set; }
        public int BulletGroupID { get; set; }
        public int ListOrder { get; set; }
        public string DisplayText { get; set; }
        public string LastUpdatedBy { get; set; }
        public DateTime LastUpdatedDate { get; set; }
    }





我尝试过:



我试过分配



What I have tried:

I have tried assigning

<%# Eval("BulletGroupID") %>"



标记我的emptyDataTemplate文本



我挖掘了两个网格视图的所有属性

我的键盘已经磨损了谷歌搜索


to label text in my emptyDataTemplate

I have dug through all the properties of both gridviews
I have worn out my keyboard with Google searches

推荐答案

我不确定你的目标到底是什么。 GridView row doesn' t有一个 ID 但是,您可以通过强制对象的 sender 来引用该行会触发事件。例如,在您的添加按钮中,您可以这样:



I'm not exactly sure what exactly is your goal. A GridView row doesn't have an ID however, you can reference the row by casting the sender of the object that triggers the event. For example, in your Add Button, you can something like this:

protected void LinkButton1_Click(object sender, EventArgs e){
        LinkButton lb = (LinkButton)sender;
        GridViewRow row = (GridViewRow)lb.NamingContainer;

        //here the row variable is the row of your parent GridView

}


确定它有点像黑客,但我的解决方案是添加一个项目填充嵌套的db表当我在填充父网格视图的db表中创建记录时,网格gridview。



Ok it is a bit of a hack, but my solution was to add an item the db table that populates the nested grid gridview when I am creating a record in the db table that populates the parent grid view.

InfoCenterBulletGroup bg = new InfoCenterBulletGroup();
     int.TryParse(txtbxListOrder.Text, out tmp);
     bg.DisplayText = txtbxDisplayText.Text;
     bg.ListOrder = tmp;
     bg.LastUpdatedBy = _nbid;
     bg.InfoCenterID = int.Parse(hParentID.Value);
     InfoCenterData sql = new InfoCenterData(_connString);
     int bulletGroupID = sql.AddBulletGroup(bg);
     InfoCenterBulletItem bulletItem = new InfoCenterBulletItem();
     bulletItem.BulletGroupID = bulletGroupID;
     bulletItem.DisplayText = "New Bullet";
     bulletItem.ListOrder = 0;
     bulletItem.LastUpdatedBy = _nbid;
     sql.AddBulletItem(bulletItem);
     UpdateParentObject();
     InfoCenterParent icParent = (InfoCenterParent) Session[PARENT];
     List<InfoCenterBulletGroup> bulletGroupsData = icParent.bulletGroups;
     gvBulletGroup.DataSource = bulletGroupsData;
     gvBulletGroup.DataBind();
     txtbxListOrder.Text = string.Empty;
     txtbxDisplayText.Text = string.Empty;


这篇关于在向嵌套网格视图添加行时,如何在网格视图中获取父行的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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