Search

just show me the code

Tuesday, September 29, 2009

Xml Literals in vb

Here I am creating an XElement in C#

   19         public static XElement PropertyTable(List<Property> propertyList)
   20         {
   21             XElement root = new XElement("div");
   22             root.Add(new XElement("div"
   23                 , new XAttribute("class", "propertyCount hand")
   24                 , string.Format("{0} properties", propertyList.Count)));
   25             XElement tableElement = new XElement("table", new XAttribute("class", "propertyTable"));
   26             root.Add(tableElement);
   27             XElement headerRow = new XElement("tr");
   28             headerRow.Add(new XElement("th", "Code"));
   29             headerRow.Add(new XElement("th", "Value"));
   30             tableElement.Add(headerRow);
   32
   33             foreach (Property p in propertyList)
   34             {
   35                 XElement projectRow = new XElement("tr");
   36                 projectRow.Add(new XElement("td", p.Code));
   37                 projectRow.Add(new XElement("td", p.Value));
   38                 tableElement.Add(projectRow);
   39             }
   40             return root;
   41         }

Here I am creating the EXACT SAME XElement in VB using xml literals


   19     Public Function PropertyTable(ByVal propertyList As List(Of [Property])) As XElement
   20       Dim root = <div>
   21                    <div class="propertyCount hand"><%= String.Format("{0} properties", propertyList.Count) %></div>
   22                    <table class="propertyTable">
   23                      <tr>
   24                        <th>Code</th>
   25                        <th>Value</th>
   26                      </tr>
   27                      <%= From p In propertyList Select _
   28                        <tr>
   29                          <td><%= p.Code %></td>
   30                          <td><%= p.Value %></td>
   31                        </tr> %>
   32                    </table>
   33                  </div>
   34       Return root
   35     End Function  

which would you rather read?  

Tuesday, July 7, 2009

Using jQuery with ASP .NET

.............

.............
thanks to dotnetslackers
.............

Page.aspx
...........

    3
    4 <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    5
    6     <div>
    7         <input id="name" type="text" />
    8         <input id="number" type="text" /><br />
    9         <br />
   10         <input id="Button1" style="width: 158px" type="button" value="button" 
   11             language="javascript"  />&nbsp;
   12     </div>  
   13
   14 </asp:Content>
   15 <asp:Content ID="Content3" ContentPlaceHolderID="javascript" runat="server">
   16     <script src="../Content/Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
   17     <script> 
   18         $(document).ready(function() {
   19             $("#Button1").click(function(event) {
   20                 $.ajax({
   21                     type: "POST",
   22                     url: "SimpleService.asmx/SayHelloJson",
   23                     // Looks like this           {'Name': 'Payton', 'Number': '34'} check it in firefox
   24                     data: "{'Name': '" + $('#name').val() + "', 'Number': '" + $('#number').val() + "'}",
   25                     contentType: "application/json; charset=utf-8",
   26                     dataType: "json",
   27                     success: function(msg) {
   28                         AjaxSucceeded(msg);
   29                     },
   30                     error: AjaxFailed
   31                 });
   32             });
   33         }); 
   34        function AjaxSucceeded(result) {  
   35            alert(result.d);  
   36        }  
   37        function AjaxFailed(result) {  
   38            alert(result.status + ' ' + result.statusText);  
   39        }     
   40     </script> 
   41 </asp:Content>

.............

SimpleService.asmx.cs

.............



    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Web;
    5 using System.Web.Services;
    6 using System.Web.Services.Protocols;
    7
    8 namespace Ajax.Web.Public
    9 {
   10     /// <summary>
   11     /// Summary description for SimpleService
   12     /// </summary>
   13     [WebService(Namespace = "http://tempuri.org/")]
   14     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
   15     [System.ComponentModel.ToolboxItem(false)]
   16     // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
   17     [System.Web.Script.Services.ScriptService]
   18     public class SimpleService : System.Web.Services.WebService
   19     {
   28         [WebMethod]
   29         public string SayHelloJson(String Name, String Number)
   30         {
   31             return "Your Name=" + Name + " \nYour Number=" + Number;
   32         }
   33     }
   34 }


.............

Wednesday, June 24, 2009

call javascript from code behind

    8     <asp:UpdatePanel ID="UpdatePanel1" runat="server"
    9         UpdateMode="Conditional" >
   10         <ContentTemplate>
   12             <asp:Button ID="Button1" runat="server" Text="Button"
   13                 onclick="Button1_Click" />
   14         </ContentTemplate>
   15     </asp:UpdatePanel>

.................


   17         protected void Button1_Click(object sender, EventArgs e)
   18         {
   21             string script = "alert('Message')";
   22             ScriptManager.RegisterClientScriptBlock(UpdatePanel1,
   23                 typeof(UpdatePanel), "jscript", script, true);
   25         }
 
