iT邦幫忙

0

DetailsView 新增一筆資料,自動帶入流水號的問題

請問一個問題
我在DetailsView內按下新增
要讓其中一個欄位(id)
自動帶入流水號,SQL語法已寫好

SQL語法
.
.
.
SqlDataReader myDataReader = myCommand.ExecuteReader();

while (myDataReader.Read())
{
  if (myDataReader["NewID"].ToString() != "")
  {
    TextBox TB = (TextBox)DetailsView1.FindControl("TextBox5");
    TextBox5.Text = (myDataReader[0].ToString());
  }

可正常帶入DetailsView以外的TEXTBOX1 (TEXTBOX1不再DetailsView內)
但是就是無法帶入DetailsView的TEXTBOX5內 (TEXTBOX5是 DetailsView id樣板)
TextBox5.Text會顯示找不到這個位置
請問如何修正才能找到TextBox5
謝謝

以上是寫在DetailsView1_ItemCreated內
也有試過寫在其他事件內,一樣找不到TextBox5

自問自答一下

protected void Button1_Init(object sender, EventArgs e)
{
  string strConn = "server=EIP;database=STOCK_MGMT;User ID=sa;Password=XXXXXXX;Trusted_Connection=True;";
  SqlConnection myConn = new SqlConnection(strConn); //建立連接
  myConn.Open(); //打開連接
  String strSQL =
  @"
    Declare @ClassNum Varchar(12), @NewNum0 Varchar(5), @NewNum Varchar(5), @NewClassNum Varchar(12)
    Set @ClassNum = (select max(Asset_Class_Num) from Asset_Class )
    Set @NewNum0 = Replicate('0',5-Len(Cast(Cast(SubString(@ClassNum,2,5) As Int) + 1 As Varchar(12)))) --第2個開始取5位
    Set @NewNum = Cast(Cast(SubString(@ClassNum,2,5) As Int) + 1 As Varchar(12))
    Set @NewClassNum =   @NewNum0 + @NewNum
    Select 'C' + @NewClassNum as NewClassNum
  ";
  SqlCommand myCommand = new SqlCommand(strSQL, myConn); //建立SQL命令對象
  SqlDataReader myDataReader = myCommand.ExecuteReader();  //得到Data結果集

  //讀取結果
  while (myDataReader.Read())
  {
    if (myDataReader["NewClassNum"].ToString() != "")
    {
       TextBox TB = (TextBox)DetailsView1.FindControl("TextBox2");
       //Response.Write(myDataReader[0].ToString());
       TB.Text = (myDataReader[0].ToString());
    }
  }
  myConn.Close();
}

尚未有邦友回答

立即登入回答