Web Forms :: Custom Control With Databind And Collections
Jun 8, 2010
I've written a custom control that converts datasources into an IEnumerable and then binds the data. This works fine and I have LoadPostData working after postbacks. I can also using a collection to load it. Like adding listitems to a dropdownlist on an html page. This works fine and I have LoadPostData working after postbacks
[Category("Behavior"),
Description("A collection of CheckBoxItems"),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
NotifyParentProperty(true),
PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public CheckBoxFieldCollection CheckBoxItems
{....}
Kinda struggling on merging the two. I think when i databind, instead of adding my CheckBoxItem to Controls I think I need to add it to my collection CheckBoxItems and then proced with creating childcontrols. It works the first pass through when I bind to the datasource. When I do a postback my controls collection goes to null. Can anyone point me in which direction to go? Also, after databinding, if I do , MyControl.CheckBoxItems.Add(new CheckBoxItem(..)), the new one doesnt show up.
However it seems to render properly, it doesn't work anymore if I add some asp.net labels and input controls inside the property (see above asp.net code). I'll get the HttpException:
Unable to find control with id 'KeywordTextBox' that is associated with the Label 'KeywordLabel'.
Somewhat understandable, because the label appears before the textbox in the controlcollection. However, with default asp.net controls it does work, so why doesn't this work? What am I doing wrong? Is it even possible to have two control collections in one control? Should I render it differently?
I'm creating an ASP.NET Control which has a property collection for Columns which the user can edit in the Property Collection Editor..
In most circumstances this works fine, the collection can be edited in the designer and the appropriate tags are added inside the control in the ASPX file.
It works fine when
My control is added to the form
[Code]....
My control is added inside an Update Panel
[Code]....
[Code]....
However, if My control is inside a Content Placeholder which is inside an Update Panel, it fails to work as it should. In this scenario, when the Columns collection is edited in the designer, if you then switch back to Source view, the Columns collection is emptied. I have to save the aspx page before switching back to Source view for the changes to be kept.
[Code]....
how to get around this problem? Is there something I may have missed?
I have a simple custom control that boils down to a label and a listbox. Selections in the listbox are reflected in the label as comma separated values. My question is what is the best and simplest way to make the page developers experience of working with the custom control exactly mimic the experience of working with just the listbox - when it comes to working with data. For some reason it feels wrong to simply create mirrored properties in the custom control and forward their values to the internal listbox. A small, perhaps unrelated complication is that the final product has to play nice with the forms collection due to the nature of the host application.
I have a custom user control UserProfileLink on which I have two basic string properties, UID and UserFullName which I would like to use in a template column in a DataGrid like this:
I get an error saying "The server tag is not well formed" which leads me to believe this type of binding isn't possible. Is there a way to do this, or is it impossible?
I reference a user control in my ListView in this manner:
[Code]....
The problem is that the #Bind command doesn't update the database, but it does so when I paste the code directly into the aspx page (in other words, without the user control involved).
I made a composite control that contains a ListBox. I am using the Composite Control throughout my application but I can not bind the listbox in the Compositecontrol. I have setup a Public get to access my listbox in the composite control.
I have a Custom Repeater control that inherits from Repeater and has paging functionality, however when I click the next page button the first time it refreshes the control but does not change the page, if I click it again after that it changes page perfectly. I know what the issue is, when I click the next button it does a postback, then the data is bound to the repeater, and then after that the NextButton Event is handled.
Is there any way I can change the order of the page load events?? Or force the repeater to reload again after the event is handled?? I've included my Custom Repeater class bellow:
using System.Web.UI.WebControls; using System.Web.UI; using System.Data; using System.Collections; using System; namespace ASPresentation.Controls { [ToolboxData("<cc:PagedRepeater runat=server></cc:PagedRepeater>")] public class PagedRepeater : Repeater { public int PageSize { get; set; } public int CurrentPageIndex { get { return Convert.ToInt16(Page.Session["ProjectIndex"]); } set { Page.Session.Add("ProjectIndex", value); } } public PagedDataSource pagedData = new PagedDataSource(); LinkButton NextBtn = new LinkButton(); LinkButton PrevBtn = new LinkButton(); public bool IsLastPage { get { return pagedData.IsLastPage; } } public bool IsFirstPage { get { return pagedData.IsFirstPage; } } public override object DataSource { get { return base.DataSource; } set { pagedData.DataSource = (IEnumerable)value; } } protected void NextButtonClick(object sender, EventArgs e) { if (!IsLastPage) { CurrentPageIndex++; } } protected void PrevButtonClick(object sender, EventArgs e) { if (!IsFirstPage) { CurrentPageIndex--; } } protected override void OnInit(EventArgs e) { base.OnInit(e); NextBtn.Text = "Next"; PrevBtn.Text = "Prev"; NextBtn.Click += new EventHandler(NextButtonClick); PrevBtn.Click += new EventHandler(PrevButtonClick); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); base.Controls.Add(PrevBtn); base.Controls.Add(NextBtn); } protected override void Render(HtmlTextWriter writer) { base.Render(writer); } public override void DataBind() { pagedData.AllowPaging = true; pagedData.PageSize = PageSize; pagedData.CurrentPageIndex = CurrentPageIndex; base.DataSource = pagedData; base.DataBind(); } } }
Using VB.net: I have several collections, each collection has several items and each item has several 1-d arrays (all the same length). Is it possible to print each one of these arrays through an ASP Control?
in datalist templatefield, i want to bind an imagebutton's visible property. the scenario is .. it is a picture comments page like one in facebook. if its your own comment, the delete button is shown, otherwise not. the problem is .. for the imagebutton i want to set visible = false, if the user logged in is the same user as the one who commented. how can i do that ?
currently im using this code on datalist databind event. but the label is returned as null
public bool set_visibility() { Label lb = DataList3.FindControl("Label7") as Label; if (lb.Text == Session["user_id"].ToString()) return true; else return false; } findcontrol dosent return the control.
i'm opening a new window and passing on to it variables in a querystring, the values are loaded into a function that is filling my dataset and then filling my Gridview. after i press the button field in my grid to delete an item, the databind doesn't show the updated grid but remains the same. i've noticed that if i navigate back and refresh i see it correctly. in FF btw no problem.
However, when I try to build the control I'm getting the error as shown in the subject. I've tried searching, and it seems the signature for the original function matches the one I have, and all other solutions I've seen uses the same signature.
I have a custom control that is based off the gridview control located at: here The control is basically a gridview that automatically creates a column of checkboxes that can be used to "check" individual rows in the gridview. During the gridview's "CreateColumns" event, the "checkboxcolumn" is created dynamically. The checkboxcolumn also contains another checkbox in the header that is used to "select/deselect all" checkboxes in the column. Since the gridview does not automatically remember the state of the checkboxes in the checkboxcolumn on postback, I added a method to the control called "SaveCheckBoxState" which stores the indexes of the checked rows in Viewstate, and then I modified the "OnRowDataBound" event to check the Viewstate and reset the checkboxes based on the Viewstate.
I then added a call to "SaveCheckBoxState" in the gridview's OnSorting and OnPageIndexChanging events. This works great so long as I'm sorting or changing pages. However, I need it to update the viewstate everytime someone clicks or unclicks one of the checkboxes. At this time, the checkboxes are rendered with an onclick event that calls some javascript to highlight the row, or in the case of the checkbox in the header, to select/deselect all checkboxes. I need to call the "SaveCheckBoxState" method from the javascript used by the customcontrol, or I need to find a way to modify viewstate from javascript and perform the same action as "SaveCheckBoxState".
I've tried adding the "SaveCheckBoxState" to the onclick event declaration in the checkboxes, but when run, it simply tells me that the method is undefined. It doesn't exist in the parent page, and I don't think I should have to make an event for the parent page to pass the click to. It seems to me this should be all self contained within the custom control. Does anyone know how I can acheive this? Here is the code for the gridview OnPreRender event where the onclick event of the checkbox is set:
protected override void OnPreRender(EventArgs e) { // Do as usual base.OnPreRender(e); // Adjust each data row foreach (GridViewRow r in Rows) { // Get the appropriate style object for the row TableItemStyle style = GetRowStyleFromState(r.RowState); // Retrieve the reference to the checkbox CheckBox cb = (CheckBox)r.FindControl(InputCheckBoxField.CheckBoxID); // Build the ID of the checkbox in the header string headerCheckBoxID = String.Format(CheckBoxColumHeaderID, ClientID); // Add script code to enable selection cb.Attributes["onclick"] = String.Format("ApplyStyle(this, '{0}', '{1}', '{2}')", SelectedRowStyle.CssClass, style.CssClass, headerCheckBoxID); // Update the style of the checkbox if checked if (cb.Checked) { r.BackColor = SelectedRowStyle.BackColor; r.ForeColor = SelectedRowStyle.ForeColor; r.Font.Bold = SelectedRowStyle.Font.Bold; } else { r.BackColor = style.BackColor; r.ForeColor = style.ForeColor; r.Font.Bold = style.Font.Bold; } } }
I have a custom control which inherit from the Table class and in the constructor, it takes a an integer as an argument. There is no empty constructor.
Is there a way for the user to set that variable in the properties window after they drag the control onto a form.
I know some .NET controls, you can set the source for the parameter to different things like another control's property, QueryString using just the properties window.
Right now, I have to create the control dynamically. I read the query string and then created the object.
here's a situation and I would appreciate your response.
I have programmatically created the Wizard control:
Page_Load(obj s, evargs e) { Wizard ClaimDetailWizard = new Wizard(); foreach(int item in selectedItems) { //create new step //added custom control to new step //add step to wizard } //added wizard to a placeholder on a page }
Based on List I get from Session i added new steps to my wizard To each step I had added a custom control
Each custom control in tern contains another custom Gridview Control in it.
So here's the problem when the page loads for example for two steps. All is good Wizard does what it's supposed to do.
But when I try to use sorting or paging in that custom Gridview. Somehow it displays the gridview I should see in the next step of the wizard.
Also what I'm noticing through debugging. Is that when I press next in the wizard I go back to the original page where I do all of the code specified above, and it recreates the wizzard. But it goes to the next step. Is this the way wizard supposed to work? Just doesn't seem very efficient.
created a Custom Textbox Control that is intended to be a "masked date" textbox, so that I can handle different ways a user might enter in a date and make it work no matter what.I did this by creating a custom control with 3 separate Textboxes one for month,one for day one for year,then do some handling for different things that could be entered and it would all work.
The problem I am having is,when I add this control to the page,if I do an alert to check the Text value of the custom control it just gives me "[ServerControl1]" even if I enter in a date.
I tried doing an AutoPostback after entering so that it would refesh the Viewstate to have the values of the textboxes but the control doesnt seem to actually postback, even though I have it enabled.accessing the Textbox value after someoen has entered a date?first custom control I have created and have been using google.
I have created a user control and am ready to convert it into a custom control. I am trying to follow:
[URL] the steps mentioned in the link.
I am not seeing Build / Publish Web Site option.
I am using VS 2008. I only see Publish User Control but it doesn't give all the settings mentioned below. When I do publish user control, it just copies the ascx file to the desired location. how can I convert the user control to custom control.
I have a blank/unbound GridView control on my form and I am binding it in the code behind like this:
GridView1.DataSource = _dataSet DataBind()
Running this code populates my GridView control with all the columns and data that _dataSet has. I want to display only some of the columns, change the column names, and rearrange some of these columns too (i want the last column from the actual sql database table to be displayed first in my GridView).