Adding User input to List then populating Gridview

谁都会走 提交于 2019-12-12 06:37:22

问题


I have an 3 tier application (DAL, BBL, UI) BBL at the moment do Nothing just a pass-thru

I have a grid view and for simplicity's sake one text box(TB) and one drop down list(DDL). and Two submit buttons.

(I changed my Custom Class to Object. just for this example)

First Submit button adds the TB.text & DDL.SelectedValue to a Object X in the UI.

the BBL takes that object X to adds it to a List(X) in the BBL.

Then the BBL should populate the Gridview with the List(X). (with ajax partial page load)

the second Submit should send the full List(X) to the database.

The problem im having is when I click the first Submit(the local) I dont get new Rows just keep over writing the same row. what am I Missing?

in the UI class

   private businesslogic blogic = new businesslogic();

   protected void B1_local_Click(object sender, EventArgs e)
    {

        object x = new object();
        x.id = Convert.ToInt32(TB_1.Text);
        x.var1 = Convert.ToInt32(DDL_1.SelectedValue);

        blogic.addrowtolist(x);

        Gridview1.DataSource = blogic.grablist();
        Gridview1.Databind();

     }

in the BBL class

    public List<object> locallist = new List<object>();
    public void addrowtolist(object x)
    {
       locallist.Add(x);

    }
    public List<object> grablist()
    {
     return locallist;
    }

回答1:


With every postback, you're loading a new BL with a new (empty) List. To see your List grow, you're going to need to save it somewhere that persists (doesn't disappear) between one request and the next.

I would recommend putting your List in a Session key

Session["items"] = blogic.locallist;

and then pulling it out and sending it to a second BL constructor on postback. This is probably simplest, but not always the correct approach.

http://msdn.microsoft.com/en-us/library/z1hkazw7.aspx



来源:https://stackoverflow.com/questions/7983763/adding-user-input-to-list-then-populating-gridview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!