- 8 Week Game
- Quick Ref
Archive for category VB.net
Great snippet to wack in the web.config file, which allows you to run c# and vb.net code (*spit* ) in the same web project;
<system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=22.214.171.124, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4"> <providerOption name="CompilerVersion" value="v3.5" /> <providerOption name="WarnAsError" value="false" /> </compiler> <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=126.96.36.199, Culture=neutral, PublicKeyToken=b77a5c561934e089" warningLevel="4"> <providerOption name="CompilerVersion" value="v3.5" /> <providerOption name="OptionInfer" value="true" /> <providerOption name="WarnAsError" value="false" /> </compiler> </compilers> </system.codedom>
Going from dev to live, where the dev system referenced dev version of web services, but the live system has to reference live versions which are HTTPS i recieved this error;
The provided URI scheme ‘http’ is invalid; expected ‘https
This is resolved simply by updating the web.config file and setting the security tag’s mode attribute from None to Transport;
<bindings> <wsHttpBinding> <binding name="WSHttpBinding_IWSHttpService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" /> <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" /> <security mode="Transport"> <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> <message clientCredentialType="None" negotiateServiceCredential="true" algorithmSuite="Default" establishSecurityContext="true" /> </security> </binding> </wsHttpBinding> </bindings>
Quick example of the current culture switch in the web.config of a dot net project.
<globalization fileEncoding=”utf-8″ requestEncoding=”utf-8″ responseEncoding=”utf-8″ culture=”en-GB” uiCulture=”en-GB”/>
just found this little nugget over on DotNetSlackers.com - By default dot net will activly stop you from maintaining the value in password text boxes on post back- which can be a pain in the ass forcing your user to keep retyping their password (twice as you will almost certainly have a password confirm box!). You can get round this using this beauty;
Protected Sub txtMyPasswordField_PreRender( ByVal sender As Object, ByVal e As System.EventArgs) Handles txtMyPasswordField.PreRender txtMyPasswordField.Attributes("value") = txtMyPasswordField.Text End Sub
In visual studio 2008 you'll notice a lot of operators such as foreach/ switch etc etc are listed as snippets in the intellisense menu, however when you hit tab to insert them it just autocompletes the keyword- if you hit tab twice it will insert the full snippet, giving you a dummy template usage for that keyword!
Just found a cool tool online for converting vb code to c#, and vice-versa. It's not 100% perfect so make sure you have a look through what it turfs out, but it's handy for converting small code snippets of example functions you find online. It's up on the DeveloperFusion website here.
The install was easy- but a couple of bits did catch me out- the code sample above was the example they give you in the install page. So I blindly copied over this code to the head of my blog, and copied all the files from the zip file over to my sites js and css folders. I found that html highlighting worked fine and so did c# but vb and sql weren’t happening… It eventually clicked that you need to add the references to the additional syntax types you need as the example only has a line for the CS and XML/HTML “brush” libraries. So I popped those lines in and it worked fine.
The other problem that I had, and indeed still have until I get home to actually edit the code, is compatibility with TinyMCE which is the editor used by default in BlogEngine. I found a couple of articles which might help with this but won’t know till tonight;
- TinyMCE Code highlighter Plugin to work with Google Syntaxt Highlighter
- TinyMCE configuration settings to help everything get along a little better
As mentioned on the Wiki pages there is also the issue that adding a name value to a pre tag, isn’t valid xhtml.
Using FindControl to find a repeater nested inside a repeater with HeaderTemplate or FooterTemplate defined
I just today cracked a problem which consumed my Friday afternoon and a big chunk of this morning. This was something i hadn’t come across in .net when using repeaters before.
On the aspx side I had a repeater embedded within a repeater;
<asp:Repeater ID="rptAddedSeries" runat="server"> <headerTemplate> <div id="seriesHeader"> </headerTemplate> <itemTemplate> <h2> <%#Container.DataItem().Title%> </h2> <asp:Repeater ID="rptBooks" runat="server"> <headerTemplate><ul></headerTemplate> <itemTemplate> <li> <%#Container.DataItem().BookID%> </li> </itemTemplate> <footerTemplate> </ul> </footerTemplate> </asp:Repeater> </itemTemplate> <footerTemplate> </div> </footerTemplate> </asp:Repeater>
I simply wanted to populate the inner repeater and perform the databinding. The code i had worked fine- using FindControl to grab the inner repeater then setting the datasource and executeing DataBind(). However, when i added the HeaderTemplate and FooterTemplate to the parent “rptAddedSeries” control, the FindControl suddenly stopp working and i started getting a NullReferenceException when trying to set my DataSource.
Protected Sub rptAddedSeries_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)Handles rptAddedSeries.ItemDataBound Dim this_repeater_item As RepeaterItem = DirectCast(sender, Repeater).Controls.Item(_repeater_counter) Dim child_repeater As Repeater = DirectCast(this_repeater_item.FindControl("rptBooks"), Repeater) child_repeater.DataSource = _highlight.Highlights_Seriess(_repeater_counter).Highlight_Series_Books child_repeater.DataBind() _repeater_counter += 1 End Sub
After a bit of googling I found a forum post on asp.net forums by someone with a similar issue. The ItemDataBound event occurs against each part of the your repeater item- meaning if you have a HeaderTemplate and FooterTemplate as well as your standard ItemTemplate, the ItemDataBound will execute 3 times. So I added a condition at the start of the function to ensure that i was running my code only during the ItemTemplate data binding;
If e.Item.ItemType = ListItemType.Item Then ... End If
However i was still having issue’s with the FindControl failing. Reading down the same forum post i found a note mentioning that FindControl wouldn’t work directly against the RepeaterItem but there was nothing to say whatI should be running it against. Considering the it was the e (RepeaterItemEventArgs) object we tested to find the current ItemType, it stood to reason that the actual item might be contained within there so i modified the method and it worked perfectly;
Protected Sub rptAddedSeries_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptAddedSeries.ItemDataBound If e.Item.ItemType = ListItemType.Item Then 'Dim this_repeater_item As RepeaterItem = DirectCast(sender, Repeater).Controls.Item(_repeater_counter) 'Dim child_repeater As Repeater = DirectCast(this_repeater_item.FindControl("rptBooks"), Repeater) 'Dim child_repeater As Repeater = DirectCast(this_repeater_item.TemplateControl.FindControl("rptBooks"), Repeater) Dim child_repeater As Repeater = DirectCast(e.Item.FindControl("rptBooks"), Repeater) child_repeater.DataSource = _highlight.Highlights_Seriess(_repeater_counter).Highlight_Series_Books child_repeater.DataBind() _repeater_counter += 1 End If End Sub
Hope this save’s someone else a few hours of tearing their hair out!