|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
assigning itemtemplate, edititemtemplate programmaticallyusing; however it should apply to Datagrid as well. I would like to be able to assign the itemtemplate and edititemtemplate programmatically based on how the user would like the data layed out. I have been able to achieve this by using creating a class that implements the ITemplate interface, however I soon realized the drawback in that custom classes do not save their viewstate by default. Therefore I have to retrieve the data and assign the templates at every postback, which consumes resources and doesn't take advantage of the Viewstate properties of the datalist/datagrid. I tried adding my custom template class to the viewstate, but the error was that the object had to be serializable. I looked at this briefly but gave up due to time constraints. Having said all this, *ideally* what I would like is to be able to do is just have the html templates in separate files and then instantiate a class based on the user's preferred layout and assign it to the datalist. I think this is possible somehow but I don't have the time to research it right now. If anyone has done something like this before or would like to try, that would be great!! Thanks, Steve Okay, I found out that you can also use Page.LoadTemplate("<filename>.ascx")
to assign the templates programmatically: DataList1.ItemTemplate = Page.LoadTemplate("itemtemplate1.ascx"); This allows you to store the templates in different files, and assign them dynamically based on user input. However, there are still a few problems. First, you can't seem to be able to set DataList1.ExtractTemplateRows = true when doing this, because this causes the DataList to require that there be an asp table and doesn't recognize that my itemtemplate1.ascx actually contains a table. Second, and more importantly, postback is a major problem I've had with dynamically loading templates. This goes for doing it using a custom class that implements ITemplate and by using Page.LoadTemplate. It works fine if there are no command buttons. But if you do use, for instance, an Edit button (commandname=edit), then it will not work. The problem is that on the containing page (e.g. mydatalist.aspx), you want the page_load method to have the !IsPostBack requirement: if (!IsPostBack) { DataSub(); } private void DataSub() { DataList1.DataSource = getDataSource(); DataList1.ItemTemplate = Page.LoadTemplate("itemtemplate1.ascx"); DataList1.DataBind(); } Unfortunately, if you have the !IsPostBack requirement, the page will not load DataSub() again initially when you click the edit button, but also the DataList1_EditCommand(...) method will never fire either!! The only way to get it to fire is to remove the !IsPostBack requirement; then, for some reason, the initial DataSub() will occur in page_load, and then the editcommand event will fire afterwards, followed by anothere DataSub(), which is totally inefficient: DataList1_EditCommand(object sender, System.EventArgs e) { DataList1.EditItemIndex = e.Item.ItemIndex; DataSub(); } So, if you remove the DataSub() from the editcommand method, then DataSub only occurs once, but only BEFORE the edititemindex has been assigned. ARRRGHH!!!! Does anyone have a proven method to dynamically assigning templates for the datagrid/datalist??? If so please let me know! :-) Thanks Show quoteHide quote "Steve P" wrote: > This really is a question for Datalist, since that is what I am primarily > using; however it should apply to Datagrid as well. > > I would like to be able to assign the itemtemplate and edititemtemplate > programmatically based on how the user would like the data layed out. I have > been able to achieve this by using creating a class that implements the > ITemplate interface, however I soon realized the drawback in that custom > classes do not save their viewstate by default. Therefore I have to retrieve > the data and assign the templates at every postback, which consumes resources > and doesn't take advantage of the Viewstate properties of the > datalist/datagrid. I tried adding my custom template class to the viewstate, > but the error was that the object had to be serializable. I looked at this > briefly but gave up due to time constraints. > > Having said all this, *ideally* what I would like is to be able to do is > just have the html templates in separate files and then instantiate a class > based on the user's preferred layout and assign it to the datalist. I think > this is possible somehow but I don't have the time to research it right now. > If anyone has done something like this before or would like to try, that > would be great!! > > Thanks, > Steve Okay, found the solution to the problem about the edit button not firing.
You have to assign the datalist templates in the page_init method as in the microsoft example at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbgrfcreatingtemplatecolumnsdynamicallyindatalistwebservercontrol.asp. However, now I have another problem in that the editcommand actually fires twice instead of just once. If you copy the example code at the link above this is what you will find, unless there is something I'm doing wrong I will make a new post. Show quoteHide quote "Steve P" wrote: > Okay, I found out that you can also use Page.LoadTemplate("<filename>.ascx") > to assign the templates programmatically: > > DataList1.ItemTemplate = Page.LoadTemplate("itemtemplate1.ascx"); > > This allows you to store the templates in different files, and assign them > dynamically based on user input. However, there are still a few problems. > First, you can't seem to be able to set DataList1.ExtractTemplateRows = true > when doing this, because this causes the DataList to require that there be an > asp table and doesn't recognize that my itemtemplate1.ascx actually contains > a table. Second, and more importantly, postback is a major problem I've had > with dynamically loading templates. This goes for doing it using a custom > class that implements ITemplate and by using Page.LoadTemplate. It works > fine if there are no command buttons. But if you do use, for instance, an > Edit button (commandname=edit), then it will not work. The problem is that > on the containing page (e.g. mydatalist.aspx), you want the page_load method > to have the !IsPostBack requirement: > > if (!IsPostBack) { > DataSub(); > } > > private void DataSub() { > DataList1.DataSource = getDataSource(); > DataList1.ItemTemplate = Page.LoadTemplate("itemtemplate1.ascx"); > DataList1.DataBind(); > } > > Unfortunately, if you have the !IsPostBack requirement, the page will not > load DataSub() again initially when you click the edit button, but also the > DataList1_EditCommand(...) method will never fire either!! The only way to > get it to fire is to remove the !IsPostBack requirement; then, for some > reason, the initial DataSub() will occur in page_load, and then the > editcommand event will fire afterwards, followed by anothere DataSub(), which > is totally inefficient: > > DataList1_EditCommand(object sender, System.EventArgs e) { > DataList1.EditItemIndex = e.Item.ItemIndex; > DataSub(); > } > > So, if you remove the DataSub() from the editcommand method, then DataSub > only occurs once, but only BEFORE the edititemindex has been assigned. > > ARRRGHH!!!! > > Does anyone have a proven method to dynamically assigning templates for the > datagrid/datalist??? If so please let me know! :-) > > Thanks > > "Steve P" wrote: > > > This really is a question for Datalist, since that is what I am primarily > > using; however it should apply to Datagrid as well. > > > > I would like to be able to assign the itemtemplate and edititemtemplate > > programmatically based on how the user would like the data layed out. I have > > been able to achieve this by using creating a class that implements the > > ITemplate interface, however I soon realized the drawback in that custom > > classes do not save their viewstate by default. Therefore I have to retrieve > > the data and assign the templates at every postback, which consumes resources > > and doesn't take advantage of the Viewstate properties of the > > datalist/datagrid. I tried adding my custom template class to the viewstate, > > but the error was that the object had to be serializable. I looked at this > > briefly but gave up due to time constraints. > > > > Having said all this, *ideally* what I would like is to be able to do is > > just have the html templates in separate files and then instantiate a class > > based on the user's preferred layout and assign it to the datalist. I think > > this is possible somehow but I don't have the time to research it right now. > > If anyone has done something like this before or would like to try, that > > would be great!! > > > > Thanks, > > Steve Found the solution. It was related to how Visual studio generates code
automatically to handle events. After I deleted this code ("#region Web Form Designer generated code") the button fired only once. Show quoteHide quote "Steve P" wrote: > Okay, found the solution to the problem about the edit button not firing. > You have to assign the datalist templates in the page_init method as in the > microsoft example at > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbgrfcreatingtemplatecolumnsdynamicallyindatalistwebservercontrol.asp. > However, now I have another problem in that the editcommand actually fires > twice instead of just once. If you copy the example code at the link above > this is what you will find, unless there is something I'm doing wrong I will > make a new post. > > "Steve P" wrote: > > > Okay, I found out that you can also use Page.LoadTemplate("<filename>.ascx") > > to assign the templates programmatically: > > > > DataList1.ItemTemplate = Page.LoadTemplate("itemtemplate1.ascx"); > > > > This allows you to store the templates in different files, and assign them > > dynamically based on user input. However, there are still a few problems. > > First, you can't seem to be able to set DataList1.ExtractTemplateRows = true > > when doing this, because this causes the DataList to require that there be an > > asp table and doesn't recognize that my itemtemplate1.ascx actually contains > > a table. Second, and more importantly, postback is a major problem I've had > > with dynamically loading templates. This goes for doing it using a custom > > class that implements ITemplate and by using Page.LoadTemplate. It works > > fine if there are no command buttons. But if you do use, for instance, an > > Edit button (commandname=edit), then it will not work. The problem is that > > on the containing page (e.g. mydatalist.aspx), you want the page_load method > > to have the !IsPostBack requirement: > > > > if (!IsPostBack) { > > DataSub(); > > } > > > > private void DataSub() { > > DataList1.DataSource = getDataSource(); > > DataList1.ItemTemplate = Page.LoadTemplate("itemtemplate1.ascx"); > > DataList1.DataBind(); > > } > > > > Unfortunately, if you have the !IsPostBack requirement, the page will not > > load DataSub() again initially when you click the edit button, but also the > > DataList1_EditCommand(...) method will never fire either!! The only way to > > get it to fire is to remove the !IsPostBack requirement; then, for some > > reason, the initial DataSub() will occur in page_load, and then the > > editcommand event will fire afterwards, followed by anothere DataSub(), which > > is totally inefficient: > > > > DataList1_EditCommand(object sender, System.EventArgs e) { > > DataList1.EditItemIndex = e.Item.ItemIndex; > > DataSub(); > > } > > > > So, if you remove the DataSub() from the editcommand method, then DataSub > > only occurs once, but only BEFORE the edititemindex has been assigned. > > > > ARRRGHH!!!! > > > > Does anyone have a proven method to dynamically assigning templates for the > > datagrid/datalist??? If so please let me know! :-) > > > > Thanks > > > > "Steve P" wrote: > > > > > This really is a question for Datalist, since that is what I am primarily > > > using; however it should apply to Datagrid as well. > > > > > > I would like to be able to assign the itemtemplate and edititemtemplate > > > programmatically based on how the user would like the data layed out. I have > > > been able to achieve this by using creating a class that implements the > > > ITemplate interface, however I soon realized the drawback in that custom > > > classes do not save their viewstate by default. Therefore I have to retrieve > > > the data and assign the templates at every postback, which consumes resources > > > and doesn't take advantage of the Viewstate properties of the > > > datalist/datagrid. I tried adding my custom template class to the viewstate, > > > but the error was that the object had to be serializable. I looked at this > > > briefly but gave up due to time constraints. > > > > > > Having said all this, *ideally* what I would like is to be able to do is > > > just have the html templates in separate files and then instantiate a class > > > based on the user's preferred layout and assign it to the datalist. I think > > > this is possible somehow but I don't have the time to research it right now. > > > If anyone has done something like this before or would like to try, that > > > would be great!! > > > > > > Thanks, > > > Steve
How can I display hashtable key values in a web page?
Move bound column to right of dynamic column in datagrid? Print WebForm DataGrid To Eliyahu problem exporting datagrid to excel can i use dynamic variable inside a dataadapter and bind to datagrid How to pull the data out from two table and bind to repeater User Control ASP .Net Message box help Needed Very urgent SortCommand and PageIndexChange event not firing |
|||||||||||||||||||||||