使用AJAX更新列表框时选择列表框项目导致错误(asp.net)

时间:2012-04-26 08:40:31

标签: asp.net jquery

我有一个列表框,其中包含我的网站我的应用程序的登录用户。我希望列出的框能异步更新为1)停止整页刷新和2)停止在页面加载时调用其他内容。我最初使用内置的AJAX控件(更新面板和计时器),他们工作,但每当我从列表框中选择一个项目(登录用户)(想法是用户将从列表框中选择另一个用户,一个按钮会出现,他们可以开始游戏)我会得到这个错误:

'E:\ITERATION 4\PROJECT\PROJECT\APP_DATA\DATABASE.MDF.dbo.User'; column does not allow nulls. INSERT fails. The statement has been terminated.'

如果我转到列表框的属性并将AutoPostBack变为false,我可以选择一个用户,但该按钮不会出现。

讲师告诉我写一个AJAX脚本并使用web方法更新列表框,所以我做了:

[WebMethod()]
    public static string[] getCurrentList()
    {
        int count = 0;
        for (int i = 0; i < Global.ListUsers.Count(); i++)
            count++;

        string[] results = new string[count];

        for (int i = 0; i < count; i++)
            results[i] = Global.ListUsers[i].Username.ToString();

        return results;
    }

脚本:

$(function updateListbox() {
var lBox = $('select[id$=ListBox1]');
setInterval(function () {
    $.ajax({
        beforeSend: function (req) {
            req.setRequestHeader("Accept", "application/json");
        },
        type: "POST",
        url: "Homepage.aspx/getCurrentList",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            var results = data.d;
            if (results.length > 0) {
                var updatedList = [];
                for (var i = 0; i < results.length; i++) {
                    lBox[0].options[i] = new Option(results[i], results[i], true, false)
                }
                $(lBox).append(updatedList.join(''));
            }
        }
    });
}, 5000);

});

播放按钮代码:

protected void btnPlay_Click(object sender, EventArgs e)
    {
        // When the "Play" button is clicked the following occurs:
        // The selected player's ID is parsed and passed to 'getUserByID' method
        // That user will be returned and added to the 'AddPlayerToGame' method in the Global.asax file
        // The user who is currently on the homepage will be passed to the same method

        string ID1 = ListBox1.SelectedItem.Value;
        int ID = Convert.ToInt32(ID1);  
        User u2 = uDAO.getUserByID(ID);

        Global.AddPlayerToGame(u);
        Global.AddPlayerToGame(u2);

        g.Player1ID = u.UserID;
        g.Player2ID = u2.UserID;
        gDAO.AddPlayers(g);


        u2.InvitedBy = u.Username;
        uDAO.InsertOrUpdateObject(u2);

        // script to open gamepage window from server-side. currently being executed on client-side

        /*Response.Write("<Script>");
        Response.Write("window.open('Gamepage.aspx', '_blank', 'height=750, width=1300')");
        Response.Write("</Script");*/
    }

现在这个工作,列表框异步更新,但是当我选择一个用户时,我得到上面的错误,当我把AutoPostBack设为false时,我看不到按钮。

任何人都知道解决方法吗?遇到了类似的问题?

0 个答案:

没有答案