Using EditItemTemplate for InsertItemTemplate in FormView (ASP.NET)
There is a nice way to reuse your hardly made EditItemTemplate in the InsertItemtemplate. Found in ASP.NET forum:
Ref: http://forums.asp.net/t/1102469.aspx
protected void FormView1_Init(object sender, EventArgs e) {
FormView1.InsertItemTemplate = FormView1.EditItemTemplate
}
protected void FormView1_DataBound(object sender, EventArgs e) {
if (FormView1.CurrentMode == FormViewMode.Edit) {
LinkButton InsertButton = FormView1.FindControl("InsertButton") as LinkButton;
LinkButton UpdateButton = FormView1.FindControl("UpdateButton") as LinkButton;
InsertButton.Visible = false;
UpdateButton.Visible = true;
} else if (FormView1.CurrentMode == FormViewMode.Insert) {
LinkButton InsertButton = FormView1.FindControl("InsertButton") as LinkButton;
LinkButton UpdateButton = FormView1.FindControl("UpdateButton") as LinkButton;
InsertButton.Visible = true;
UpdateButton.Visible = false;
}
}
Ref: http://forums.asp.net/t/1102469.aspx
Can I use same template for ItemTemplate too?
ReplyDeleteI think so. Did you tried it?
ReplyDeleteI just discovered this yesterday by looking at the FormView implementation in Reflector. If you don't specify an InsertItemTemplate (ie. it is null) and your FormView is in InsertMode it will actually use the EditItemTemplate (You can see this for yourself by looking at the implementation for FormView.CreateDataRow).
ReplyDeleteIt seems that the TemplateField also does the same thing, so it appears to be a common pattern. However, I've not not yet come across any documentation that says anything about this behaviour (maybe I'm not looking hard enough).
But it means you'd still have to handle the command buttons as you're doing in FormView1_DataBound I believe. I've yet to fully test this solution yet.
That`s what I call solution! Thank`s man! you save my a.. :)
ReplyDelete