| Home | Forums | Reviews | Guides | Newsgroups | Register | Search |
![]() |
| Thread Tools |
| CAK |
|
|
|
| |
|
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
|
I'm wondering if Denis Bauer's DynamicControlsPlaceholder control would help
you here? "Problem: ASP.NET gives a developer the opportunity to programmatically add controls to a web form using ParentControl.Controls.Add(new Control()); However, these controls are not persisted in any way thus having to be recreated for each subsequent request. Goal: To create a control that behaves like a placeholder but additionally handles recreating dynamic controls on subsequent requests." http://www.denisbauer.com/ASPNETCont...aceholder.aspx "CAK" <ck-> wrote in message news: om... > Hi, > > I'm having problems retaining my dynamic columns during postbacks. > I've read that if you create dynamic columns at runtime, you have to > add them during the page_init event. I've done that, but I'm not sure > what else I'm supposed to do to make this work properly. Basically, > I've created a search page with a bunch of criteria - when the user > clicks search, it searches for all the criteria that has been filled > in and eventually stores whether a criterion is active or not in a > view state variable, then it should go through them and add a column > if the criterion is active. I've been playing around with different > things, but nothing makes my dynamic columns stay when I try to sort > or, more importantly, when I try to pull data from the dynamic > columns. I've added the function to the page_init event that adds all > the dynamic columns, but am I supposed to rebind the grid afterwards? > I may also be mismanaging my view state variables. > > Thanks for any ideas or suggestions anyone can give me. > > Code Follows - I tried to edit it down a bit: > > <asp:datagrid id="dgdSearch" runat="server" BackColor="White" > Width="374px" Font-Names="verdana" > BorderColor="#3366CC" BorderStyle="None" Font-Size="X-Small" > AutoGenerateColumns="False" AllowPaging="True" PageSize="15" > AllowSorting="True" > CellPadding="4" BorderWidth="1px"> > <SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" > BackColor="#009999"></SelectedItemStyle> > <AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle> > <ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle> > <HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" > BackColor="#003399"></HeaderStyle> > <FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle> > <Columns> > <asp:HyperLinkColumn Text="View" Target="_blank" > DataNavigateUrlField="PropertyID" > DataNavigateUrlFormatString="PropertyMain.aspx?Pro pertyID={0}" > DataTextField="PropertyID" SortExpression="PropertyID" > HeaderText="Property ID"></asp:HyperLinkColumn> > <asp:TemplateColumn HeaderText="Mark"> > <ItemTemplate> > <asp:CheckBox id="chkMark" runat="server"></asp:CheckBox> > </ItemTemplate> > </asp:TemplateColumn> > <asp:ButtonColumn Visible="False" Text="Delete" > CommandName="Delete"></asp:ButtonColumn> > </Columns> > <PagerStyle HorizontalAlign="Left" ForeColor="#003399" > BackColor="#99CCCC" Mode="NumericPages"></PagerStyle> > </asp:datagrid> > > > > > Private Sub Page_Init(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles MyBase.Init > 'CODEGEN: This method call is required by the Web Form > Designer > 'Do not modify it using the code editor. > InitializeComponent() > > Call AddDynamicColumns() > > End Sub > > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles MyBase.Load > 'Put user code to initialize the page here > txtUser.Text = Web.HttpContext.Current.User.Identity.Name > > If Not Page.IsPostBack Then > FillDropDowns() > Me.btnAddNew.Attributes.Add("onClick", > "window.open('PropertyMain.aspx');") > > ' Create viewstates for the boolean variables > viewstate.Add("blnCounty", False) > viewstate.Add("blnPropertyType", False) > '** additional criteria items removed for brevity *** > > End If > > End Sub > > > Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e > As System.EventArgs) Handles btnSearch.Click > > FillSearchDatagrid() > > End Sub > > Private Sub FillSearchDatagrid() > > Dim lstItem As ListItem > Dim strSql As String > Dim intCount As Integer > > Dim blnCounty As Boolean > Dim blnPropertyType as Boolean > '** additional criteria items removed for brevity *** > > ' Reset all the variables > strSQL = " WHERE 0 = 0" > > > ' County > intCount = 0 > For Each lstItem In lstCounty.Items > If lstItem.Selected Then > If Convert.ToInt32(lstItem.Value) > 0 Then > If intCount > 0 Then > strSql += " OR County = " & lstItem.Value > Else > strSql += " AND (County = " & lstItem.Value > End If > intCount = intCount + 1 > blnCounty = True > End If > End If > Next > If intCount > 0 Then > strSql += ")" > End If > ' Property Type > intCount = 0 > For Each lstItem In lstPropertyType.Items > If lstItem.Selected Then > If Convert.ToInt32(lstItem.Value) > 0 Then > If intCount > 0 Then > strSql += " OR PropertyType = " & > lstItem.Value > Else > strSql += " AND (PropertyType = " & > lstItem.Value > End If > intCount = intCount + 1 > blnPropertyType = True > End If > End If > Next > If intCount > 0 Then > strSql += ")" > End If > > '*** additional criteria items removed for brevity *** > > If viewstate("SearchString") Is Nothing Then > viewstate.Add("SearchString", strSql) > Else > viewstate("SearchString") = strSql > End If > > Dim dsWeb As DataSet = > DAL.Properties.PropertiesSearchDataset(strSql, _ > blnCounty) > > '*** additional criteria items removed for brevity *** > > Dim dvWeb As New DataView(dsWeb.Tables(0)) > > Dim strSort As String = "" > > If Not viewstate("SortField") Is Nothing Then > strSort = Convert.ToString(viewstate("SortField")) & > Convert.ToString(viewstate("SortDirection")) > Else > strSort = "PropertyID" > End If > > viewstate("blnCounty") = blnCounty > viewstate("blnPropertyType") = blnPropertyType > '*** additional criteria items removed for brevity *** > > dvWeb.Sort = strSort > dgdSearch.DataSource = dvWeb > dgdSearch.DataKeyField = "PropertyID" > dgdSearch.DataBind() > > > End Sub > > Private Sub dgdSearch_SortCommand(ByVal source As Object, ByVal e > As System.Web.UI.WebControls.DataGridSortCommandEvent Args) Handles > dgdSearch.SortCommand > > If dgdSearch.EditItemIndex = -1 Then > ' If SortDirection viewstate doesn't exist, add it > If viewstate("SortDirection") Is Nothing Then > viewstate.Add("SortDirection", " ASC") > End If > ' If SortField viewstate doesn't exist, add it > If Convert.ToString(viewstate("SortField")) Is Nothing > Then > viewstate.Add("CustomSortField", e.SortExpression) > Else > ' If SortField viewstate is the same as the sort > expression, then we > ' just want to change direction > If Convert.ToString(viewstate("SortField")) = > e.SortExpression Then > If Convert.ToString(viewstate("SortDirection")) = > " ASC" Then > viewstate("SortDirection") = " DESC" > Else > viewstate("SortDirection") = " ASC" > End If > Else > ' SortField viewstate is different than the sort > expression, we need > ' to set new sortfield > viewstate("SortField") = e.SortExpression > viewstate("SortDirection") = " ASC" > End If > End If > FillSearchDatagrid() > End If > > End Sub > > Private Sub dgdSearch_PageIndexChanged(ByVal source As Object, > ByVal e As System.Web.UI.WebControls.DataGridPageChangedEvent Args) > Handles dgdSearch.PageIndexChanged > > If dgdSearch.EditItemIndex = -1 Then > dgdSearch.CurrentPageIndex = e.NewPageIndex > FillSearchDatagrid() > End If > End Sub > > > > Private Sub dgdSearch_ItemDataBound(ByVal sender As Object, ByVal > e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles > dgdSearch.ItemDataBound > > If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = > ListItemType.AlternatingItem Then > Dim webCtrl As WebControl > For Each webCtrl In e.Item.Controls > If CType(e.Item.Controls(2).Controls(0), > LinkButton).CommandName = "Delete" Then > CType(e.Item.Controls(2).Controls(0), > WebControl).Attributes.Add("onclick", "return confirm('Are you sure > you want to delete this item?');") > End If > Next > End If > > End Sub > > Private Sub dgdSearch_DeleteCommand(ByVal source As Object, ByVal > e As System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles > dgdSearch.DeleteCommand > > > DAL.Properties.PropertiesDelete(Convert.ToInt32(dg dSearch.DataKeys(e.Item.ItemIndex))) > FillSearchDatagrid() > > End Sub > > Private Sub chkAllowDelete_CheckedChanged(ByVal sender As Object, > ByVal e As System.EventArgs) Handles chkAllowDelete.CheckedChanged > > If Me.chkAllowDelete.Checked = True Then > Me.dgdSearch.Columns(2).Visible = True > Else > Me.dgdSearch.Columns(2).Visible = False > End If > FillSearchDatagrid() > > End Sub > > Sub AddDynamicColumns() > > > Dim bndCol4 As New BoundColumn > bndCol4.HeaderText = "Property Name" > bndCol4.DataField = "PropertyName" > bndCol4.SortExpression = "PropertyName" > dgdSearch.Columns.Add(bndCol4) > > If Convert.ToBoolean(viewstate("blnPropertyType")) = True Then > Dim bndCol5 As New BoundColumn > bndCol5.HeaderText = "PropertyType" > bndCol5.DataField = "DescrPropertyType" > bndCol5.SortExpression = "DescrPropertyType" > dgdSearch.Columns.Add(bndCol5) > End If > > If Convert.ToBoolean(viewstate("blnCounty")) = True Then > Dim bndCol8 As New BoundColumn > bndCol8.HeaderText = "County" > bndCol8.DataField = "DescrCounty" > bndCol8.SortExpression = "DescrCounty" > dgdSearch.Columns.Add(bndCol > End If > '*** additional criteria items removed for brevity *** > > End Sub |
|
|
|
|
|||
|
|||
| Ken Cox [Microsoft MVP] |
|
|
|
| |
![]() |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Data disappearing on postback in GridView with Dynamic columns | =?Utf-8?B?dmFuZGls?= | ASP .Net | 2 | 01-25-2007 08:20 AM |
| How to add a Dropdown list to a datagrid at runtime (dynamic) without using template columns in ASP.NET and still have the ability to us the datagrid Update event. | Daniel Roth | ASP .Net Datagrid Control | 0 | 04-05-2005 03:58 AM |
| Binded Datagrid Formatting columns or hiding columns | ton | ASP .Net Web Controls | 2 | 02-11-2004 04:09 AM |
| Datagrid Dynamic Columns Postback and SelectedIndexChanged problems | George | ASP .Net Datagrid Control | 1 | 01-22-2004 04:49 PM |
| Columns and Inherited Datagrid...Active Schema does not support columns | rob thomson | ASP .Net Datagrid Control | 0 | 09-04-2003 03:09 PM |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc..
SEO by vBSEO ©2010, Crawlability, Inc. |




