开发者

how to display database information (evals? labels? literals?)

I have the following asp.net page

<form id="form1" runat="server">

<asp:Button id="display_button" runat="server" Text="Display" OnClick="Button1_Click" /> &nbsp;
<asp:Button id="edit_button" runat="server" Text="Edit" OnClick="Button2_Click" /> &nbsp; 
<asp:Button id="save_button" runat="server" Text="Save" OnClick="Button3_Click" Visible="false" /> &nbsp; 
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
<br />

<asp:MultiView id="MultiView1" runat="server" ActiveViewIndex="0">
    <asp:View id="View1" runat="server">

        <asp:FormView id="view_program" runat="server"> 
            <ItemTemplate>

                 <tr>
                    <td class="add_border_bold" nowrap">Status</td>
                    <td width="100%" class="add_border">
                        <img src="images/<%# Eval("status").ToString().Trim() %>_light_16.gif" alt="status" />
                        &nbsp;
                    </td>
                </tr>

                 <tr>
                    <td class="add_border_bold" nowrap">Short Title</td>
                    <td width="100%" class="add_border">
                        <%# Eval("short_title") %>
                        &nbsp;
                    </td>
                </tr>

            </ItemTemplate>
        </asp:FormView>

    </asp:View>

    <asp:View id="View2" runat="server">
        <asp:FormView id="edit_program" runat="server"> 

            <ItemTemplate>
                 <tr>
                    <td class="add_border_bold"nowrap">Status </td>
                    <td width="100%" class="add_border">
                        <asp:DropDownList id="p_status" runat="server"></asp:DropDownList>
                    </td>
                </tr>

                 <tr>
                    <td class="add_border_bold" nowrap">Short Title</td>
                    <td width="100%" class="add_border">
                        <asp:TextBox runat="server" id="short_title" /> 
                    </td>
                </tr>

            </ItemTemplate>
        </asp:FormView>

    </asp:View>
</form>

with the following code behind page

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;

namespace TM_non_deploy
{
    public partial class Program : System.Web.UI.Page
    {
        protected Label Label1;
        protected Person myPerson;
        protected TestProgram myProgram;

        List<TestProgram> program = null;

        protected void Page_Load(object sender开发者_运维知识库, EventArgs e)
        {

            try
            {
                myPerson = new Person("user");
                myProgram = new TestProgram("999");

                //needs to be done to refresh info on page
                program = new List<TestProgram> { myProgram };
                view_program.DataSource = program;
                view_program.DataBind();


                if (!IsPostBack)
                {
                    //create controls and bind data
                    edit_program.DataSource = program;
                    edit_program.DataBind();

                    DropDownList p_status = edit_program.FindControl("p_status") as DropDownList;
                    p_status.Items.Add(new ListItem("Green"));
                    p_status.Items.Add(new ListItem("Yellow"));
                    p_status.Items.Add(new ListItem("Red"));
                    p_status.SelectedValue = myProgram.Status.Trim();

                    TextBox short_title = edit_program.FindControl("short_title") as TextBox;
                    short_title.Width = 200;
                    short_title.Text = myProgram.Short_Title.Trim();

                }

            }
            catch (Exception ex)
            {
                Response.Write(ex);
                Label1.Text = ex.ToString();
            }

        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            MultiView1.SetActiveView(View1);
            save_button.Visible = false;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            MultiView1.SetActiveView(View2);
            save_button.Visible = true;
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            DropDownList c_status = edit_program.FindControl("p_status") as DropDownList;

            myProgram.Status = c_status.SelectedValue;

            bool update = myProgram.SaveTestProgram();
            if (update)
            {
                Label1.Text = "Saved!";

                //needs to be done to refresh info on page
                program = new List<TestProgram> { myProgram };
                view_program.DataSource = program;
                view_program.DataBind();

                MultiView1.SetActiveView(View1);
                save_button.Visible = false;
            }
            else
            {
                Label1.Text = "Error Saving";
            }
        }

    }
}

basically, it is one page that both displays the fields, and then on a button click displays the editable version of all those fields. my question is, should i be displaying all of the information like i am now, with evals? or should i switch to labels, or literals, or something else entirely? i want to know before i get too far and have to undo a lot of work.

there will end up being a ton of fields on this page, all types from checkboxes to dropdowns to multiline textboxes, so i want to make sure i pick the path that works best for displaying all of those different kinds of data, even though in this example i am only displaying small text information.


If you just need to display text, there's no reason not to just use your <%# Eval("title") %>

One thing that should be noted though, is that it's better to cast your DataItem and access the properties that way instead of Eval as Eval has to use reflection and is more costly (maybe to the being of actually being noticeable if you have a lot of stuff repeated).

Use: <%# (Container.DataItem as SomeObject).Title %> instead of <%# Eval("Title") %>

Kind of outside the scope of your question but just a side note. :)


I wouldn't go for ServerSide Controls (Labels/Literals) unless you need formatting or to change the control values in other server side events. They just add to ViewState (unless ViewState is disabled on those Controls).

Coming to whether it's the right approach, I would suggest you use EditItemTemplate of FormView instead of 2 FormViews in Different Views & a MultiView Control!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