How To Do Databinding On Postback

Jul 8, 2010

Working on an ASP.NET 4.0 project, which uses user controls to dynamically generate a form based on definitions stored in the database. One form field would look like this:

[code]....

Behind the scenes the control emits a RegularExpressionValidator based on the RefControl.ValidationFormat property.

This works fine, however the problem is that this architecture only allows us to validate with regexes. Now I need to add date format validation, based on the user's preferences (which aren't dependent on their UICulture).

Reluctant to refactor the entire user control setup, I need to be able to pass a on-the-fly regex pattern to the ValidationFormat property. Currently I'm using a data binding expression to bind the property to a page instance method:

[code]....

Works okay on first page load, but on subsequent postbacks the validation doesn't work. I think the issue is that the data binding expression doesn't evaluate at all, but I'm not sure I understand why. I'm calling Page.DataBind() in Page_Init whether Page.IsPostBack or not, so shouldn't this work?

If you see I'm barking up the wrong tree, any alternative solutions to the same problem are also welcome.

EDIT

Managed to solve this problem. The issue was with the way ASP.NET page life cycle invokes the user control's events before the page's own events. The control values were being initialized before the data binding on the page could happen.

Because I still need to do the control initialization before Page_Load to subscribe the controls to viewstate, I simply moved the initialization logic to the Page.InitComplete event so the call to Page.DataBind() could get called first.

protected void Page_Init(object sender, EventArgs e)
{
Page.InitComplete += new EventHandler(Page_InitComplete);
}

So the event tree becomes

User Control Page_Init => Hook InitComplete handler
Page (aspx) Page_Init => Bind data
User Control Page_InitComplete => Initialize the control

View 1 Replies


Similar Messages:

LoginView Inside FormView Control Is Not Databinding On PostBack?

Apr 20, 2010

I have a fairly simple form:

[code]....

However, once the update is finished (and I call e.Cancel = true), the LoginView control does not databind its children... so they are all blank. The FormView's viewstate is still fine, as all the rest of the controls outside of the LoginView appear fine. I even handle the FormView_DataBound event and a Trace shows that the FormView is being databound on postback.

Why then is the LoginView not keeping its ViewState/being databound? Here's a sample code snippet showing the flow:

[code]....

View 1 Replies

Forms Data Controls :: GridView Sorting, Paging And DataBinding / When Control Causes A Postback GridView Is No Longer Sorted?

Jan 10, 2011

I'm trying to extend the GridView control to enable sorting and paging for any situation.

When using my control I am fetching data from a database and filling a DataSet with it, then binding the GridView upon every page load. My first question would be, is this the correct approach?

To sort the GridView I am overriding the OnSorting method which stores the sort expression and direction in the ViewState, then creates a DataView and utalises the Sort method to sort the underlying data. It then sets the Data Source to this DataView and rebinds the GridView.

Paging is handled by OnPageIndexChanging which simply sets the PageIndex property and again rebinds the GridView.

My problem is; when any control causes a postback my GridView is no longer sorted, presumably because it is persistently rebound. If I don't rebind it then the GridView is empty on postback since the data isn't automatically stored in the ViewState. I have considered saving the data source in the ViewState but I would assume that this is bad practice for large amounts of data? - also DataViews are not seralisable.

The only solution I can think of currently is to override OnDataBound and sort the data every time. This results in a double sort when paging triggers a postback which seems inefficient. Code illustration of this below,

[Code]....

I'm looking for the cleanest 'best practice' solution as this is a learning exercise more than anything else.

View 12 Replies

C# - Databinding Does Not Work?

Dec 12, 2010

In MainPage.aspx I have

<asp:HyperLink runat="server" NavigateUrl='<%#"http://google.pl"%>'>test</asp:HyperLink>

It does not add an href tag but only outputs <a>test</a>.
When I do:

<asp:HyperLink runat="server" NavigateUrl='http://google.pl'>test</asp:HyperLink>

It works fine.

Why <%#"http://google.pl"%> does not work ?

How to debug it ?

View 4 Replies

Databinding With LINQ?

Aug 2, 2010

