Tuesday, January 22, 2008

maintaining the tab order on page postback

The code to set focus to the next control after a postback is given below:
C#
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
WebControl wcICausedPostBack = (WebControl)GetControlThatCausedPostBack(sender as Page);
int indx = wcICausedPostBack.TabIndex;
var ctrl = from control in wcICausedPostBack.Parent.Controls.OfType<WebControl>()
where control.TabIndex > indx
select control;
ctrl.DefaultIfEmpty(wcICausedPostBack).First().Focus();
}
}
protected Control GetControlThatCausedPostBack(Page page)
{
Control control = null;
string ctrlname = page.Request.Params.Get("__EVENTTARGET");
if (ctrlname != null && ctrlname != string.Empty)
{
control = page.FindControl(ctrlname);
}
else
{
foreach (string ctl in page.Request.Form)
{
Control c = page.FindControl(ctl);
if (c is System.Web.UI.WebControls.Button || c is System.Web.UI.WebControls.ImageButton)
{
control = c;
break;
}
}
}
return control;
}

VB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack Then
Dim wcICausedPostBack As WebControl = CType(GetControlThatCausedPostBack(TryCast(sender, Page)), WebControl)
Dim indx As Integer = wcICausedPostBack.TabIndex
Dim ctrl = _
From control In wcICausedPostBack.Parent.Controls.OfType(Of WebControl)() _
Where control.TabIndex > indx _
Select control
ctrl.DefaultIfEmpty(wcICausedPostBack).First().Focus()
End If
End Sub
Protected Function GetControlThatCausedPostBack(ByVal page As Page) As Control
Dim control As Control = Nothing
Dim ctrlname As String = page.Request.Params.Get("__EVENTTARGET")
If ctrlname IsNot Nothing AndAlso ctrlname <> String.Empty Then
control = page.FindControl(ctrlname)
Else
For Each ctl As String In page.Request.Form
Dim c As Control = page.FindControl(ctl)
If TypeOf c Is System.Web.UI.WebControls.Button OrElse TypeOf c Is System.Web.UI.WebControls.ImageButton Then
control = c
Exit For
End If
Next ctl
End If
Return control
End Function

Tuesday, January 1, 2008

Databound checkboxlist aggravation

Discovered today that the databound checkboxlist items' SELECTED property doesn't reflect what's in the VALUE property. The following code on the checkboxlist databound event hanlder cleared things up for me:

Protected Sub cblRoles_DataBound(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles cblRoles.DataBound

Dim loItem As ListItem = Nothing
For Each loItem In cblRoles.Items
loItem.Selected = loItem.Value
Next
End Sub


Now, when the form loads, the items with Value=True now display with the checkbox selected.

Get the return value from a stored procedure

I just burt more hours than I care to admit figuring out this one. I was using an ObjectDataSource to call a stored proc which inserts records into a couple of tables, then returns the identity of the second table. No matter what I did, I could not retrieve the return_value. Instead, I got "Nothing" in the RETURN_VALUE output parameter.

After reading numerous posts out there, I finally figured out that a Strongly Typed Dataset will populate the RETURN_VALUE with the first column of the first row of the resulting result set. Since my stored proc wan't returning a result set, I was getting nothing. By simply ending the proc with the following, I got my RETURN_VALUE:

-- Do inserts and stuff...

SELECT Scope_Identity()
RETURN Scope_Identity()
END