在客户端添加的访问列表框项。

时间:2021-08-21 07:23:14
  1. I have a listbox with runat=server
    1. Items are added to this listbox on the client side, using javascript
    2. 使用javascript将项添加到客户端列表框中
    3. I would want to retrieve the items on the server side on the click of a button
    4. 我希望在单击按钮时在服务器端检索条目。
  2. 我有一个列表框,其中的runat=server项被添加到客户端这个列表框中,我希望使用javascript在单击按钮时检索服务器端项

The problem is in the Buttons's server side click handler, I cannot see the new items added to the listbox. Only the items that were there on page_load are displayed. How do i accomplish what i want to do

问题是在按钮的服务器端单击处理程序中,我看不到添加到列表框的新项。只显示page_load上的项。我如何完成我想做的事

Edit 1

My Code Is like this

我的代码是这样的

 protected void Page_Load(object sender, EventArgs e)
    {


        if (!IsPostBack)
        {
            if (ViewState["gameID"] == null)
            {
                //Populate Listbox
                //Set gameid in viewstate
            }

            //Add javascript handlers to buttons
            btnSomeButton.Attributes.Add("onclick", "aJavaScriptFunction"); 

        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        ListItemCollection x = ListBoxRanks.Items;
        //Here items is has just those items that are added to the listbox on page load

    }

3 个解决方案

#1


4  

Ah when abstractions leek :)

当抽象出现时

Web server controls are serialised to view state before the response is sent, the control is recreated on postback and the options all put back from view state.

在发送响应之前,Web服务器控件被序列化为视图状态,在回发时重新创建控件,所有选项都从视图状态放回。

When you add option items client side they are not added to viewstate. The only way is to use your own hidden field to serialise client side additions and read them on postback or ajax the additions serverside.

当您添加选项项客户端时,它们不会被添加到viewstate中。唯一的方法是使用您自己的隐藏字段来串行化客户端添加,并在postback或ajax上读取添加的服务器端。

#2


0  

Only bind to the ListBox if Page.IsPostBack is false. This will allow you to see any items added on the client side.

只绑定到列表框。IsPostBack是错误的。这将允许您查看在客户端添加的任何项目。

If you are binding to the control on each load you are wiping out any existing items that were loaded by ASP.NET from the request. By only binding if the current page load was not triggered by a postback you are allowing all the items from the request to load, including any items added client-side.

如果您在每个负载上绑定到控件,那么您将删除由ASP加载的任何现有项目。净从请求。只有当当前页负载不是由回发触发时,才进行绑定,这样就允许从请求中加载所有项,包括客户端添加的任何项。

#3


0  

You should use ajax to add items to the dropdown list. It will ensure your viewstate is in sync with the serverside dropdown control.

您应该使用ajax将项添加到下拉列表中。它将确保您的viewstate与serverside下拉控件保持同步。

#1


4  

Ah when abstractions leek :)

当抽象出现时

Web server controls are serialised to view state before the response is sent, the control is recreated on postback and the options all put back from view state.

在发送响应之前,Web服务器控件被序列化为视图状态,在回发时重新创建控件,所有选项都从视图状态放回。

When you add option items client side they are not added to viewstate. The only way is to use your own hidden field to serialise client side additions and read them on postback or ajax the additions serverside.

当您添加选项项客户端时,它们不会被添加到viewstate中。唯一的方法是使用您自己的隐藏字段来串行化客户端添加,并在postback或ajax上读取添加的服务器端。

#2


0  

Only bind to the ListBox if Page.IsPostBack is false. This will allow you to see any items added on the client side.

只绑定到列表框。IsPostBack是错误的。这将允许您查看在客户端添加的任何项目。

If you are binding to the control on each load you are wiping out any existing items that were loaded by ASP.NET from the request. By only binding if the current page load was not triggered by a postback you are allowing all the items from the request to load, including any items added client-side.

如果您在每个负载上绑定到控件,那么您将删除由ASP加载的任何现有项目。净从请求。只有当当前页负载不是由回发触发时,才进行绑定,这样就允许从请求中加载所有项,包括客户端添加的任何项。

#3


0  

You should use ajax to add items to the dropdown list. It will ensure your viewstate is in sync with the serverside dropdown control.

您应该使用ajax将项添加到下拉列表中。它将确保您的viewstate与serverside下拉控件保持同步。