I'm databinding a radiobuttonlist with a LINQ query like so:

var query = from m in Db.Udt_Menu
orderby m.Name
select m;
this.radMenuSelection.DataValueField = "menuID";
this.radMenuSelection.DataTextField = "name";
this.radMenuSelection.DataSource = query;
this.radMenuSelection.DataBind();

However, when i want to update the record I need to set the selectedindex of the radiobutton to a value from the database. There is a table called udt_PageMenuSelection which has a column called menuID which is a foreign key to udt_Menu.menuID.

When i want to update an existing record, how do i set the selectedindex of the radiolist to the value equal to udt_PageMenuSelection.menuID ?

Do I need to do an additional query?

View 1 Replies

Databinding Gridview To Datatable?

Feb 18, 2011

basically I had a repeater that i needed to be able to now update row by row. I thought the easiest way would be to replace with a datagrid.

Here is the code:

Code:

[code]....

If I take away the select filtering, I then get this error on rowdatabound

Unable to cast object of type 'System.Data.DataRowView' to type 'System.Data.DataRow'.

View 1 Replies

Databinding - How To Bind With Some Textbox

Oct 25, 2010

I have in my code behind the following property

public string Firstname {get;set;}

when I want to bind it to some textbox I do the following:

<asp:TextBox runat="server" ID="txtFirstname" Text='<%# Bind("Firstname") %>'/>

then I want value put in this textbox to be set in my Firstname property (because I want to process it e.g. save this value) in my presenter.

[code]....

View 1 Replies

How To Put Code Into PreRender Such As Databinding

May 19, 2010

Would a call to a database be called if you put the databind method in the PreRender event of a listbox and the listbox was rendered on the screen for example? This is a specific example regarding a listbox, but basically does code in PreRender only get called if the control is rendered on the screen. If this is so, is it good practice to put code into PreRender such as databinding?

View 1 Replies

DataBinding Not Working In UserControl?

Jul 30, 2010

[code]....

((Alert)Container.DataItem).AlertId.ToString() the value I get for SystemObjectRecordId in debug is null (but it still works when assigned to the text attribute of a literal).

5) The location of the User Control is default.aspx and the control is comments.ascx. The code files do not share a namespace.

6) I have tried manipulating the OnItemDataBound property of my repeater but since I have proven with the literal that data is getting bound using a declarative value I am not sure what else I could be doing inside the method logic of OnItemDataBound.

Once the end-user makes a comment in the UI, the SystemObjectRecordId value (which should have already been assigned when the page loaded - and we know it does in the case of the literal) should get passed to the codebehind of the custom usercontrol:

[code]....

No matter what I do, I always get zero for SystemObjectRecordId when passed via the custom user control I have on my default.aspx page.

View 2 Replies

Polymorphism And Databinding To A Datagrid?

Feb 10, 2010

[Code]....

polymorphism and databinding to a datagrid?

View 2 Replies

AJAX :: Databinding The Accordion?

Jan 3, 2011

I'm having trouble with my accordion control. I'm trying to databind the control using my code- behind, but I can't seem to populate the accordion

My .aspx code is:

[Code]....

and my code- behind is:
[Code]....

View 1 Replies

C# - Databinding Two Sql Tables To One Repeater?

Oct 26, 2010

I'm very new to using Linq-to-SQL, that's why I'm asking this question. I've searched the site, but can't seem to figure out how to do this.

My problem is:

I have a database mapped with LINQ to SQL:

Table 1: PersonalTasks

TaskId
Header
[Content]
IsDone
CreatedDate

Table 2: Comments

CommentId
TaskId
UserId
Comment
CreatedDate

I have a repeater:

<asp:Repeater ID="PersonalTaskRepeater" runat="server">
<ItemTemplate>
<%#Eval("Header") %>
<%# Eval("Content") %>
Task done: <asp:CheckBox ID="IsDoneCheckBox" runat="server" Checked='<%#Eval("IsDone") %>' /><img src="Images/tick-circle.png" />

Here i want to access the comment table, with the comments related to the taskID

</ItemTemplate>
</asp:Repeater>

