首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >合并Templatefield与Boundfield

合并Templatefield与Boundfield
EN

Stack Overflow用户
提问于 2014-12-24 05:53:25
回答 1查看 2K关注 0票数 1

我有这个行字段,它包含与前一行相同的值。我可以使用boundField合并它,但是当我将它更改为templateField时,代码中的最后一行并不像我想的那样工作。因此,我使用绑定字段(将最后一行更改为边界字段)对其进行了比较,它起了作用。我之所以使用templatefield,是因为我想使用链接按钮commandArgument,这样它就可以使用会话将选定的数据传递到另一个页面。所以我的问题是,为什么合并与模板字段不能很好地结合呢?但是它在束缚场中工作得很好吗?我仍然想使用模板字段,因为链接按钮。但是,如果有人可以给我一个例子来使用绑定字段与链接,我是开放的使用它。

这是我的aspx代码。

代码语言:javascript
复制
<asp:GridView ID="GridViewApproval" AutoGenerateColumns="false" runat="server" CssClass="Grid" AllowPaging="true"       
    Width="100%" OnDataBound="OnDataBound" ShowHeaderWhenEmpty="true" EmptyDataText="No Data Found">

     <Columns>

        <asp:TemplateField HeaderText="No.">
            <ItemTemplate>
                <asp:Label ID="lblNo" runat="server"
                    Text = '<%# Container.DataItemIndex + 1 %>'> </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

         <asp:BoundField DataField="EMPLOYEE_ID" HeaderText="Emp ID"/>
         <asp:BoundField DataField="EMPLOYEE_NAME" HeaderText="Emp Name"/>
         <asp:BoundField DataField="POSITION_DESCRIPTION" HeaderText="Position"/>
         <asp:BoundField DataField="SECTION_DESCRIPTION" HeaderText="Section"/>
         <asp:BoundField DataField="PROPOSED_TOPIC" HeaderText="Topic"/>
         <asp:BoundField DataField="METHOD_DEV_NAME" HeaderText="Method of Development"/>
         <asp:BoundField DataField="ESTIMATE_COST" HeaderText="Cost Estimation(USD)" />
         <asp:BoundField DataField="ROD_DESC" HeaderText="Reason of Dev"/>
         <asp:BoundField DataField="SCHEDULE_PLAN" HeaderText="Schedule Plan"/>
         <asp:BoundField DataField="ILP_CODE" HeaderText = "CODE" />

         <asp:TemplateField HeaderText="CODE">
            <ItemTemplate>
            <asp:LinkButton ID="linkAction" runat="server"
                CommandArgument = '<%# Eval("ILP_CODE")%>'                   
                Text = '<%# Bind("ILP_CODE")%>' OnClick = "ActionApproval"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>

         </Columns>

    </asp:GridView>

这是我的aspx.cs代码

代码语言:javascript
复制
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack & Request.IsAuthenticated)
        {
            BindData();
        }
    }



    private void BindData()
    {
        String magicNumber = Server.HtmlEncode(Request.Cookies[Constant.CookiesEmployeeId].Value);
        decimal nextAppEmpId = decimal.Parse(magicNumber);
        GridViewApproval.DataSource = (nextAppEmpId.Equals("null") || nextAppEmpId == 0) ? apDetail.GetIlpProcessPosition() : apEntity.getIlpProcessPositionNextEmpId(nextAppEmpId);

        GridViewApproval.DataBind();
    }

    protected void ActionApproval(object sender, EventArgs e)
    {
        LinkButton linkAction = (LinkButton)sender;
        Session["ILP_CODE"] = linkAction.CommandArgument;
        Response.Redirect("~/Web/ILP/ApprovalListDetail.aspx");
    }

    protected void OnDataBound(object sender, EventArgs e)
    {
        for (int i = GridViewApproval.Rows.Count - 1; i > 0; i--)
        {
            GridViewRow row = GridViewApproval.Rows[i];
            GridViewRow previousRow = GridViewApproval.Rows[i - 1];
            for (int j = 0; j < row.Cells.Count; j++)
            {
                if (j != 0)
                {
                if (row.Cells[j].Text == previousRow.Cells[j].Text)
                {
                    if (previousRow.Cells[j].RowSpan == 0)
                    {
                        if (row.Cells[j].RowSpan == 0)
                        {
                            previousRow.Cells[j].RowSpan += 2;
                        }
                        else
                        {
                            previousRow.Cells[j].RowSpan = row.Cells[j].RowSpan + 1;
                        }
                        row.Cells[j].Visible = false;
                    }
                }
                }

            }
        }

        string ilpCode = null, ilpCodeSave = null;

        for (int count = 0; count < GridViewApproval.Rows.Count; count++)
        {
            ilpCode = GridViewApproval.Rows[count].Cells[10].Text;
            if (!ilpCode.Equals(ilpCodeSave))
            {
                //GridViewApproval.Rows[count].Cells[10].Text = "Details";
            }
            ilpCodeSave = GridViewApproval.Rows[count].Cells[10].Text;

        }
    }

这是我编译时的图像

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-02 08:21:57

以下是我能做的:享受

代码语言:javascript
复制
//looping for boundfield
    for (int i = GridViewApproval.Rows.Count - 1; i > 0; i--)
        {
            GridViewRow row = GridViewApproval.Rows[i];
            GridViewRow previousRow = GridViewApproval.Rows[i - 1];
            for (int j = 0; j < row.Cells.Count & j != 5; j++)
            {
                if (row.Cells[j].Text == previousRow.Cells[j].Text)
                {
                    if (previousRow.Cells[j].RowSpan == 0)
                    {
                        if (row.Cells[j].RowSpan == 0)
                        {
                            previousRow.Cells[j].RowSpan += 2;
                        }
                        else
                        {
                            previousRow.Cells[j].RowSpan = row.Cells[j].RowSpan + 1;
                        }
                        row.Cells[j].Visible = false;
                    }
                }
            }
        }

        //Looping for TemplateField
        for (int i = GridViewApproval.Rows.Count - 1; i > 0; i--)
        {
            GridViewRow row = GridViewApproval.Rows[i];
            GridViewRow previousRow = GridViewApproval.Rows[i - 1];
            for (int j = 0; j < row.Cells.Count - 1; j++)
            {
                //Define what index on your template field cell that contain same value
                if (((LinkButton)row.Cells[9].FindControl("linkAction")).Text == ((LinkButton)previousRow.Cells[9].FindControl("linkAction")).Text)
                {
                    if (previousRow.Cells[9].RowSpan == 0)
                    {
                        if (row.Cells[9].RowSpan == 0)
                        {
                            previousRow.Cells[9].RowSpan += 2;
                        }
                        else
                        {
                            previousRow.Cells[9].RowSpan = row.Cells[9].RowSpan + 1;
                        }
                        row.Cells[9].Visible = false;
                    }
                }
            }
        }

        //This is to redefine again.
        for (int count = 0; count < GridViewApproval.Rows.Count; count++)
        {
            LinkButton lblprojectTest = GridViewApproval.Rows[count].Cells[9].FindControl("linkAction") as LinkButton;
            lblprojectTest.Text = "Details";
        }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27632163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档