2011-06-09 55 views
0

如何從DetailsView中將選定的值獲取到文本框?到目前爲止,我使用這個TextBox.Text = DetailsView1.SelectedValue.String();但是返回一個錯誤:對象引用未設置爲對象的實例。我可以在OnDataBound和ItemInserted在formview中完成,沒有問題,但是這次我想從detailsview中將選定的值粘貼到Page_Load事件的文本框中。從DetailsVIew獲取選定的值到文本框

幫助將不勝感激。提前致謝!

附後相同的代碼,代碼的其餘部分從我以前的帖子:

using System; 
using System.Data; 
using System.Data.SqlClient; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace LibraryManagementSystemC4.User 
{ 
    public partial class Reserving : System.Web.UI.Page 
    { 
     public string GetConnectionString() 
     { 
      return System.Configuration.ConfigurationManager.ConnectionStrings["LibrarySystemConnectionString"].ConnectionString; 
     } 
     //string reservationid 
     private void ExecuteInsert(string bookid, string EmployeeID, string reservedate) 
     { 
      SqlConnection conn = new SqlConnection(GetConnectionString()); 

      string sql = "INSERT INTO BookReservation (bookid, EmployeeID, reservedate) VALUES " + " (@bookid, @EmployeeID, @reservedate)"; 
      try 
      { 
       conn.Open(); 
       SqlCommand cmd = new SqlCommand(sql, conn); 
       SqlParameter[] param = new SqlParameter[3]; 

       //param[0] = new SqlParameter("@reeservationid", SqlDbType.Int, 50); 
       param[0] = new SqlParameter("@bookid", SqlDbType.BigInt, 50); 
       param[1] = new SqlParameter("@EmployeeID", SqlDbType.NVarChar, 50); 
       param[2] = new SqlParameter("@reservedate", SqlDbType.DateTime, 10); 

       //param[0].Value = reservationid; 
       param[0].Value = bookid; 
       param[1].Value = EmployeeID; 
       param[2].Value = reservedate; 

       for (int i = 0; i < param.Length; i++) 
       { 
        cmd.Parameters.Add(param[i]); 
       } 

       cmd.CommandType = CommandType.Text; 
       cmd.ExecuteNonQuery(); 
      } 

      catch (System.Data.SqlClient.SqlException ex) 
      { 
       string msg = "Insert error"; 
       msg += ex.Message; 
       throw new Exception(msg); 
      } 

      finally 
      { 
       conn.Close(); 
      } 
     } 

     protected void Button1_Click(object sender, EventArgs e) 
     { 
      if (bookidTextBox != null) 
      { 
       ExecuteInsert(bookidTextBox.Text, EmployeeIDTextBox.Text, reservedateTextBox.Value); 

       ClearControls(Page); 
      } 

      else 
      { 
       Response.Write("Please input ISBN"); 
       bookidTextBox.Focus(); 
      } 

     } 

     protected void Page_Load(object sender, EventArgs e) 
     { 
      { 

       bookidTextBox.Text = DetailsView1.SelectedValue.ToString(); 
       EmployeeIDTextBox.Text = HttpContext.Current.User.Identity.Name.ToString(); 
      } 
     } 

     public static void ClearControls(Control Parent) 
     { 
      if(Parent is TextBox) 
      { 
       (Parent as TextBox).Text = string.Empty; 
      } 

      else 
      { 
       foreach (Control c in Parent.Controls) 
       ClearControls(c); 
      } 
     } 
    } 
} 

回答

2

DetailsView1.SelectedValue.String()將拋出一個空引用異常,如果的SelectedValue爲空,即不存在選擇的值,這在一個範圍內我認爲DetailsView意味着它不包含任何數據。你想要做的:

if (DetailsView1.SelectedValue != null) 
{ 
    MyTextbox.Text = DetailsView1.SelectedValue.ToSTring(); 
} 
+0

謝謝!但它返回空白,因此將其轉換爲數據綁定。再次謝謝你!! – Loupi 2011-06-09 07:55:06