Category: ASP.NET Webforms

Installing JQuery in Visual Studio step by step

All the knowledge is out there, but for people new to jQuery it can be a challenge to make a more or less clean integration in Visual Studio. The key components are the nuget package manager and the script manager component, which often are described separately. Keep in mind there are multiple methods to get a valid jQuery installation, this is just a method that is both easy to install and maintain, and more or […]

Hiding a control’s commandfield button at runtime

Hiding a command button at runtime can be archieved by casting the commandfield in the DataBound event, and accessing the buttons from there. The sample below shows the hiding of a button based on a security role. protected void DetailsViewDocuments_DataBound(object sender, EventArgs e) { // Check role string currentuser = Common.GetRequestUser(); // some user retrieval process if (!Shared.API.Permissions.IsSomeRole(currentuser, _database)) // permissions check { // disable edit and delete buttons CommandField cmd = (CommandField)DetailsViewDocuments.Fields[6]; cmd.ShowEditButton = […]

Custom DataContext and Query with a Gridview bound to a LinqDataSource

Using an editable grid with a LinqDatasource with a custom connection string and query Binding to a custom datasource Create a custom datacontext object connected to the database to be used; private string _database; private SomeDataContext _dc; protected void Page_Load(object sender, EventArgs e) { _database = Common.GetRequestConnectionString(); _dc = new SomeDataContext(_database); } Catch the contextcreating event on the datasource, and ovveride the ObjectInstance: protected void LinqDataSource1_ContextCreating(object sender, LinqDataSourceContextEventArgs e) { e.ObjectInstance = _dc; } Setting […]

Setting the column width on a gridview column from code

To set the widht of a column on a GridView in ASP.NET you can hook into the RowDataBound event. This code detects the header line (datacontrolrowtype.header) and then hooks in to set the width of the columns. Instead of percentages you can use any Unit.* measure you like (pixels, mm) VB.NET Example Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound If e.Row.RowType = DataControlRowType.Header Then e.Row.Cells(0).Width = Unit.Percentage(10) e.Row.Cells(1).Width = Unit.Percentage(20) […]

Validation of a detailsview field against a numeric value

Validation of a field inside a detailsview does not need to be done with the Updating or Editing events, but can be accomplished by a simple validation control in the markup language. Convert the field in the detailsview to a template field, and use a standard validation control. <asp:CompareValidator ID=”CompareValidator2″ runat=”server” ErrorMessage=”Numeric input required” Operator=”DataTypeCheck” Type=”Double” ControlToValidate=”TextBox2″> </asp:CompareValidator> And applied to a data bound field: <asp:TemplateField HeaderText=”Cost amount” SortExpression=”wsCostAmount”> <EditItemTemplate> <asp:TextBox ID=”TextBox2″ runat=”server” Text='<%# Bind(“wsCostAmount”) […]

ENTER key behaviour in ASP.NET

The behaviour of the ENTER key in an ASP.NET application can vary greatly between browsers and versions. Worse: It can change depending on the content of your page. Depending on this browser and version, you may also observe the form getting submitted, but the event handler code not executed… Especialy when using a pagemaster you will generaly observe the enter key taking a “wrong turn”, and use one of the buttons on the master page […]

Validating DetailsView field values during Insert and Update

If you want to perform validation on the fields of a DetailsView at run time you can hook into the ItemInserting and ItemUpdating events as shown below. By Setting e.Cancel to true, it is possible to abort the insert/update action, and thus force the user to return to the form. /// <summary> /// Check if the chosen date represents a valid Date, and check the lower and upper bounds /// </summary> /// <param name=”sender”></param> /// […]

Changing a field into a date editor

The following example shows the markup for a DATETIME field from a database bound to a ASP.NET DetailsView before and after converting it into a templatefield and binding it to the ASPxDateEdit control. Default markup after converting to a template field: <asp:TemplateField HeaderText=”GebDat” SortExpression=”hrGebDat”> <EditItemTemplate> <asp:TextBox ID=”TextBox2″ runat=”server” Text='<%# Bind(“hrGebDat”) %>’></asp:TextBox> </EditItemTemplate> <InsertItemTemplate> <asp:TextBox ID=”TextBox2″ runat=”server” Text='<%# Bind(“hrGebDat”) %>’></asp:TextBox> </InsertItemTemplate> <ItemTemplate> <asp:Label ID=”Label2″ runat=”server” Text='<%# Bind(“hrGebDat”) %>’></asp:Label> </ItemTemplate> </asp:TemplateField> Markup after binding to a ASPxDateEdit […]

Handling the gridview’s pageindex from code

When binding a custom dataset to a gridview and enabling paging, you will be greeted by an error stating that the griview started the PageIndexChanging event, and that the event wasn’t handled. This occurs because the built in sqldatasource will handle paging for you by caching the dataset, and by binding your own dataset you bypassed this behaviour. If you have no further custom handling you need, it is sufficient to pass the new page […]

Next Page »