I've tried using: <%#Eval("Comment.Comment1")%> but that throws this error:

DataBinding:
'System.Data.Linq.EntitySet`1[[Assistr.Models.Comment,
Assistr, Version=1.0.0.0,
Culture=neutral,
PublicKeyToken=null]]' does not
contain a property with the name
'Comment'.

My code-behind:

var tasks = db.PersonalTasks.OrderBy(x => x.IsDone).ToList();
PersonalTaskRepeater.DataSource = tasks;
PersonalTaskRepeater.DataBind();

Do I need to use 2 repeaters to do what I want to do or? :)

View 1 Replies

Web Forms :: DataBinding With Expression Builders?

Sep 22, 2010

I'm trying to build a webpage without using any asp.net data binding controls ( listview, repeaters etc.)that I'm trying to do is to use stored procedure to get all necessary items, loop through them, and bind to labels, images etc.The problem is when i want to repeat .aspx code. I'm able to do everything i want in c#, but it's really pain to create all the necessary html elements such breaklines, spans, textboxes and so on.I tried to use expression builders, but it only repeats the last value from database.

Here is my code(ASPX):
[Code]....
c# code:
[Code]....

View 2 Replies

Web Forms :: Use Databinding Option In Wizard?

Oct 10, 2010

I have Wizard steps in a a Wizard control. Each step looks like a form with textboxes and dropdowns for users to complete. I don't want to lose the data as user completes each of the wizard step.

1. I'm thinking about saving user input to sql database when user clicks the next button with each wizard step but I wonder if this is wise since it would create many runs and writes to the server? If not the best way then what method should I use? I just don't want user to lose data if browser crash or for any reason.

2. Or should I use the DataBinding option in the Wizard? Would this be a better performance?

3. Another concern I have is that there are on average about 7, 8 HTML-Server CONTROLS (I added id and Runat="server" to the regular html controls) in a wizard step. If I load up all 7 steps and each step has 8 controls then is that a lot of controls? Wouldn't that affect performance easily when there are a few more users access this web app? Maybe I shouldn't use Wizard for this then? I should just have 7 forms and each form would have Next and Prev buttons that takes users to whatever page they want?

View 2 Replies

Controls That Replicate Following Databinding Technique

Apr 20, 2010

Nifty and free asp.net controls that replicate the following databinding technique? [URL]

View 11 Replies

.NET Checkbox Databinding To Byte Datatype?

Mar 1, 2010

How can I get asp.net to two-way databind (via Bind("myfieldname")) to a Byte value? I'm storing boolean values in Sql Server to a Byte type and it seems to be looking for boolean. Apparently I need something to convert my datasource's Byte

Looked into creating a method to call like MyConvertMethod(Bind("myfieldname")) but asp.net 4.0 did not allow that with "Bind()" though it allowed it with "Eval()" but Eval only seems to do one way databinding. I looked into the ConvertHandler but seems to be winforms and not webforms.

View 1 Replies

Databinding - Why Binding Expression Shows Nothing

Mar 7, 2010

I'm just testing a simple databind expression with:

<div>
Now: <%# DateTime.Now.ToString()%>
</div>

According to MSDN:

[URL]

literal text <%# data-binding expression %>

should work.

When I compile, it's OK but I got a blank instead of Now DateTime.

View 1 Replies

Databinding A Hyperlink From Access DB Fiels?

Apr 4, 2011

We are using a DataList with a couple fields inside the ItemTemplate. The one that is giving me trouble is the Hyperlink. The code is below. When the page loads the hyperlink renders like this:

[Code].....

I cannot see where the extra # signs are coming from. They don't appear to be in the field in the table.

View 1 Replies

Web Forms :: Databinding A Dropdownlist On The Parent?

Apr 15, 2010

I'm new to asp.net. I have a page with formview to read, edit and insert data to sql server 2008. On the page there are textboxes, dropdownlists. The Dropdownlist1 is bound to a lookup table. I also have a pop-up page to add, delete and update data underlying table. The problem is after i insert a new record to the lookup table and close the pop-up page, rebind the dropdownlist on the parent page. I'm refreshing the page with window.opener.location.href ....

