View Single Post
  #9 (permalink)  
Old September 11th, 2007, 04:58 PM
aboundweb aboundweb is offline
Registered User
Join Date: Sep 2007
Location: West Springfield, Massachusetts, USA.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts

Hey there,

This is Joe from Abound Inc. I spent some time on this issue and found the simple solution. You simply need to create a template field for the field you want to set a default value for. In the template field ASP.Net is very flexible in that you can put any type of control in there whether it be a label, text box, checkbox etc... You set the value for that template field by making a call to a method you create in the code-behind file. I have a page called Clients.aspx where I have a gridview and detailsview. In this page I set it up so that when the user clicks to add a new client, the COMPANY_ID field is set to session variable and cannot be changed by the user.

In the Clients.aspx file the code for the template field looks like this:

<asp:TemplateField HeaderText="COMPANY_ID" SortExpression="COMPANY_ID">
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("COMPANY_ID") %>'></asp:Label>
                    <asp:Label ID="Label2" runat="server" Text='<%# GetCompanyID() %>'></asp:Label>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("COMPANY_ID") %>'></asp:Label>

Notice the call to GetCompanyID() in the InsertItemTemplate. That method is defined in the code-behind file (Clients.aspx.cs):

protected String GetCompanyID()
        String sCompany = "";
        if (Session["company_id"] != null)
            sCompany = (string)Session["company_id"];
            sCompany = "0";
        return sCompany;

I hope this helps

Joe Werner
Abound Inc.

Reply With Quote