thanks to link

Tuesday, June 16, 2009

jquery ajax post

....

   93
   94 <div id="Result">Click here for current time</div>

....


   99     <script language="javascript">
  100
  101      //Set up the jQuery Char Counter for text area
  102         $(document).ready(function() {
  104             // Add the page method call as an onclick handler for the div.
  105             $("#Result").click(function() {
  106                 $.ajax({
  107                     type: "POST",
  108                     url: "Main.aspx/GetDate",
  109                     data: "{}",
  110                     contentType: "application/json; charset=utf-8",
  111                     dataType: "json",
  112                     success: function(msg) {
  113                         // Replace the div's content with the page method's return.
  114                         $("#Result").text(msg.d);
  115                     },
  116                     error: function() {
  117                         alert('error');
  118                     } 
  119                 }); 
  120             }); 
  121         });
  122      </script>

Main.aspx.cs ....


   30         [WebMethod]
   31         public static string GetDate()
   32         {
   33             return DateTime.Now.ToString();
   34         }



....

Monday, June 1, 2009

passing data in an ntier application

http://stackoverflow.com/questions/917457



How do you pass data to layers in an n-tier application? I have mapped out 3 different methods.

A) generic .net objects generic data tables, Hashtables, generic datasets, strings, ints etc... then using the datasets to fill your business objects which get sent to the UI layer.

alt text

http://dabbleboard.com/draw?b=eiu165&i=26&c=54eef6f1ac01f03c85919518f4a24e798e57e133

Pro- No extra layers needed Con- Have to work with Generic datasets and tables in the business layer

B) using an entities layer that the other layers would reference. This layer would contain either strongly typed datasets or Plain Old C Objects. The objects would be mostly container data and very little logic. these would be the same objects sent to the UI layer.

alt text

http://dabbleboard.com/draw?b=eiu165&i=6&c=d0c2b346894a96b12bd3867f630e474a2af098fa

Pro- working with the same classes in all layers Con- adding a reference to entities.dll to all layers

C) use data transfer objects (conatiner objects only) defined in the DataAccess Layer. then using those objects to fill business objects which get sent to the UI layer.

alt text

http://dabbleboard.com/draw?b=eiu165&i=27&c=f886efa3f9d5eb4b45ddb02361c79cdcdaec0a9b

Pro- the business layer would not have to work with generic classes Con- working with two types of objects and you would have to hydrate the business objects with the transfer objects

We had a discussion at work and wanted to see what the community thought. I also added a link to the dabbleboard. please copy and create instead of editing.
Thanks

Answer:http://stackoverflow.com/questions/917457

Wednesday, May 27, 2009

forms authentication

...........

   38     <authentication mode="Forms">
   39       <forms loginUrl="~/Admin/Login.aspx" defaultUrl="~/Admin/Login.aspx"  protection="All" timeout="60"> 
   40         <credentials passwordFormat="Clear">
   41           <user name="ff" password="xxxxxx"/>
   42           <user name="someguy" password="xxxxxx"/>
   43         </credentials>
   44       </forms>
   45     </authentication>
   46     <authorization>
   47       <allow users="?/>
   48       <allow users="*">
   49     </authorization>

...........


  118   <location path="Public">
  119     <system.web>
  120       <authorization>
  121         <allow users="?"/>
  122       </authorization>
  123     </system.web>
  124   </location>
  125   <location path="Content">
  126     <system.web>
  127       <authorization>
  128         <allow users="?"/>
  129       </authorization>
  130     </system.web>
  131   </location>
  132   <location path="Admin">
  133     <system.web>
  134       <authorization>
  135         <allow users="ff"/>
  136         <deny users="*"/>
  137         <!--<allow users="?"/>-->
  138       </authorization>
  139     </system.web>
  140   </location>
  141   <location path="Admin/Login.aspx">
  142     <system.web>
  143       <authorization>
  144         <allow users="?"/>
  145       </authorization>
  146     </system.web>
  147   </location>


...........


   17
   18     protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
   19     {
   20         if (FormsAuthentication.Authenticate(this.Login1.UserName, this.Login1.Password))
   21             FormsAuthentication.RedirectFromLoginPage(this.Login1.UserName, true); 
   22     }




...........




Thanks to this link

Friday, May 22, 2009

powershell rename recurse

get-Childitem C:\Root\Dev\Drycode\Source * -recurse | rename-item -newName { $_.Name -replace 'XTempNameX', 'DryCode' } -whatif

(Get-Content C:\Root\Dev\Drycode\Source\NNNNNNNNN.txt) |
Foreach-Object {$_ -replace "NNNNNNNNN", "@@@@@@@@@@@@@@@@"} |
Set-Content C:\Root\Dev\Drycode\Source\NNNNNNNNN.txt -whatif

