在asp.net-c中的网页之间传递值(通过列表框)#

时间:2012-05-01 10:16:33

标签: c# javascript asp.net stringbuilder

〜/ Admin / AdimHome.aspx.cs C#代码

protected void Page_Load(object sender, EventArgs e)
 {
     System.Text.StringBuilder sb = new System.Text.StringBuilder();
     sb.Append("<script language='javascript'>function Open() {");
     sb.Append(string.Format("window.open('Chat.aspx?rid={0}'", lstRooms.SelectedValue));
     sb.Append(", 'newwindow','toolbar=no,location=no,menubar=no,width=290,height=330,resizable=no,scrollbars=no,top=350,left=980,right=500');return false;");
     sb.Append("}</script>");

     if (!ClientScript.IsClientScriptBlockRegistered("JSScriptBlock"))
     {
         ClientScript.RegisterClientScriptBlock(this.GetType(), "JSScriptBlock", sb.ToString());
     }

    lblFacultyNo.Text = Session["User_Id"].ToString();
    lblUserType.Text = Session["User_Type"].ToString();

    pnlChat.Visible = false;   
}

〜/ Admin / Chat.aspx.cs页面 C#代码

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["User_Id"] == null)
            Response.Redirect("~/Admin/AdimHome.aspx");
        if (string.IsNullOrEmpty(Request.QueryString["rid"]))
            Response.Redirect("~/Admin/AdminHome.aspx");

        txtMsg.Attributes.Add("onkeypress", "return clickButton(event,'btn')");

        if (!IsPostBack)
        {
            hdnRoomID.Value = Request.QueryString["rid"];
            ChatRoom room = ChatEngine.GetRoom(hdnRoomID.Value);
            string prevMsgs = room.JoinRoom(Session["User_Id"].ToString(), Session["User_Id"].ToString());
            txt.Text = prevMsgs;
            foreach (string s in room.GetRoomUsersNames())
            {
                lstMembers.Items.Add(new ListItem(s, s));
            }

        }
    }

希望将 lstRooms.SelectedValue 传递给 Chat.aspx.cs页面,以根据客户端请求检查以区分聊天室:

sb.Append(string.Format("window.open('Chat.aspx?rid={0}'", lstRooms.SelectedValue));

点击btnChat事件:

<asp:Button ID="btnChat" Runat="server" CssClass="btn" OnClientClick="JavaScript:Open()" OnClick="btnChat_Click" Text="Join Room" />

1 个答案:

答案 0 :(得分:0)

如果您想更改代码,问题的简单解决方案就是......

//Javascript function
function Open()
{
   var ddl = document.getElementbyId('<%= lstRooms.ClientID%>');
   var ddlvalue = ddl.options[ddl.selectedIndex].value;
   Window.Open("Chat.aspx?rid=" + ddlvalue );
}

在pageload中删除JS的所有代码并将其放在aspx页面上。 如果它解决了,请告诉我