0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
using System.Web.Services;
using System.IO;

namespace T_Smade
{

    public partial class ConferenceManagement : System.Web.UI.Page
    {
        volatile int i = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            GetSessionList();
        }


        public void GetSessionList()
        {
            string secondResult = "";
            string userName = "";

            try
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    userName = HttpContext.Current.User.Identity.Name;
                }

                SqlConnection thisConnection = new SqlConnection(@"data Source=ZOLA-PC;AttachDbFilename=D:\2\5.Devp\my DB\ASPNETDB.MDF;Integrated Security=True");
                thisConnection.Open();

                SqlCommand secondCommand = thisConnection.CreateCommand();
                secondCommand.CommandText = "SELECT myApp_Session.session_id FROM myApp_Session, myApp_Role_in_Session where myApp_Role_in_Session.user_name='" + userName + "' and myApp_Role_in_Session.session_id=myApp_Session.session_id";
                SqlDataReader secondReader = secondCommand.ExecuteReader();

                while (secondReader.Read())
                {
                    secondResult = secondResult + secondReader["session_id"].ToString() + ";";
                }
                secondReader.Close();

                SqlCommand thisCommand = thisConnection.CreateCommand();
                thisCommand.CommandText = "SELECT * FROM myApp_Session;";
                SqlDataReader thisReader = thisCommand.ExecuteReader();

                while (thisReader.Read())
                {
                    test.Controls.Add(GetLabel(thisReader["session_id"].ToString(), thisReader["session_name"].ToString()));
                    string[] compare = secondResult.Split(';');
                    foreach (string word in compare)
                    {
                        if (word == thisReader["session_id"].ToString())
                        {
                            test.Controls.Add(GetButton(thisReader["session_id"].ToString(), "Join Session"));
                        }
                    }
                }
                thisReader.Close();
                thisConnection.Close();

            }
            catch (SqlException ex)
            {

            }
        }

        private Button GetButton(string id, string name)
        {
            Button b = new Button();
            b.Text = name;
            b.ID = "Button_" + id + i;
            b.Command += new CommandEventHandler(Button_Click);
            b.CommandArgument = id;
            i++;
            return b;
        }

        private Label GetLabel(string id, string name)
        {
            Label tb = new Label();
            tb.Text = name;
            tb.ID = id;
            return tb;
        }

        protected void Button_Click(object sender, CommandEventArgs e)
        {
            Response.Redirect("EnterSession.aspx?session=" + e.CommandArgument.ToString());
        }
    }

当用户从该页面单击时,下一页将生成为

www.mypage/Entersession.aspx?session=session_id

但我宁愿拥有它

www.mypage/Entersession.aspx?session=session_name

session_id 和 session_name 都来自数据库

任何的想法?

}
4

3 回答 3

1

这是你想要的?改名CommandArgument?这是一个非常非常容易解决的问题。

更新

您可以添加一个commandArgument参数到GetButton().

    private Button GetButton(string id, string name, string commandArgument)
    {
        Button b = new Button();
        b.Text = name;
        b.ID = "Button_" + id + i;
        b.Command += new CommandEventHandler(Button_Click);
        b.CommandArgument = commandArgument; // this changed to commandArgument
        i++;
        return b;
    }

    GetButton(thisReader["session_id"].ToString(), "Join Session", thisReader["session_name"].ToString())
于 2012-04-06T16:58:36.450 回答
1

只是改变

test.Controls.Add(GetButton(thisReader["session_id"].ToString(), "Join Session"));

test.Controls.Add(GetButton(thisReader["session_name"].ToString(), "Join Session"));
于 2012-04-06T17:00:13.050 回答
1

您正在寻找的改变发生在您使用GetButton方法的地方

改变:

test.Controls.Add(GetButton(thisReader["session_id"].ToString(), "Join Session"));

到:

test.Controls.Add(GetButton(thisReader["session_name"].ToString(), "Join Session"));

您的第一个输入参数GetButton被映射到CommandArgument. 因此,通过session_name而不是session_id会成功。

于 2012-04-06T17:03:05.047 回答