Default values in a FormView

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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;
}
}
 
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
 

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

Similar Threads


Back
Top