Changing column header text of a gridview inside user control

Hi,

I have an ascx file with a gridview, and I need to change the header text of the gridview's column in the code-behind. Something like that:

foreach (DataControlField col in GridView1.Columns)
{
col.HeaderText = GetString(col.HeaderText);
}

However, no matter where I place that code, the header texts return to their original value, and that's what's being displayed.

What could be the reason for that? And how can it be solved?

 

0
Amit00
8/13/2008 10:06:52 AM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

6 Replies
1185 Views

Similar Articles

[PageSpeed] 43

...Anyone?

I'm having the feeling that I'm missing something quite basic here, any help would be very appreciated.

0
Amit00
8/14/2008 6:25:59 AM

Amit00:
col.HeaderText = GetString(col.HeaderText);

instead of that try

co.HeaderText = "test";

check what's the o/p? if all the column names are changed to "test", the mistake is in that function. it is returning same value.

if still it generating the same old column names, first make sure that code is executing and the function is correctly returning values.

if not it may due to the loss of control state. if that is the case post your code.

 

 


Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
0
ramireddyindia
8/14/2008 7:11:11 AM

Thanks for the reply.

I already tried to place a simple string the the HeaderText property, it didn't help. In addition, when I'm debugging I see the col.HeaderText changing its value, but it's not seen on the screen.

Here is the relevant code:

WebControlUser.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White"

BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black"

GridLines="Vertical">

<FooterStyle BackColor="#CCCCCC" />

<Columns>

<asp:BoundField DataField="value1" HeaderText="HEADER1" />

<asp:BoundField DataField="value2" HeaderText="HEADER2" />

</Columns>

<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />

<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />

<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />

<AlternatingRowStyle BackColor="#CCCCCC" />

</asp:GridView>

 

WebControlUser.ascx.cs:

public partial class WebUserControl : System.Web.UI.UserControl

{

protected void Page_Load(object sender, EventArgs e)

{

DataSet ds = new DataSet();

DataTable table = ds.Tables.Add();

table.Columns.Add("value1");

table.Columns.Add("value2");

DataRow row = table.NewRow();

row["value1"] = "hey";

row["value2"] = "hello";

table.Rows.Add(row);

GridView1.DataSource = ds;

GridView1.DataBind();

// I also tried to place this part in "DataBound" or "OnPreRender", just in case, but didn't make a difference

foreach (DataControlField col in GridView1.Columns)

{

col.HeaderText =
"test";

}

}

}

 

Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register TagPrefix="uc" TagName="Res" Src="WebUserControl.ascx" %>

<!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 runat="server">

<title>Untitled Page</title>

<link rel="Stylesheet" href="styles.css" type="text/css" />

</head>

<body>

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

<div>

<uc:Res id="uc1" runat="server" />

</div>

</form>

</body>

</html>

 

And my output is a gridview with one row and two columns, with, unfortunately the header texts "HEADER1" and "HEADER2".

0
Amit00
8/14/2008 7:36:04 AM
Amit00:
foreach (DataControlField col in GridView1.Columns)

{

col.HeaderText =
"test";

}

 

change the above two lines to like this.

foreach (DataControlFieldCell cell in GridView1.HeaderRow.Cells)

      cell.Text = "test";


Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
0
ramireddyindia
8/14/2008 8:00:05 AM

 Great, that did the trick. Thanks a lot.

Any idea why the first version doesn't work?

0
Amit00
8/14/2008 8:17:25 AM

your way is also correct. but you have to keep those 2 lines before the gridview binding. this will work.

 

foreach (DataControlField col in GridView1.Columns)

{ col.HeaderText = "test"; }

DataSet ds = new DataSet();

DataTable table = ds.Tables.Add();

table.Columns.Add("value1");

table.Columns.Add("value2");

DataRow row = table.NewRow();

row["value1"] = "hey";

row["value2"] = "hello";

table.Rows.Add(row);

GridView1.DataSource = ds;

GridView1.DataBind();

 

When my post helped you to solve your problem, mark my answer as answer, and close this thread. i am telling this because you are a beginner in this forum.


Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
0
ramireddyindia
8/14/2008 8:31:25 AM
Reply:

Similar Artilces:

