This is really starting to get to me. Basically I want the users to be able to choose between a LOV (dropdownlist) and freeform (textbox) editing in DetailsView. I couldn't get buttons with a method to work right so I'm using hyperlinks with querystrings. It's not pretty, but it does the job. Here's my DetailsView:
<asp:DetailsView OnDataBound="DV2_DB" ID="DetailsView2" runat="server" AllowPaging="True" AutoGenerateRows="False"
CellPadding="4" DataKeyNames="DBAS_APPL_I" DataSourceID="ObjectDataSource2" ForeColor="#333333"
GridLines="None" Height="50px" Width="275px">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#E2DED6" Font-Bold="True" Width="200px" />
<EditRowStyle BackColor="#999999" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<PagerStyle BackColor="#990000" ForeColor="White" HorizontalAlign="Center" />
<Fields>
<asp:TemplateField HeaderText="Application ID" SortExpression="DBAS_APPL_I">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("DBAS_APPL_I") %>'></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("DBAS_APPL_I") %>'></asp:Label>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("DBAS_APPL_I") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Application Name" SortExpression="APPL_N">
<EditItemTemplate>
<asp:DropDownList width="150px" ID="DropDownList1" runat="server" SelectedValue='<%# Eval("APPL_N") %>' DataSourceID="SqlDataSource1" DataTextField="APPL_N" DataValueField="APPL_N"></asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server" Visible="false" Text='<%# Eval("APPL_N") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList width="150px" ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="APPL_N" DataValueField="APPL_N"></asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server" Visible="false"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("APPL_N") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="BPO" SortExpression="BPO_N">
<EditItemTemplate>
<asp:DropDownList width="150px" ID="DropDownList2" runat="server" SelectedValue='<%# Eval("BPO_N") %>' DataSourceID="SqlDataSource2" DataTextField="BPO_N" DataValueField="BPO_N"></asp:DropDownList>
<asp:TextBox ID="TextBox2" runat="server" Visible="false" Text='<%# Eval("BPO_N") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList width="150px" ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2" DataTextField="BPO_N" DataValueField="BPO_N"></asp:DropDownList>
<asp:TextBox ID="TextBox2" runat="server" Visible="false"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("BPO_N") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Director" SortExpression="DIR_N">
<EditItemTemplate>
<asp:DropDownList width="150px" ID="DropDownList3" runat="server" DataSourceID="SqlDataSource3" SelectedValue='<%# Eval("DIR_N") %>' DataTextField="DIR_N" DataValueField="DIR_N"></asp:DropDownList>
<asp:TextBox ID="TextBox3" runat="server" Visible="false" Text='<%# Eval("DIR_N") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList width="150px" ID="DropDownList3" runat="server" DataSourceID="SqlDataSource3" DataTextField="DIR_N" DataValueField="DIR_N"></asp:DropDownList>
<asp:TextBox ID="TextBox3" runat="server" Visible="false"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("DIR_N") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="VP" SortExpression="VP_N">
<EditItemTemplate>
<asp:DropDownList width="150px" ID="DropDownList4" runat="server" SelectedValue='<%# Eval("VP_N") %>' DataSourceID="SqlDataSource4" DataTextField="VP_N" DataValueField="VP_N"></asp:DropDownList>
<asp:TextBox ID="TextBox4" runat="server" Visible="false" Text='<%# Eval("VP_N") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList width="150px" ID="DropDownList4" runat="server" DataSourceID="SqlDataSource4" DataTextField="VP_N" DataValueField="VP_N"></asp:DropDownList>
<asp:TextBox ID="TextBox4" runat="server" Visible="false" Text='<%# Eval("VP_N") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("VP_N") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemStyle BackColor="#E2DED6" />
<EditItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="True" CommandName="Update"
Text="Update"></asp:Button>
<asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:Button><br />
</EditItemTemplate>
<InsertItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="True" CommandName="Insert"
Text="Insert"></asp:Button>
<asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:Button><br />
</InsertItemTemplate>
<ItemTemplate>
<asp:HyperLink ID="HyperLink3" runat="server" NavigateUrl='<%# "Details.aspx?id=" + Request.QueryString["ID"] + "&editmode=loved" %>'>Edit (List of Values)</asp:HyperLink> |
<asp:HyperLink ID="HyperLink4" runat="server" NavigateUrl='<%# "Details.aspx?id=" + Request.QueryString["ID"] + "&editmode=lovnew" %>'>New (List of Values)</asp:HyperLink>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "Details.aspx?id=" + Request.QueryString["ID"] + "&editmode=ffed" %>'>Edit (Freeform)</asp:HyperLink> |
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# "Details.aspx?id=" + Request.QueryString["ID"] + "&editmode=ffnew" %>'>New (Freeform)</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Fields>
<FieldHeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:DetailsView>
So as you can see I've got the two controls in each field. I basically am using the hyperlinks to select which field should be visible. Here's my codebehind:
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 string editmode = String.Empty;
4 string dbid = Request.QueryString["id"];
5 editmode = Request.QueryString["editmode"];
6
7 // Find our textboxes and dropdownlists
8 if (!IsPostBack)
9 {
10 if (editmode != null)
11 {
12 if (editmode == "loved" || editmode == "ffed")
13 {
14 DetailsView2.ChangeMode(DetailsViewMode.Edit);
15 }
16 else
17 {
18 DetailsView2.ChangeMode(DetailsViewMode.Insert);
19 }
20 DropDownList ddl1 = (DropDownList)DetailsView2.Rows[1].Controls[1].FindControl("DropDownList1");
21 DropDownList ddl2 = (DropDownList)DetailsView2.Rows[2].Controls[1].FindControl("DropDownList2");
22 DropDownList ddl3 = (DropDownList)DetailsView2.Rows[3].Controls[1].FindControl("DropDownList3");
23 DropDownList ddl4 = (DropDownList)DetailsView2.Rows[4].Controls[1].FindControl("DropDownList4");
24 TextBox txt1 = (TextBox)DetailsView2.Rows[1].Controls[1].FindControl("TextBox1");
25 TextBox txt2 = (TextBox)DetailsView2.Rows[2].Controls[1].FindControl("TextBox2");
26 TextBox txt3 = (TextBox)DetailsView2.Rows[3].Controls[1].FindControl("TextBox3");
27 TextBox txt4 = (TextBox)DetailsView2.Rows[4].Controls[1].FindControl("TextBox4");
28
29 // if it's LOV we want, we STILL need to rebind
30 // the update and insert parameters because they
31 // may have already clicked the other one.
32 switch (editmode)
33 {
34 case "loved":
35 DetailsView2.ChangeMode(DetailsViewMode.Edit);
36
37 ddl1.Visible = true;
38 ddl2.Visible = true;
39 ddl3.Visible = true;
40 ddl4.Visible = true;
41 txt1.Visible = false;
42 txt2.Visible = false;
43 txt3.Visible = false;
44 txt4.Visible = false;
45 break;
46 case "lovnew":
47 DetailsView2.ChangeMode(DetailsViewMode.Insert);
48 ddl1.Visible = true;
49 ddl2.Visible = true;
50 ddl3.Visible = true;
51 ddl4.Visible = true;
52 txt1.Visible = false;
53 txt2.Visible = false;
54 txt3.Visible = false;
55 txt4.Visible = false;
56 break;
57 case "ffed":
58 DetailsView2.ChangeMode(DetailsViewMode.Edit);
59 ddl1.Visible = false;
60 ddl2.Visible = false;
61 ddl3.Visible = false;
62 ddl4.Visible = false;
63 txt1.Visible = true;
64 txt2.Visible = true;
65 txt3.Visible = true;
66 txt4.Visible = true;
67 break;
68 case "ffnew":
69 DetailsView2.ChangeMode(DetailsViewMode.Insert);
70 ddl1.Visible = false;
71 ddl2.Visible = false;
72 ddl3.Visible = false;
73 ddl4.Visible = false;
74 txt1.Visible = true;
75 txt2.Visible = true;
76 txt3.Visible = true;
77 txt4.Visible = true;
78 break;
79
80 }
81 }
82 }
Now I know that eventually I'll have to rebind the update or insert parameters based on which control we're using - but I'm not too worried about that part.. Here are the results I'm getting:
I can successfully switch to Editing via dropdownlists or textboxes, i can successfully switch to Inserting via dropdownlists, but for some reason when I try to insert via textbox it always brings up the dropdowns. I don't know if there's something different about the InsertTemplate that causes it to be drawn differently or what, but I can't figure this out for the life of me.