Bunch's Blog

One day I'll have a catchy subtitle, one day
posts - 87 , comments - 125 , trackbacks - 0

CSharp

There are 16 entries for the tag CSharp
BoundField Formatting
If you have a GridView that uses BoundFields but does not display line breaks or a lot of spaces quite right here is a way to get around that. This example uses a GridView with one BoundField that uses a DataField of comments. The comments are stored in a database and entered from another page which displays the comments properly in a textbox with all the spaces and line breaks you intended. The trick to get a BoundField to show those items is to take the item and replace the \r\n with a <br /> ......

Posted On Wednesday, May 23, 2012 4:24 PM | Comments (0) |

Finding Buried Controls

This post is pretty specific to an issue I had but still has some ideas that could be applied in other scenarios. The problem I had was updating a few buttons so their Text values could be set in the code behind which had a method...

Posted On Wednesday, March 28, 2012 2:27 PM | Comments (0) |

RadGrid PagerStyle in Code Behind
Recently I ran into an issue where the paging text in a RadGrid control needed to be customizable. In my case I was specifically looking at the "Page size" and "xx items in yy pages" text. Getting around it for the "Page size" was a snap. If your RadGrid is called rgTest one line would do it: rgTest.PagerStyle.PageSizeL... = "your text here"; The "xx items in yy pages" was a bit trickier, but still handled in one line of code. rgTest.PagerStyle.PagerText... = "{4} {5} things in {1} my pages"; ......

Posted On Tuesday, March 6, 2012 3:56 PM | Comments (0) |

Retrieving a Logged On User ID
Here is an easy way to retrieve the user ID from who ever is logged on to a PC using ASP.Net. This is handy for ASP.Net applications where you want to put a "Welcome joe user" label at the top, auto populate a form with the user's ID or add the user ID to some data you are storing back into a database if you keep track of who edited a record last. The code below assumes you have an aspx page with two labels on it, lblFName and lblLName. It grabs the user ID with HttpContext and then splits the ID ......

Posted On Friday, January 20, 2012 8:22 AM | Comments (0) |

Using WCF Service With a Class

This post is an example of how to write a WCF Service using a class. The example uses Visual Studio 2010, written in C#, SQL Server 2008 and hosted in IIS. So lets have at it.

Posted On Wednesday, January 18, 2012 9:19 AM | Comments (1) |

Finding a GridViews Edit Control
Here is one way to access a control in a GridView right after a user clicks a button to change from view to edit mode. This example shows a TextBox that gets populated with today's date. The GridView is setup like this:<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="TestID" DataSourceID="SqlDataSource1" onrowdatabound="GridView2_R... > <Columns> <asp:TemplateField> <ItemTemplate> <asp:Button ID="btnEdit" runat="server" Text="Edit" ......

Posted On Friday, December 23, 2011 11:40 AM | Comments (0) |

