Archive for the 'c#' Category

March 21
asp.net ajax postback on keyup
posted by mike at 9:30 pm

so I got a question from some one via the cmap group, for some help with a kind of tricky asp.net ajax problem. So they had been using a pagemethod, to retrieve a list from a web service to update a datalist. Sounds simple enough... but the problem w/ using web services from client code in asp.net ajax is that the page method, which although it exists in the codebehind of a page, and used to be accessable from the instance of the page in atlas, now has to be static... anywho.... so the problem is if you call a webservice in client js, its hard to do things like bind a datalist...

so my solution/kludge was to add the postback js call of a control, to the onkeyup js event of the textbox. Basicly forcing the textbox to do a asp.net postback on a keyup (really this should be only posted back like every 3 key ups...)

heres the aspx

CODE:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="onkeyuppostback.aspx.cs" Inherits="onkeyuppostback" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head runat="server">
  7.     <title>Untitled Page</title>
  8. </head>
  9. <body>
  10.     <form id="form1" runat="server">
  11.     <div>
  12.     <asp:ScriptManager runat="server" ID="smTest" EnablePartialRendering="true"></asp:ScriptManager>
  13.         <asp:TextBox runat="server" ID="txtTest" AutoPostBack="True" OnTextChanged="txtTest_TextChanged"></asp:TextBox>
  14.     <asp:UpdatePanel runat="server" ID="upTest">
  15.         <ContentTemplate>
  16.             &nbsp;
  17.         <asp:DataList runat="server" ID="dlTest" Width="207px">
  18.             <ItemTemplate>
  19.                 <%# Container.DataItem %>
  20.             </ItemTemplate>
  21.         </asp:DataList>     
  22.         </ContentTemplate>
  23.         <Triggers>
  24.             <asp:AsyncPostBackTrigger ControlID="txtTest" />
  25.         </Triggers>
  26.     </asp:UpdatePanel>
  27.     </div>
  28.     </form>
  29. </body>
  30. </html>

and here is the cs

C#:
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11.  
  12. public partial class onkeyuppostback : System.Web.UI.Page
  13. {
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.        txtTest.Attributes.Add("onkeyup", this.GetPostBackClientEvent(this.txtTest,string.Empty));
  17.     }
  18.  
  19.     protected void txtTest_TextChanged(object sender, EventArgs e)
  20.     {
  21.         dlTest.DataSource = System.DateTime.Now.ToString();
  22.         dlTest.DataBind();
  23.     }
  24. }

so yah kind of kludgy, but is an option in general for doing something in js, which will force a asp.net ajax updatepanel postback... for example, add a button somewhere, have it w/ a style display:none;, then on serverside create a js function that calls its postback function.

 
March 21
cfdump how i miss you some days
posted by mike at 12:20 pm

so when I was doing lots of cf on a daily basis, I would long for strongly typed data and a real ide w/ real integrated debugging tools... now doing .net dev day in day out , I have those things, and surely appreciate it... but... I miss cfdump. You needed to see in a page what a variable was at any given point, there you had it.... I had a problem the yesterday where a problem only existed on a production server. No nice debugger / ide on a production server, just little old notepad, and me. So I whipped up some reflection code, heres a snippet

C#:
  1. protected void PrintProperties(object o,System.Type[] allowable)
  2. {
  3.  
  4.    System.Type t = o.GetType();
  5.    System.Reflection.PropertyInfo[] properties_info = t.GetProperties();
  6.  
  7.    for (int y = 0; y <properties_info.Length; y++)
  8.   {
  9.       Response.Write(properties_info[y].Name + ":");
  10.      try
  11.     {
  12.         if (properties_info[y].GetValue(o, null) != null)
  13.             Response.Write(properties_info[y].GetValue(o, null).ToString());
  14.  
  15.        if ((!properties_info[y].GetValue(o, null).GetType().Equals(typeof(String)) ||!(properties_info[y].GetValue(o, null).GetType() is System.ValueType)) && (Array.IndexOf(allowable, properties_info[y].GetValue(o, null).GetType())> -1))
  16.     PrintComplexObject(o, properties_info[y], allowable);
  17.  
  18.  
  19.    }
  20.    catch
  21.   {
  22.    .......
  23.  
  24.      }
  25.  
  26.   }
  27.  
  28. }

So I know, cf no "real" data types, makes the writting of such stuff much easier, exctly why var_dump exists for php, but man, I wish I had something like that.... which stard me thinking I need an Ndump tag.... throw it on a page, and it dumps a variable / complex object in a session etc... I started working on some basic functionality for it today.... Have a Basic Library written, when I get time ill clean it up, put it in a custom tag and post it on up... emphasis on when I get TIME, what is this TIME I speak of.

UPDATE: so i spent some time today and have a working prototype of an Ndump tag/control if any one wants it just ask...

 
March 19
east coast time
posted by mike at 1:26 pm

so had to do something interesting for work that I thought would be usefull for others... 2 servers on each coast... need to write the same time, but code does not know which server its running on... so I use gmt to figure out the right time....

heres the solution in sql and c#... sql is useful , as that you could set the default of a datetime field in sql to dbo.fn_GetEastCoastTime(getdate().getutc())

SQL:
  1. CREATE FUNCTION [dbo].[fn_GetEastCoastTime]
  2. (@date datetime,@utcdate datetime
  3. )
  4. RETURNS datetime
  5. AS
  6. BEGIN
  7.  
  8. declare  @march datetime
  9. declare  @november datetime
  10. declare  @dstStart datetime
  11. declare  @dstEnd datetime
  12. declare  @difamt int
  13.  
  14. SET @march= '3/01/'+cast(year(@date)AS varchar)
  15. SET @november= '11/01/'+cast(year(@date)AS varchar)
  16.  
  17. SET @dstStart = DATEADD(wk,1,(@march)+(7-DATEPART(dw,(@march-
  18. Day(@march)))))
  19.  
  20. SET @dstEnd = DATEADD(wk,0,(@november)+(7-DATEPART(dw,(@november-
  21. Day(@november)))))
  22.  
  23. IF(@date> @dstStart AND @date <@dstEnd)
  24. SET @difamt = -4
  25. else
  26. SET @difamt = -5
  27.  
  28. RETURN dateadd(hour,datediff(hour,dateadd(hour,@difamt,@utcdate),@date),@date)
  29.  
  30. END

C#:
  1. public static DateTime EastCoastDate
  2.  
  3. {
  4.  
  5. get
  6.  
  7. {
  8.  
  9. int intTimeShift =-5;
  10.  
  11. if(TimeZone.CurrentTimeZone.IsDaylightSavingTime(System.DateTime.Now))
  12.  
  13. intTimeShift = -4;
  14.  
  15. TimeSpan hourdiff =  System.DateTime.UtcNow.AddHours(intTimeShift) - System.DateTime.Now;
  16.  
  17. return System.DateTime.Now.AddHours(hourdiff.Hours);
  18.  
  19. }
  20.  
  21. }