2012-04-06 11 views
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()); 
     } 
    } 

, wenn Benutzer auf dieser Seite klicken Sie auf die nächste Seite alsasp.net url Generation

www.mypage erzeugt wird/Entersession.aspx? Session = session_id

aber ich würde eher, es haben wie

www.mypage/Entersession.aspx? session = session_name

beide Sitzungs_ID und session_name sind aus der Datenbank

keine Idee?

} 
+0

Sie möchten also Ihr 'CommandArgument' ändern? – jrummell

Antwort

1

Gerade

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

zu

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

Ich habe versucht, immer noch habe ich die ID an die URL – buni

1

Ist das wonach Sie suchen? Ändern CommandArgument zu benennen? Es ist eine sehr, sehr einfache Lösung.

aktualisieren

Sie könnten einen commandArgument Parameter GetButton() hinzuzufügen.

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()) 
+0

Dies führt dazu, dass "Join Session" immer das CommandArgument ist. – Khan

+0

Ja, wird es. Ich habe meine Antwort aktualisiert. – jrummell

+0

Kein Überladen für die Methode get button dauert 2 Argumente. das, was ich als Fehler bekommen habe – buni

1

Die Änderung, die Sie suchen findet in dem Sie verwenden Ihre GetButton Methode

Wechsel:

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

An:

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

Ihr erster Eingabeparameter GetButton wird die CommandArgument abgebildet wird. So wird es passieren, session_name in statt session_id zu passieren.

+0

Ich habe versucht, noch habe ich die ID an die URL angeschlossen – buni

+0

Hat Ihr Etikett den Sitzungsname korrekt angezeigt? – Khan

+0

ja es zeigt – buni