Thursday, October 30, 2008

Binding to a DropDown list in a Footer Row

I typically use a separate ObjectDataSource that I place outside the GridView for clarity and easy of access in the CodeBehind. Alternatively, you could load it by hand using FindControl or declaring an OnLoad handler.


<footertemplate>
<asp:DropDownList ID="inDataAssetIDFooter" runat="server" DataSourceID="dsDataAsset"
DataValueField="ID" DataTextField="Name" CssClass="DropDownList" AppendDataBoundItems="true">
<asp:ListItem Value="0">Select Data Asset</asp:ListItem>
</asp:DropDownList>
<asp:CompareValidator ID="vldDANameFooter" runat="server"
Operator="GreaterThan" ValueToCompare="0" ControlToValidate="inDataAssetIDFooter"
Text="*" ErrorMessage="Data Asset is Required"/>
</footertemplate>

<asp:ObjectDataSource ID="dsDataAsset" runat="server" SelectMethod="GetUnRelatedDataAssetsByAppID"
TypeName="MyProj.Lib.DataAssetDAL">
<SelectParameters>
<asp:QueryStringParameter Name="ID" QueryStringField="PID" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>

Tuesday, October 28, 2008

FileUpload file type validation using regular expression validator

The MSDN docs have an excellent example of how to perform file type validation using regular expressions. I tweaked the implementation to allow for configuration via the web.config:

<asp:RegularExpressionValidator
id="vldDocument" runat="server"
ValidationExpression=""
OnLoad="Setup_Doc_Validator"
ControlToValidate="inDocument" />

The codebehind gets the following handler:

    Protected Sub Setup_Doc_Validator(ByVal sender As Object, ByVal e As System.EventArgs)
Dim docValidator As RegularExpressionValidator = CType(sender, RegularExpressionValidator)

docValidator.ValidationExpression = DocumentDAL.GetValidationRegularExpression
docValidator.ErrorMessage = String.Format(ProjectConstants.DOC_TYPE_ERROR_MESSAGE, _
DocumentDAL.GetAllowableTypes())
End Sub
    Public Shared Function GetValidationRegularExpression() As String
Dim allowableTypes As String = DocumentDAL.GetAllowableTypes()
Dim regEx As String = "^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+("

' Note, the file extensions must be PIPE delimited in the Reg Exp, but the web.config uses commas
regEx &= allowableTypes.Replace(",", "|") & ")$"
Return regEx
End Function


Tuesday, October 21, 2008

IE 6.0 Hack - Float Issue

I ran into a strange browser compatibility issue when displaying a GridView.  FireFox wanted to float the grid off the screen to the right until I added Align="Left" to the GridView, but IE reacted by floating the grid behind the master page template so the content got truncated.  I ended up adding an IE hack to the CSS by placing “* html” before the class:

* html .ReportGrid {float:none;}

 

Wednesday, October 15, 2008

Oracle PL/SQL Split Function Boosts WHERE IN Clause Usability

Big thanks to Tech Republic for an article about creating a SPLIT user-defined function that accepts a delimited string and creates a table with N rows of data:




function Split
(
p_list varchar2,
p_del varchar2 := ','
) return split_tbl pipelined
is
l_idx pls_integer;
l_list varchar2(32767) := p_list;
l_value varchar2(32767);
begin
loop
l_idx := instr(l_list,p_del);
if l_idx > 0 then
pipe row(substr(l_list,1,l_idx-1));
l_list := substr(l_list,l_idx+length(p_del));
else
pipe row(l_list);
exit;
end if;
end loop;
return;
end split;


 

And here’s how I used it to perform a “dynamic” query passing in a list of values:




select *
from myTable
where IN_VARCHAR_LIST IS NULL OR
APP.AGENCY_ID IN(
select column_value
from table(Split(IN_VARCHAR_LIST)))


Thursday, October 2, 2008

Warning Users About Unsaved Changes

I was asked to implement a popup warning to alert users they were going to lose data if they navigated away from a form without saving. It turned out to be easier than I thought by using window.onbeforeunload



<script language="javascript">
var doCheck = false;
window.onbeforeunload = checkSave;

function mustCheck() {
doCheck = true;
}

function checkSave() {
if (doCheck) {
return "You have some changes that have not been saved. If you CONTINUE, your changes WILL BE LOST.";
}
}
</script>

And then adding a startup script in the FormView PreRender

ClientScript.RegisterStartupScript(Me.GetType, "mustCheck", "mustCheck();", True)

UPDATE - You have to add the following attribute to any buttons that do not need the warning (Cancel, Save, etc.):

OnClientClick="doCheck = false;"