Using RadioButton Controls inside a Repeater (groupname problem)

Radio Buttons have the attribute ‘groupname’. The point of this attribute to to ensure any radio buttons in the same group are mutely exclusive (only one in the group can be selected).

This is the case when using them normally but as soon as you put them into a repeater control (e.g. datalist) .Net scrambles the group name value and it no longer works as a group. All can be selected.

This can be fixed with a bit of Javascript and some code during the list databinding event (provided by Microsoft).

See this link -> http://www.codeguru.com/csharp/csharp/cs_controls/custom/article.php/c12371/

Works for .Net 1.0 and 2.0.

Set CheckBox ‘checked’ from SQL DB if an entry exists without any code-behind

Found an interesting way to set the checked value of a checkbox as a databound control. Used in a datalist for example.

The checkbox in the datalist bound to a value in the usual way:

<asp:CheckBox ID=”MainCatCbx” runat=”server” Checked='<%# Eval(“Active”)%>’ CssClass=”smalltext12″ />

Then the query, this was fairly complicated as I wanted to join 2 tables and set the checkbox to ticked if a value was found and not-ticked if it wasn’t, but still display all the records.

The important part (bold) adds a column (active) which is detmind by the result of the case statement. If the DBID column is null the the active column is set to 0(zero) or 1 (one) and cast as a bit so that when the binding takes place it is evaluated as true or false for the checkbox checked value.

SelectCommand=”SELECT tb1.DBID, tb1.Title, tb2.DBID,
CASE WHEN tb2.DBID IS NULL THEN CAST(0 AS bit) ELSE CAST(1 AS bit) END AS Active,
tb1.ParentDBID FROM tb1 LEFT OUTER JOIN tb2 ON Specs_NavTbl.DBID = tb2.CatDBID
WHERE (tb2.DBID = ?) OR (tb2.DBID IS NULL)
AND (tb1.ParentDBID = 0)”

I found this to become usefull rather than writing codebehind to populate datasets from the DB and manually loop through and set the checkbox checked values etc.

 

Formview/Detailsview EditMode & FindControl

This stumped me for a bit today.

I changed a Formview to defaultmode=”edit” and found that FindControl would not work either in Page_Load or even in the FormView databound event – which I found very odd.

Anyway to solve it I just attached a function to the Prerender event for the controls I was trying to change values for (TextBox) and set it there.

Summary:

Page_Load or DataBound are no good for accessing controls on a Formview in Editmode.

Use the controls Prerender event to handle updating the control value before the page is displayed.

ASP.Net -> object does not exist in the current context – really??

Another one of those problems that can stump you for a while. I often make backups of files as I progress and sometimes leave them in the website folder, this was fine with classic ASP but as I have found with ASP.Net can cause all sorts of problems.One issue I was having was trying to reference a component in my aspx page from the codebehind page and seeing the following error when trying to reference a label control:

“Label2 does not exist in the current context”

So I found that the problem was even though I renamed one of my old files it still contained the class that my codebehind was referencing (i.e. the class names were still the same) – and therefore was not seeing the newly added Label control to my new code.Shame the compiler doesn’t realise the duplication and give some indication.

Lesson: Remove any backed up files from the web folder and/or build directory to avoid pain 🙂

Note:
From a comment made by Chris (below) this can also be caused by a missing files if it was built as a ‘Web Application’ project and you are now trying to build it as a ‘Web Site’ project (e.g. could be missing a designer.cs file).

Either when downloading source from another developer or migrating a web app from one server to another you may find it was actually created as a ‘Web Application’ project in Visual Studio, and if you have created it as a ‘Web Site’ project you may hit problems. The solution to this is to convert the project to a ‘Web Application’ (right click on your project in solution explorer) and then rebuild it.