tips (en)‎ > ‎

asp.net

DropDownList within Insert/Edit Template of DetailsView Triggers error  Eval(), XPath(), and Bind()...
The context: DropDownList within Insert/Edit Template of DetailsView, whose selected value is bound to a field of the DetailsView.
The problem: You get the error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control".
Potential source of the problem: your code is triggering the DetailsView to databind after it is in Edit or Insert mode.

PopupControlExtender displays popup in wrong position
When using multiple PopupControlExtenders in a page the positioning of the popup panels might get a little bit shifted. A solution is to always set a css-class with the style "display: none;" on all panels that will be used as popups. This will fix the alignment, and will also remove the flicker of the control when loading the page.

The source of this tip is http://benef-it.blogspot.com.es/2012/02/quick-tip-popupcontrolextender-displays.html

I tested it and it works like a charm!

ImageButtons causing unexpected postback after pressing Enter in textbox
ImageButton do not have a UseSubmitBehavior property that can be disabled to avoid unexpected postbacks of the page. When a user happens to hit enter on a textbox of the page with postback = false, the ImageButton is indeed hitted and the page postbacks to the url configured at the ImageButton ;-(  For instance, I had a set of icons in the masterpage used as short-cut menu controls. These icons were indeeed ImageButtons. Some of the pages, which used the masterpage, had a form including some textboxes with postback = false. Each time the user pressed enter, the page linked by the first menu icon was loaded!!! When that happen, the contents of the form was lost before it could be saved! 

Solution 1
A solution is to use a Image element wrapped in a LinkButton element instead of the ImageButton. Example:
 <asp:LinkButton ID="LinkButton9" runat="server" PostBackUrl="~/test/test.aspx">
         <asp:Image ID="ImageTest" runat="server" ImageUrl="~/Images/test.png" />
</asp:LinkButton> 

The drawback of this solution is that then the linkbutton losses the postback advantage, which is sometimes useful for clearing the contents of the current form. For instance, if a menu icon is clicked from the page linked by the same icon, then there is no page reload and, then, no clearing of the form ;-(

Solution 2
A most favoured solution is to wrap the contents of the page in a Panel. Then, use the DefaultButton Property to set up a button or imagebutton to trigger when the user presses enter. For instance, I set up a form 'save' button, which is triggered each time the user presses enter.

ImageButton/Button Click Event Handler Firing Twice
If you have a 'runat=server' and onclick="ImageButton1_click", and an event handler in your code-behind (ie. aspx.vb), it will cause the event to be fired twice. 

Wrong declaration
In aspx:

<asp:ImageButton id="ImageButton1" onclick="ImageButton1_click" runat="server"  ImageUrl="~/icon/whatever.png"></asp:ImageButton>

In aspx.vb:
    Protected Sub ImageButton1_Click(sender As Object, e As ImageClickEventArgs) Handles ImageButton1.Click

Correct declaration 1
In aspx:
<asp:ImageButton id="ImageButton1onclick="ImageButton1_click"  
 runat="server" ImageUrl="~/icon/whatever.png"></asp:ImageButton>
In aspx.vb:
    Protected Sub ImageButton1_Click(sender As Object, e As ImageClickEventArgs) Handles ImageButton1.Click

Correct declaration 2
In aspx:
<asp:ImageButton id="ImageButton1" onclick="ImageButton1_click" runat="server"  ImageUrl="~/icon/whatever.png"></asp:ImageButton>
In aspx.vb:
    Protected Sub ImageButton1_Click(sender As Object, e As ImageClickEventArgs) 
Handles ImageButton1.Click

Format Error When Creating/Editing Decimal Value in MVC
Solution:
  1. Install Globalize and jquery.validation.globalize Nuget packages
  2. Add to bundle
  3. Remove jquery.validation.unobstrusive.js from bundle
  4.  Add bundle with Globalize.js and jquery.validation.globalize.js to edit page
MVC or Web Api: Ya hay un DataReader abierto asociado a este Command, debe cerrarlo primero
This kind of instruction triggers the error:
var costumers = db.Customer.Include(c => c.CustomerAddresses);

The solution is:
var costumers = db.Customer.Include(c => c.CustomerAddresses).ToList();
Count Total Rows of a Paged Gridview
When using paging on a gridview, gridview.Rows.Count gets the number of rows within the current page ;-(

A solution to get the total number of rows matching your select operation is to use the Selected event of the ObjectDataSource!

Protected Sub ObjectDataSource1_Selected(sender As Object, e As ObjectDataSourceStatusEventArgs) Handles ObjectDataSource1.Selected
        Dim dt As DataTable = DirectCast(e.ReturnValue, DataTable)
        LabelCount.Text = "rows: " + dt.Rows.Count
        'e.AffectedRows cannot be used in this case because is -1 (use only to get the number of updated/inserted records)
    End Sub

Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

To be solved
Select DefaultImageID, ID, ... FROM TABLE


Comments