|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Default values in a FormViewWhere 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; } } 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 > 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 > >
Other interesting topics
Web.config session timeout
ASP.NET 2.0 SmartNavigation vs MaintainScrollPositionOnPostback? sorting xml data in server control problem dynamically removing items from a droplist..? Register script block from app_code How to Comment an WebControl Type.GetType() daab and mysql App Directory Problem with ASP.NET Development Server |
|||||||||||||||||||||||