change visibility of a control inside user control
how can i change the visibility of a control inside an user control by the code behind of my content page? Try first retrieving a reference to your control by using the FindControl method of your UserControl. Once you have the reference, you can change the visibility. Another way is to add a property to your UserControl specifically for the embedded control.Thanks, EdMicrosoft MVP - ASP/ASP.NET using findControl didnt work, but i believe that if i add a property it will be working. thanks...

Change password control inside a user control
I make a aspx page with a  the change password control inside the page.  Everything work fine.  I next take the change password control to inside a user control.  On my aspx page, I include the user control.On the first load of the page, the change password control is display.  When I click on the change password button, the job seems to be done, but on the postback, the control is not reload.Next, I try to load the user control dynamicly in the page_load event.  The result fails also to display thechange password control.  The change password control is lo...

User Control inside User Control
I have an ASPX with user controls in it called UC1.There is another user control called UC2 within user control UC1.(UC1 - parent , UC2 - child) What I need to do is to pass value of the text box that’s located inside user control UC1 to UC2. This is the code I put into  user control UC1. How to I read it in User control UC2?   Public ReadOnly Property propCountryName() As TextBox Get Return Me.txtCountrygName End Get End Property    Thank you100 years live, 100 years learn You can define a property inside the UC1 and UC2. Pag...

user control inside a user control
i've currently got one of my pages setup so that I have a user control, CreateMessage.ascx, inside my main user control, ViewChangeMessages.ascx. My problem is that i've got a method inside my main usercontrol that either hides or shows the second user control, but it doesn't work, mainly coz i found out that the second user control is loaded before my main one. Is there any way i can get around this? You could try to load the second user control programmatically the OnPreRender method of your main user control. You'll need to add a placeholder in the spot you want the user control to...

User Control inside User Control
I have an ASPX with user controls in it called UC1.There is another user control called UC2 within user control UC1.(UC1 - parent , UC2 - child) What I need to do is to pass value of the text box that’s located inside user control UC1 to UC2. This is the code I put into  user control UC1. How to I read it in User control UC2? Public ReadOnly Property propCountryName() As TextBox Get Return Me.txtCountrygName End Get End Property    Thank you100 years live, 100 years learn Hello, In UC2, try the followin code Dim tb As TextBox=CT...

How to programmatically add user control to the middle of text inside Literal text control ? Pls help...
Greetings fellow programmers,I greatly appreciate all the help I can get. Here is the problem.I have a function that pulls content from the database and put the content into a literal control. Somewhere in that content, I need to insert a user control (bla.ascx). Ideally, I want to use string.replace to find the location where I want to insert the user control. But I can't... there is no such option on the literal control methods and properties.Do I have to overwrite render method? I'd normally use loadcontrol and add it to the placeholder control, but in this case, I can't dynamically ...

User Control Inside a Modal Pop Up
Hello, I am a beginner with AJAX and need some help. I have a user user control that I want to place in a Modal Pop up, but I am running into some issues.  The user control is used for doing some seaching of database records. After the use selects a record, they choose from one of three radio buttons and then hit a submit button. This submit button has logic tied to it that needs to be performed before closing the modal pop up. I want to make this button (btnDoLogic) that is inside the User Control, the button that is tied to the...

