Tag: ASP.NET

  • Substitute values in ASP.NET

    It is very easy to present data on a web page using Visual Studio. I want to give a suggestion on how to present a substitute for a value. For this example, I use Visual Studio 2005 and SQL Server 2005.  

    Imagine a database (TestDatabase) with a table. One of the columns in the table is a Short (smallint in SQL Server) called Answer. If the value of that column is 0, you want to present the value “No answer”, if the value is 1, you want to present the value “Yes”. Finally, if the value is 2 you want to present the value “No”.

    Let’s start with the problem. To display data from the table, I could create a data source on a web page, and connect it to a GridView control. Perhaps it’s even more easy to just drag a GridView control to the web page, and use the wizard to create a data source. The wizard can be accessed by selecting “New data source” as a the datasource in the task pane of the GridView control. After completing the wizard, the GridView now shows the content of the underlying table. It shows the actual values of the Answer column, that is 0, 1 or 2 instead of “No answer”, “Yes” or “No”.

    This is one solution for the problem: Templates!

    To convert the Answer column of the GridView to a TemplateField, open the task pane for the GridView control again. It looks different now when the control is connected to a data source. Click Edit Columns to bring up the Fields dialog, in the Selected fields list, highlight the Answer column and click the link labeled “Convert this field into a TemplateField”.

    The HTML code for the field has now changed from

    <asp:BoundField DataField=”Answer” HeaderText=”Answer” SortExpression=”Answer” />

    …to…

    <asp:TemplateField HeaderText=”Answer” SortExpression=”Answer”>
    <EditItemTemplate>
    <asp:TextBox ID=”TextBox1″ runat=”server” Text='<%# Bind(“Answer”) %>’></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID=”Label1″ runat=”server” Text='<%# Bind(“Answer”) %>’></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>

    …and it can no longer be edited using the Fields dialog. How ever, you can now replace the label in the <ItemTemplate> tag with any control you like, a custom one if you like. Now, all you have to do, is to assign the value of the field to the desired property of a custom web control that you have created. And that control can write any replacement value to the result stream depending on the value of the property that is assigned to the control.

    This might be the HTML code in <ItemTemplate> tag:

    <mySpace:MyControl ID=”Label1″ runat=”server” MyProperty='<%# Bind(“Answer”) %>’></mySpace:MyControl>

  • Session state in .NET 3.5

     This new feature saves some time, but for me, it took a while to figure it out. Or to be honest, after a while, I gave up and asked my personal guru Roger. This is the thing:In web services, to preserve session state (that is, being able to read and write session variables across multiple calls) you have to have to declare that the web method uses session state by setting that parameter in the WebMethod attribute. This could be a web method in a web service that reads and/or writes session variables: 

    <WebMethod(BufferResponse:=False, enableSession:=True)> _
    Public Sub SendMails()
    …
     

    In .NET 2.0, the client had code that instantiated an object representing the service, assigned a CookieContaner, and then things just worked. My problem (because of sloppy and careless reading on .NET 3.5) was that I didn’t find a property in that object to assign the CookieContainer to. This is how it’s done in .NET 3.5:

    Open the config file of the client (the web service consumer). Locate the binding tag, and change the attribute allowCookies from false to true. That’s it! The feature just works! Thanks!

    <binding name=”apiSoap” closeTimeout=”00:01:00″ openTimeout=”00:01:00″ receiveTimeout=”00:10:00″ sendTimeout=”00:01:00″ allowCookies=”true”…