Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

On postback TemplateField disappears in a GridView

Status
Not open for further replies.
Apr 11, 2002
193
IN
Hi,

I am using ASP.net 2.0 and am creating TemplateFields and BoundColumns for a GridView at runtime. I have created a class for creating buttons which implements ITemplate interface. Before binding the grid i have written.
TemplateField t = new TemplateField();
t.ItemTemplate = new CreateItemTemplatePushButton("btnSelectAll", "Select All", "Select All");
GridView1.Columns.Add(t);
Below is the class which implements ITemplate.
public class CreateItemTemplatePushButton : ITemplate
{
string strColumnText;
string strButtonName;
string strCommandName;
bool Visibile = true;
bool Enable = true;
public CreateItemTemplatePushButton(string ButtonName, string ColText, string CommandName)
{
this.strColumnText = ColText;
this.strButtonName = ButtonName;
this.strCommandName = CommandName;
}
public void InstantiateIn(Control objContainer)
{
Button btn = new Button();
btn.CommandName = strCommandName;
btn.ID = strButtonName;
btn.Text = strColumnText;
btn.EnableViewState = true;
objContainer.Controls.Add(btn);
}
}
The grid comes up very well, but when ever there is a postback the TemplateField disappears and the BoundColumns remain. I have tried EnableViewState=true; but it doesnt make a difference. Has anyone come across this problem.


Awaiting your advice.

Thanks,
Manish
 
Hi,

I have got a CommandField on the Grid as well. This button at the moment is doing nothing, just is posting back. When i click on it the TemplateField disappears.
On Page_Load i have
if (!IsPostBack)
BindGridView();

BindGridView() has the code to get data then create CommandFields, TemplateField and BoundColumns.
I have written something on the RowCreated event. I mean just adding some attributes to e.Row.
Where do i need to recreate the TemplateField. Do i need to rebind the Grid using BindGridView()?

Thanks,
Manish
 
On Page_Load i have
if (!IsPostBack)
BindGridView();

BindGridView() has the code to get data then create CommandFields, TemplateField and BoundColumns.
What that means is that when you click the button, BindGridView() will not be called as Page.IsPostBack will equal True. So, if you aren't calling the event, the Templatefield's will not be created on each page load and therefore will only exist the first time that you navigate to the page. You will need to call the function to create the Templatefield on every page load.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
On another note, I notice that although you have asked around 75 questions on the forums here, you have only marked one of the answers as valuable, and I can only see one or two acknowledgements of other peoples responses. If you are not getting the answers that you need read FAQ222-2244 to see how to ask better questions. If you are getting the answers you need see FAQ222-2244 to see how to acknowledge the answers given.

Paragraph 16 of the FAQ explains about this being a two-way forum, to give guidance on answering other peoples questions, as I also notice that you haven't yet made any posts in other peoples threads.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Hi,

Yes you are correct. I did store the dataset in a cache object and i did call the BindGridView() on RowCommand event and it did solve my problem. I hope it does work with AJAX updatepanel which i am just about to implement.

Thanks for your help.

Manish
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top