NNNNNNNNN.txt
asNNNNNNNNNdfasdf
saNNNNNNNNNdf
v fsda
faNNNNNNNNNsd
wefwef
sdfasNNNNNNNNN
vdafdsafasfas
NNNNNNNNN
NNNNNNNNN.txt after
as@@@@@@@@@@@@@@@@dfasdf
sa@@@@@@@@@@@@@@@@df
v fsda
fa@@@@@@@@@@@@@@@@sd
wefwef
sdfas@@@@@@@@@@@@@@@@
vdafdsafasfas
@@@@@@@@@@@@@@@@



foreach ($file in get-Childitem -recurse)
{
$file.directoryName + " " +$file.name + " " +$file.length
}


get-Childitem *NNNN* | get-member -membertype property


helpful links
link1
link2
link3
link4
link5
link6

Monday, May 18, 2009

Imports and Master Type in an aspx

here are your import and MasterType tags ...

    5 <%@ MasterType TypeName="DryCode.Web.Master.Site" %>
    6
    7 <%@ Import Namespace="DryCode.Web.Util" %>    
    8 <%@ Import Namespace="DryCode.Util" %>    

jquery ajax load

here is the content page (Load.aspx) ...

    6 <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    7     <style>
    8         .otherPage{border:solid 1px black; background:#eee; margin:5px; padding:5px;}
    9     </style>
   10     <script language="javascript">
   11         $(document).ready(function() {
   12             LoadHtml();
   13         });
   14
   15         function LoadHtml() {
   16             $("#feeds").load("feeds.html");
   17         }
   18     </script>
   19 </asp:Content>
   20 <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
   21     <span>this is in load.aspx</span>
   22     <div id="feeds" class="otherPage" ></div>
   23 </asp:Content>



here is the feeds.html



    2 <html xmlns="http://www.w3.org/1999/xhtml" >
    3 <head>
    4     <title></title>
    5 </head>
    6 <body>
    7     <div >
    8         <i>this is from feeds.html</i>
    9         <b>45</b> feeds found.
   10     </div>
   11 </body>
   12 </html>



the master page ....


     
    8     <script src="http://www.google.com/jsapi"></script>   
    9     <script type="text/javascript">  
   10
   11         // Load jQuery
   12         google.load("jquery", "1.3.2");
   13         google.load("jqueryui", "1.7.1");
   14         google.load("yui", "2.7.0");
   15
   16         google.setOnLoadCallback(function() {  
   17             // Your code goes here.  
   18         });  
   19     </script> 



result (Load.aspx) ...


this is in load.aspx


this is from feeds.html
45 feeds found.



Friday, May 15, 2009

jquery output

  184         <div id="output"></div>
 

  201     <script language="javascript">
  202         $(document).ready(function() { 
  203             $('.disclaimer').click(function() { 
  204                 $("#output").text($("#output").text() + "******* ");
  205             });
  206         });
  207     </script>

   41 </body>
   42 <script src="http://www.google.com/jsapi"></script>   
   43 <script type="text/javascript">   
   44     // Load jQuery 
   45     google.load("jquery", "1.3.2"); 
   46     //google.load("jqueryui", "1.7.1"); 
   47     //google.load("yui", "2.7.0"); 
   48     google.setOnLoadCallback(function() {  
   49         // Your code goes here.   
   50     });   
   51 </script> 
   52 <asp:ContentPlaceHolder ID="javascript" runat="server">
   53 </asp:ContentPlaceHolder>
   54 </html>

Thursday, May 14, 2009

send an array as a paramater


   17 protected void StartDateTextBox_TextChanged(object sender, EventArgs e)
   18 {
   19     this.DateRangeChanged();
   20 } 
   21 protected void EndDateTextBox_TextChanged(object sender, EventArgs e)
   22 {
   23     this.DateRangeChanged();
   24 }
   25 private void DateRangeChanged()
   26 {
   27     this.LoadOrdersDataSet();
   28     ListControl[] la = { this.lbParentBroker, this.lbClient, this.lbAccounts };
   29     this.ResetRebindControls(la);
   30 }
   31 protected void lbParentBroker_SelectedIndexChanged(object sender, EventArgs e)
   32 {
   33     ListControl[] la = { this.lbClient, this.lbAccounts };
   34     this.ResetRebindControls(la);
   35 }
   36 protected void lbClient_SelectedIndexChanged(object sender, EventArgs e)
   37 {
   38     ListControl[] la = { this.lbAccounts };
   39     this.ResetRebindControls(la);
   40 }
   41 protected void lbAccounts_SelectedIndexChanged(object sender, EventArgs e)
   42 {
   43     ListControl[] la = { };
   44     this.ResetRebindControls(la);
   45 }
   46 private void ResetRebindControls(ListControl[] lca)
   47 {
   48     this.SetVisiblePanel(PanelType.None);
   49     foreach (ListControl lc in lca)
   50         lc.SelectedIndex = -1;
   51     this.gvOrders.DataBind();
   52     foreach (ListControl lc in lca)
   53         lc.DataBind();
   54 }

Wednesday, May 13, 2009

Linq Filter by adding Where Clause


  187         public IQueryable<Talent> GetTalents(string searchExpression)
  188         {
  189             IQueryable<Talent> t;
  190             if (string.IsNullOrEmpty(searchExpression))
  191             {
  192                 t = _repository.GetTalents();
  193             } 
  194             else
  195             {
  196                 t = BasicSearch(searchExpression);
  197             }
  198             return t;
  199         }
  200
  201         private static readonly char[] SplitDelimiters = " ".ToCharArray();
  202
  203         // search should come in as  'jon johnny323@yahoo.com rodgers'
  204         private IQueryable<Talent> BasicSearch(string search)
  205         {
  206             // Just replacing "  " with " " wouldn't help with "a      b"
  207             string[] terms = search.Trim()
  208                                    .ToLower()
  209                                    .Split(SplitDelimiters,
  210                                           StringSplitOptions.RemoveEmptyEntries);
  211             IQueryable<Talent> talents = _repository.GetTalents();
  212             foreach (string s in terms)
  213             {
  214                 talents = AddBasicSearch(talents, s);
  215             }
  216             return talents;
  217         }
  218
  219         private IQueryable<Talent> AddBasicSearch(IQueryable<Talent> t, string s)
  220         {
  221             return t.Where(x =>  x.FirstName.ToLower().Contains(s)
  222                     || x.LastName.ToLower().Contains(s) 
  223                     || x.Email.ToLower().Contains(s) 
  224                     || x.LanguagesString.ToLower().Contains(s) 
  225                     );
  226         }

Tuesday, May 12, 2009

jquery validation

 
  127 <asp:Content ID="Content3" ContentPlaceHolderID="javascript" runat="server"> 
  128     <script type="text/javascript">
  129         $.validator.addMethod("lettersonly", function(value, element) {
  130             return this.optional(element)  /^[a-z]+$/i.test(value);
  131         }, "Letters only please");
  132
  133         $(document).ready(function() {
  134             $('form').validate({
  135                 errorLabelContainer: "#errorMessageBox"
  136                 , wrapper: "li"
  137                 , errorClass: "invalid"
  138                 //, submitHandler: function() { alert("Submitted!") }
  139             })
  140             $('#<%=FirstName.ClientID %>').rules("add", {
  141                 minlength: 2
  142                 , required: true
  143                 , lettersonly : true
  144                 , messages: {
  145                     minlength: jQuery.format("At least {0} characters are required for the first name")
  146                     , required: "First name is required"
  147                 }
  148             });
  149             $('#<%=LastName.ClientID %>').rules("add", {
  150                 minlength: 2
  151                 , required: true
  152                 , lettersonly: true
  153                 , messages: {
  154                     minlength: jQuery.format("At least {0} characters are required for the last name")
  155                     , required: "last name is required"
  156                 }
  157             });
  158         });
  159     </script>
  160 </asp:Content>


   19                 <div>
   20                     <ul id="errorMessageBox"></ul>
   21                 </div>
   22                 <div class="clear"></div>
   23                 <label>
   24                     <asp:Label ID="lblFirstName" runat="server"
   25                         Text="<%$ Resources:Resource,LabelFirstName%>"></asp:Label>
   26                 </label>
   27                 <asp:TextBox ID="FirstName" runat="server" class="normal" MaxLength="50"></asp:TextBox><br />
   28                 <label>
   29                     <asp:Label ID="lblLastName" runat="server"
   30                         Text="<%$ Resources:Resource,LabelLastName%>"></asp:Label>
   31                 </label>
   32                 <asp:TextBox ID="LastName" runat="server" class="normal" MaxLength="50"></asp:TextBox><br />


here is the css ...   


   95 /*validation part*/
   96 label.invalid { float: none; text-align: left; width: 100%; color:#ee3333; font-weight:normal; padding: 3px 0px; }
   97 input.invalid { background-color:#FFeeee; color:#ee3333; }
   98 select.invalid { background-color:#FFeeee; border:3px double #ff9999; color:#ee3333; }
   99 #errorMessageBox { margin-bottom: 15px;  }
  100 #errorMessageBox li { margin: 0px 40px 0px 40px; padding: 5px 20px; color:#ee3333; background: #FFeeee;
101 text-transform: none; list-style-type: disc; list-style-position: inside;}
  102 #errorMessageBox label { text-transform: none; }

Contributors