Default values in a FormView

G

Guest

Where can I specify a default value for a column when using a FormView?
Specifically, I have a checkbox for a column of bit datatype on my database,
and I'd like to default it to checked for insert of a new row.
Thanks,
Gary
 
G

Guest

Handle the ItemCreated event of the FormView and set the checkbox.checked to
true if the CurrentMode is Insert, e.g.
void FormView1_ItemCreated(object sender, EventArgs e)
{
if (FormView1.CurrentMode == FormViewMode.Insert)
{
CheckBox chk =
(CheckBox)FormView1.Row.FindControl("chkDiscontinued");
if (chk != null)
{
chk.Checked = true;
}
}
}

Another alternative is to remove the Bind method from the checkbox.Checked
property and set it to True within the InsertTemplate, and then upon
ItemInserting you would set the value as decided by the user, e.g.

void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
{
CheckBox chk = (CheckBox)FormView1.Row.FindControl("chkDiscontinued");
if (chk != null)
{
e.Values["Discontinued"] = chk.Checked;
}
}
 
G

Guest

That's great, that's the method I eventually discovered and which seemed to
do the job. I was just hoping there would be some way for me to set the
default values declaratively, i.e. not having to resort to code.


Phillip Williams said:
Handle the ItemCreated event of the FormView and set the checkbox.checked to
true if the CurrentMode is Insert, e.g.
void FormView1_ItemCreated(object sender, EventArgs e)
{
if (FormView1.CurrentMode == FormViewMode.Insert)
{
CheckBox chk =
(CheckBox)FormView1.Row.FindControl("chkDiscontinued");
if (chk != null)
{
chk.Checked = true;
}
}
}

Another alternative is to remove the Bind method from the checkbox.Checked
property and set it to True within the InsertTemplate, and then upon
ItemInserting you would set the value as decided by the user, e.g.

void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
{
CheckBox chk = (CheckBox)FormView1.Row.FindControl("chkDiscontinued");
if (chk != null)
{
e.Values["Discontinued"] = chk.Checked;
}
}
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Gary Homewood said:
Where can I specify a default value for a column when using a FormView?
Specifically, I have a checkbox for a column of bit datatype on my database,
and I'd like to default it to checked for insert of a new row.
Thanks,
Gary
 
Joined
Aug 9, 2006
Messages
1
Reaction score
0
Can someone translate this solution into VB.Net please

I am fairly new to .NET development. I programmed a very, very, very long time ago.

I need to implement a solution that allows me to programmatically set a default value for a column inside an ASP FormView control during (or just before) the SQL INSERT Occurs.

The solution is given in C#. Please help.
 
Joined
Sep 11, 2007
Messages
1
Reaction score
0
Setting default value in formview, detailsview, gridview

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:

Code:
<asp:TemplateField HeaderText="COMPANY_ID" SortExpression="COMPANY_ID">
                <EditItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("COMPANY_ID") %>'></asp:Label>
                </EditItemTemplate>
                <InsertItemTemplate>
                    &nbsp;
                    <asp:Label ID="Label2" runat="server" Text='<%# GetCompanyID() %>'></asp:Label>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("COMPANY_ID") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

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

Code:
protected String GetCompanyID()
    {
        String sCompany = "";
        if (Session["company_id"] != null)
        {
            sCompany = (string)Session["company_id"];
        }
        else
        {
            sCompany = "0";
        }
        return sCompany;
    }

I hope this helps

Joe Werner
Abound Inc.
www.aboundweb.com
 
Joined
Mar 7, 2008
Messages
1
Reaction score
0
Joe,

I don't believe your solution works on its own because your CustomerID taken from the session is not bound.

Although I'd like it if it did! ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,179
Messages
2,570,956
Members
47,509
Latest member
Jack116

Latest Threads

Top