English 中文(简体)
ASP. NET:何时和如何动态地改变《概览》中背后的头版?
原标题:ASP.NET: When and how to dynamically change Gridview s headerText s in code behind?

I have a gridview with 2 columns. I want to learn coding behind and do NOT want to do this in the aspx file. How do I set the header text for my columns dynamically? At what point do I do so? After the adapter has filled the gridview with data? Right now, I have the header text but it is exactly the same as the datafield name which is last_name and I want to see Last Name in the header field instead. I ve tried

GridView1.Columns[0].HeaderText = "Last Name";

but wherever I tried to put it, the compiler complains about index out of range.

感谢。

aspx code for the gridview:

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
                BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px"
                Width="728px" CellPadding="4" ForeColor="Black" GridLines="Vertical" OnPageIndexChanging="GridView1_PageIndexChanging"
                OnSorting="GridView1_Sorting" PageSize="14" OnRowDataBound="GridView1_RowDataBound">
                <AlternatingRowStyle BackColor="White" />
                <FooterStyle BackColor="#CCCC99" />
                <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
                <RowStyle BackColor="#F7F7DE" />
                <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#FBFBF2" />
                <SortedAscendingHeaderStyle BackColor="#848384" />
                <SortedDescendingCellStyle BackColor="#EAEAD3" />
                <SortedDescendingHeaderStyle BackColor="#575357" />
                <PagerSettings Mode="NumericFirstLast" FirstPageText="First" LastPageText="Last"
                    PageButtonCount="5" Position="Bottom" />
            </asp:GridView>
最佳回答

Try putting it in the GridView1.RowDataBound handler. Evaluate e.Row.RowType to determine if it is a header row, then then replace the HeaderText.

protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header) {
        GridView1.Columns[0].HeaderText = "Last Name";

    }
}

但是,如果你积极设立栏目,并采用分类方法,那么你就需要采取这一办法,防止将连接同形连接变成平原:

protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header) {
        LinkButton HLink = (LinkButton)e.Row.Cells[0].Controls[0];
        HLink.Text = "Last Name";
    }
}

With either, add this attribute to your Gridview in the ASPX:

OnRowDataBound="GridView1_RowDataBound"
问题回答

加入:

GridView1.Columns[0].HeaderText = "Last Name"; 

工作滞后,因为它抱怨一栏数字为0,因此:

protected void grdProd_Load(object sender, EventArgs e)
{
    grdProd.HeaderRow.Cells[0].Text = "Item";
    grdProd.HeaderRow.Cells[1].Text = "Category";
}

我认为,在每一行数据约束活动(每行1次)中,你想把案文约束在你的格德!

仅看上去该页的Loaded事件,然后就把你喜欢的那段话装上。

 protected void Page_Load(object sender, EventArgs e)
 {
    GridView1.Columns[0].HeaderText = "Last Name";
 }
     <%@ Page Language="C#" AutoEventWireup="true" CodeFile="grdvw8.aspx.cs" Inherits="grdvw8" %>



    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



    <html xmlns="http://www.w3.org/1999/xhtml">

    <head id="Head1" runat="server">

    <title></title>

    </head>

    <body>

    <form id="form1" runat="server">

    <div>

    first header name change To<asp:TextBox ID="txt1" runat="server"></asp:TextBox>

    <br />

    Second header name change To<asp:TextBox ID="txt2" runat="server"></asp:TextBox>

    <br />

    <asp:Button ID="btnChange" Text="Change Header Text" runat="server" onclick="btnChange_Click" />

    <asp:GridView ID="grdvw" runat="server">

    <HeaderStyle Font-Bold="true" ForeColor="Brown" />

    </asp:GridView>

    </div>

    </form>

    </body>

    </html>


/ASPX.CS PAGE/

 using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Data.SqlClient;

using System.Configuration;



public partial class grdvw8 : System.Web.UI.Page

{

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["code"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)

    {

        Bind();

    }



    protected void Bind()

   {

         con.Open();

          SqlCommand cmd=new SqlCommand("select * from gridview",con);

          SqlDataAdapter da=new SqlDataAdapter(cmd);

          DataSet ds=new DataSet();

          da.Fill(ds);

        grdvw.DataSource = ds;

         grdvw.DataBind();



   }



    protected void btnChange_Click(object sender, EventArgs e)

    {

        if (grdvw.Rows.Count > 0)

        {

            grdvw.HeaderRow.Cells[0].Text = txt1.Text;

            grdvw.HeaderRow.Cells[1].Text = txt2.Text;

        }

    }



}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签