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

探している変更は、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 に答える
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

これはあなたが探しているものですか?改名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 に答える