2016-10-03 39 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"); 
}