Getting the control id of a control inside gridview control
 uhmm.. a little bit confusing regarding my subject because i dont know how to say it...by the way my problem is how can i get the id of a control (e.g. textbox) inside the gridview... and the textbox is in the templatefield... is there any way i can get its id??  hoping i could get a reply as soon as possible... Regards,MhaeyPlease remember to click “Mark as Answer” on the post that helps you.. =) What do you want's to do by getting the id of the Text box? Do you want's to find particular Textbox with certain id ? Kamran ShahidSr. Software Engineer(MCP,MCAD.net,MC...

Embedded user control inside a user control
Hi, I have a user control that displays some details in a repeater, with a view buttons etc. I have now developed a second user control that is intended to provide a visual frame around something. I wanted to the HTML to look something like: <frameControl id="myFrame"> <myUserControl id="testControl" /> </frameControl> but when the page runs I get an error saying FrameControl does not have a property called myUserControl. It's right it doesn't! Inside the frameControl I just want to know who the children are. At present I have ha...

Help Req: !!! :- On Disabling Controls Present inside a Gridview control
Hi All, We have A requirement Where we have disable controls inside a grid.These controls can be textbox, DropDownlist (in TemplateField), ComandField present inside the grid.We have to call a generic method for doing the same .(since it can be done for multiple grid.) PFB the code snippet we use. Which disables all the controls inside the page.The bolded part of the code is used to loop inside a grid view.(Not Working.)Is this ideal way to deal with the requirement.Or can someone suggest be a better way.        public void DisableMe(Page p1, st...

FileUpload control inside UpdatePanel inside user control inside popup
Hi,     The Asp.Net File Upload control is not working inside update panel. Acutally i have put the FileUpload control inside User Control again inside update panel. Since the update panel does async postback. The posted file is null in the server side. When i do full page level post back it works. but i have to put it inside update panel. How to do it? Becuase the FileUpload control should be in Ajax Model Popup Extender where i have to put update panel to avoid closing of popup after fullpostback. Since the ajax model popup extender is getting closed after full post b...

User Controls: How to change the Property of a User Control in CodeBehind???
Hi I am using the book "ASP.NET Unleasd" to learn ASP.NET. In Chapter 5 it talks about User Controls.  I am currently working on "Listing 5.7": Exposing properties and Methods in user controls.Well, I am trying to implement this code in visual studio using codebehind.In the user control (HeaderTitle.ascx) I have the following:<html><head><title><%=PageTitle%></title></head><body><h2>Global Super Company</h2><i>We mean business</i><hr>In the file HeaderTitle.ascx.cs (code behind for the user control) I have the ...

How to inform other user controls if one user control change?
I use one user control to receive the click then another user control need to update its content corresponding with the click. I use event to solve the problem. It works fine.But when I try to test another solution to solve that problem, I get headache. I try to set a session and store information to this session when user click the link. Then I want to raise a postback event so other users can verify the session varailble. How can I raise a postback event in a user control (not custom control)?Thanks!...

Accessing properties of user control inside user control
I have a simple MemberPhoto user control which has a photo of a member along with username and if they are online.I would like to reuse this control inside of another user control (MemberSummary) that would include the member's photo and some basic information about the member.  I would like to know what is the "Best Practice" to set/read properties on the member photo control from a web form.1. Should the MemberSummary define a property that returns the MemberPhoto user control and there I would have access to th...

Web resources about - Changing column header text of a gridview inside user control - asp.net.presentation-controls

Changing room - Wikipedia, the free encyclopedia
A changing room , locker room , dressing room (usually in a sports, theater or staff context) or changeroom (regional use) is a room or area ...

Beyond Timeline: The Changing Face Of Facebook Ads
Despite the hype, timeline may not be the most exciting, or important change coming on March 30. Expanded advertising opportunities may unlock ...

Robert Scoble - Micromedia, how it's changing what we blog - Facebook
Jeremiah Owyang wrote an interesting piece about micromedia and how it's changing what we do on blogs and how we distribute media (er, ideas, ...

8 Ways the Changing Facebook Home Page Will Affect Application Virality
For developers who live and die by the changes Facebook makes to the way information flows through the service, every little Facebook tweak has ...

Changing Lanes (@chginglanes) on Twitter
Sign in Sign up To bring you Twitter, we and our partners use cookies on our and other websites. Cookies help personalize Twitter content, tailor ...

Changing the way business decisions are made
We believe that social data has unlimited value, and near limitless application. Today, we’re taking an important step toward unleashing this ...

The Right and Wrong Reasons for Changing Jobs
... your profile is a clue to the folks at LinkedIn that you’re thinking of switching jobs, so don’t be The Right and Wrong Reasons for Changing ...

We're Changing Our Name (Back) to Stack Overflow
We are Stack Overflow. You may know us from such popular websites as Stack Overflow Q&A, Stack Overflow Careers, The Stack Exchange Q&A Network, ...

The Tao of Twitter: Changing your life and business 140 characters at a time - Amazon
It's time to take the mystery out of Twitter. Twitter may be the most powerful business and personal networking platform ever created but it ...

App Store - Voices 2 ~ fun voice changing!
Read reviews, get customer ratings, see screenshots, and learn more about Voices 2 ~ fun voice changing! on the App Store. Download Voices 2 ...

Resources last updated: 12/13/2015 4:58:53 AM