but i want only the refresh the dropdownlist on the parent page.

View 2 Replies

Webforms - Working With DataBinding And Page_Load In MVP?

Apr 9, 2010

I'm using WebForms MVP to create some simple reporting applications. Most of these applications consist of a few search criteria inputs and a ComponentArt datagrid that I'm populating with data from the database.

Most of the markup is in a UserControl, which is in a content page with a master page. My problem is that the control's Page_Load event is firing before the control events that caused the postback in the first place. Basically, the user clicks the search button, and Page_Load is fired BEFORE Search_Click. This is messing with the databinding scheme I've been using.

So that's the question: Why is my Page_Load event firing before the event handler, and what can I do about it? I don't THINK this problem is related to WebForms MVP or ComponentArt, but obviously I could be wrong.

View 1 Replies

Databinding To A Sub Object Declarative Syntax?

Mar 9, 2010

What is the format for databinding to a complex "object"? I have a linq to sql class that has containment, i.e object.containedobject.I want to reference the sub objects fields declarative.So I've tried my MySubField.MyBasicProperty and that did not work, as well as, MySubField_MyBasicProperty.

View 2 Replies

Web Forms :: Using Properties With Databinding Syntax?

Feb 1, 2010

In an ascx control, it's possible to expose properties - let's assume the property name is a string property with a get accessor called DataField. If the ascx control contains a bindable templated control like a FormView and the FormView has a template that uses the two-way databinding syntax, why can't you provide a reference to the property in the call to the Bind method? I see that using the Bind method in a databinding expression causes the page parser to generate a method that is something like the following:

public System.Collections.Specialized.IOrderedDictionary @__ExtractValues__control8(System.Web.UI.Control @__container)

In which, I see the following line:

@__table["Some_Field"] = TextBox1.Text;

Now, this method (@__ExtractValues__control8) is an instance member on the same generated class that defines our DataField property, but when page parser goes to compile the control, I receive the following error:A call to Bind was not well formatted. Please refer to documentation for the correct parameters to Bind. I assume from this that the page parser requires a string literal to be provided, but I don't understand why.

View 8 Replies

Databinding - Testing For Null Inline On ASP

Sep 1, 2010

I've got a bit of code that I started to manage, and it's begun to fail due to some data missing in the database. This case could happen in the future, so I'd like to gracefully handle the nulls in the front end. Here's the current code:

<asp:DropDownList ID="ddlContact" runat="server"
SelectedIndex='<%# Bind("contactInfo") == null ? "" : Bind("contactInfo") %>'>

It doesn't seem to have any affect on it, and the page still throws a NullReferenceException. It needs to be a Bind() due to the two-way data binding requirement, so I can't use Eval(). I've tried to use the null-coallescing operator "??" but that gives me a compilation error stating that Bind() does not exist in the current context. That could would look like this:

<asp:DropDownList ID="ddlContact" runat="server"
SelectedIndex='<%# Bind("contactInfo") ?? string.Empty %>'>

View 2 Replies

Adding Dropdownlist Option After Databinding

Jan 6, 2010

have a dropdownlist (ddl2) which is dependent on the selected value of another dropdownlist (ddl1).I have set the appendatabounditems to "true" for ddl2 and have added a "Please Select" and a "Select All" options. The Options show in the list when the page first loads. When ddl1 is selected the page refreshes and ddl2 is populated, however the "Please Select" and "Select All" options have disappeared.

View 7 Replies

Databinding - .net - Dynamically Displaying Controls?

Jan 7, 2010

I am developing Quiz project. In DetailsView I want to display the Question and answers.

The number of answers vary for question to question.Say example

1 ) C# Support

(i) Generics
(ii) LINQ
(iii)EntLib

2 ) Find the odd one
(i) DB2
(ii) Oracle
(iii)MS-Access
(iv) Sql Server
(v) Javascript

so i can not fix the number of radio buttons.Some questions may have multiple answers.so i need to display checkboxes instead of radio buttons.

My Question is how to generate Radio Buttons or Checkboxed Dynamically ?

View 2 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved