2016-10-03 38 views
0

我對代碼中的這個問題感到困惑,並希望有人看到我在這裏失蹤的東西。這個工作的最上面部分是完美的,但是在下面的兩個其他類似字段上執行的相同基本代碼未能將值注入到文本框中,即使我已經刪除了所有可能的事情,我可以想到這可能會抑制功能加工。將值填入asp:控件

我沒有收到任何類型的錯誤,當我調試它時,數值按預期分配。就像我所期望的那樣,文本框中沒有顯示任何內容。

protected void Page_Load(object sender, EventArgs e) 
{ 
    <%-- This part works fine --%> 
    TextBox uname = (TextBox)AddItemFv.Row.FindControl("SubmitByTbx"); 
    if (uname != null) 
     uname.Text = Session["RegUser"].ToString(); 

    TextBox udate = (TextBox)AddItemFv.Row.FindControl("SubmitDTTbx"); 
    if (udate != null) 
     udate.Text = DateTime.Now.ToString("MM/dd/yyyy"); 

    <%-- This part Fails to stuff the values into the TextBoxes --%> 
    TextBox uuname = (TextBox)AddItemFv.Row.FindControl("AssetEnteredByTextBox"); 
    if (uname != null) 
     uname.Text = Session["RegUser"].ToString(); 

    TextBox uudate = (TextBox)AddItemFv.Row.FindControl("AssetEnteredTextBox"); 
    if (udate != null) 
     udate.Text = DateTime.Now.ToString("MM/dd/yyyy"); 
} 

任何人都認爲這將是有益的將不勝感激。在過去的幾個小時裏,我一直在看它,並沒有去哪裏。基於響應


最終的解決方案提供如下所示:

protected void Page_Load(object sender, EventArgs e) 
{ 
    TextBox uname = (TextBox)AddItemFv.Row.FindControl("SubmitByTbx"); 
    if (uname != null) 
     uname.Text = Session["RegUser"].ToString(); 

    TextBox udate = (TextBox)AddItemFv.Row.FindControl("SubmitDTTbx"); 
    if (udate != null) 
     udate.Text = DateTime.Now.ToString("MM/dd/yyyy"); 

    TextBox uuname = (TextBox)AddItemFv.Row.FindControl("AssetEnteredByTextBox"); 
    if (uuname != null) 
     uuname.Text = Session["RegUser"].ToString(); 

    TextBox uudate = (TextBox)AddItemFv.Row.FindControl("AssetEnteredTextBox"); 
     if (uudate != null) 
     uudate.Text = DateTime.Now.ToString("MM/dd/yyyy"); 
} 
+0

什麼是你的.aspx文件看起來像2的ID名稱不工作可以顯示標記 – MethodMan

回答

4

你有一個錯字? uuname vs uname和uudate vs udate?

TextBox **uuname** = (TextBox)AddItemFv.Row.FindControl("AssetEnteredByTextBox"); 
    if (**uname** != null) 
     **uname**.Text = Session["RegUser"].ToString(); 

    TextBox **uudate** = (TextBox)AddItemFv.Row.FindControl("AssetEnteredTextBox"); 
    if (**udate** != null) 
     **udate**.Text = DateTime.Now.ToString("MM/dd/yyyy"); 
+0

謝謝謝謝謝謝!我正在重新使用前面的代碼,並且只是添加了一個額外的u並忽略了將它添加到每個子句中的其他兩個位置! 我想到另一組眼睛就會看到我錯過了什麼! –

+0

顯然,如果不使用縮寫名稱,它不會有這樣的問題。那麼,在剪切和粘貼後看到問題會更容易。 – Phil1970

0

固定代碼。這應該教授關於複製和粘貼的課程。錯過一些東西很容易。

protected void Page_Load(object sender, EventArgs e) 
{ 
<%-- This part works fine --%> 
TextBox uname = (TextBox)AddItemFv.Row.FindControl("SubmitByTbx"); 
if (uname != null) 
    uname.Text = Session["RegUser"].ToString(); 

TextBox udate = (TextBox)AddItemFv.Row.FindControl("SubmitDTTbx"); 
if (udate != null) 
    udate.Text = DateTime.Now.ToString("MM/dd/yyyy"); 

<%-- This part Fails to stuff the values into the TextBoxes --%> 
TextBox uuname = (TextBox)AddItemFv.Row.FindControl("AssetEnteredByTextBox"); 
if (uname != null) 
    // correct from uname to uuname 
    uuname.Text = Session["RegUser"].ToString(); 

TextBox uudate = (TextBox)AddItemFv.Row.FindControl("AssetEnteredTextBox"); 
if (udate != null) 
    //Corrected from udate to uudate 
    uudate.Text = DateTime.Now.ToString("MM/dd/yyyy"); 
}