XmlWriter Formatting
Setting up formatting when using a XmlWriter is pretty easy. Declare a XmlWriterSettings variable and set the formatting options you want. The two main items are for indenting and new lines. You add the XmlWriterSettings variable as the second parameter in the XmlWriter.Create function. VB.Net Example Dim mySettings As New XmlWriterSettings() mySettings.Indent = True mySettings.NewLineOnAttributes = True Using writer As XmlWriter = XmlWriter.Create("c:\test.x... settings) C# Example XmlWriterSettings ......

Posted On Monday, July 18, 2011 12:22 PM | Comments (0) |

Setting a drop down’s selected value fails
Recently I had an issue where I just could not set a drop down list’s selected value from the code behind. I set other drop down list’s selected values from the code behind with no problem. Just this one was giving me grief. So here is how I set the values normally (the drop down lists are inside a formview): VB.Net Dim ddl As DropDownList ddl = fvTest.FindControl("ddlWorks") ddl.SelectedValue = 5 C# DropDownList ddl; ddl = (DropDownList)fvTest.FindCo... int j = 5; ddl.SelectedValue ......

Posted On Wednesday, March 30, 2011 8:11 AM | Comments (0) |

Access Master Page Controls II
Here is another way to access master page controls. This way has a bit less coding then my previous post on the subject. The scenario would be that you have a master page with a few navigation buttons at the top for users to navigate the app. After a button is clicked the corresponding aspx page would load in the ContentPlaceHolder. To make it easier for the users to see what page they are on I wanted the clicked navigation button to change color. This would be a quick visual for the user and is ......

Posted On Wednesday, March 16, 2011 9:09 AM | Comments (0) |

Inserting a New Row into a Datatable
Adding a new row to a datatable in a specific position is a snap. The scenario in this case would be that you have a datatable filled via a stored procedure that is also used in other applications so it can not be altered. The datatable is used to populate a dropdownlist and that list needs to have the always popular ‘Select All’ option as the first item. To add this in you could use code like what is listed below after the datatable is populated from the stored procedure. The example uses two columns ......

Posted On Friday, October 30, 2009 12:36 AM | Comments (0) |

Updating Parameter Check
Every now and again you may come across an error when trying to update a database from a data driven control (i.e. gridview, formview) that is tied to a SQL or Object data source. The errors usually are along the lines of “Could not find xxx that has parameters” along with a list of those parameters. Or an error that states there are too many parameters being passed. Aside from looking for typos, counting or lining up the parameters I will use one of these techniques to try and narrow down what may ......

Posted On Friday, October 2, 2009 5:47 AM | Comments (0) |

Loading Data on a Ajax TabPanel Click
Here is one way to have data load only when a TabPanel is clicked. In my example I have an aspx page with a TabContainer, two TabPanels both with their own ObjectDataSources that fill GridViews. The first tab’s ObjectDataSource and GridView will run on the page load and that is fine since it would be the first thing a user sees. The overall idea is to have the ObjectDataSource for the second tab to not run on the page load and then have the GridView on the second tab databind only when the tab is ......

Posted On Friday, May 15, 2009 10:48 AM | Comments (2) |

Quick and Dirty Calendar Validation (Date Required)
Probably everyone has at one time or another had a form that needed some sort of date to be entered. If you are like me and use ASP.Net and want to use the ASP calendar control the asp validators don’t work. Well without some work arounds to the calendar control. However if you just needed to validate if the user clicked on a date in the calendar control you can just use the regular calendar control and a customvalidator like this. The calendar control: <asp:Calendar ID="caltest" runat="server" ......

Posted On Tuesday, February 17, 2009 3:00 PM | Comments (4) |

2-Way Binding with CascadingDropDown
This post deals with 2-way binding of data to a CascadingDropDown. It assumes that the CascadingDropDown is already configured to return data to the list. I had my DropDownList and CascadingDropDown pairs within a FormView and it is bound to a SqlDataSource. < asp : Label ID ="Label1" runat ="server" Text ="Mfr" /> < asp : DropDownList ID ="ddlMfrs" runat ="server" /> < cc1 : CascadingDropDown ID ="cddMfr" runat ="server" TargetControlID ="ddlMfrs" Category ="MfrID" PromptText ="Select ......

Posted On Wednesday, October 1, 2008 9:08 AM | Comments (5) |

Tabbing Issue with AutoPostBack
Here was a little issue that caused some frustration. I have an app with an UpdatePanel and inside that are two Textboxes each with their own RequiredFieldValidator. Both Textboxes had AutoPostBack set to True. Also each used an OnTextChanged event to trigger some checks from the code behind. The issue was after entering a valid value in Textbox1 you could not tab directly to Textbox2. When you hit the tab key the cursor would blink once in Textbox2 and then disappear. If you kept hitting tab the ......

Posted On Thursday, September 18, 2008 3:17 PM | Comments (0) |

JavaScript: Hurdling the ScriptManager
This piece of code is related to a previous post of JavaScript Window from Code Behind. The idea is the same; I needed to get some JavaScript to run from the code behind. For this example it was to open a new window. The real difference with this and the previous post is using ScriptManager instead of ClientScript. VB.Net Code Dim strScript As String = "<script language='javascript'>" strScript += "window.open('URL Goes Here');" strScript += "</script>" ScriptManager.RegisterStart... ......

Posted On Friday, June 20, 2008 9:54 AM | Comments (1) |

Powered by: