Home All Groups Group Topic Archive Search About

Default values in a FormView

Author
21 Feb 2006 11:37 AM
Gary Homewood
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

Author
22 Feb 2006 6:24 AM
Phillip Williams
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;
        }
    }
Show quoteHide quote
"Gary Homewood" wrote:

> 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
>
Are all your drivers up to date? click for free checkup

Author
22 Feb 2006 9:39 AM
Gary Homewood
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.


Show quoteHide quote
"Phillip Williams" wrote:

> 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" wrote:
>
> > 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
> >

Bookmark and Share