Ken Carter Ken Carter - 3 months ago 8
ASP.NET (C#) Question

Setting nested control values in a asp:formview from C# code behind

I was approaching this from the angle of controlling the asp:SqlDataSource bound to the controls inside the formview that is part of this topic and was getting no where rather fast, so I opt'd to close that thread and approach this at the control level inside the formview instead as it seemed to make a lot more sense to me.

So for back ground The code below is intended to inject string values listed in the first two lines of the screen clip below (and the two corresponding Response.Write's at the bottom of the included code block).
enter image description here

What I am trying to do, is use FindControl to set the Text value in the case of these items (but I have a few asp:checkbox's to deal with after getting this done as well that will need the same treatment.) and for now I'm just focusing on these two controls. The first is a asp:label

<asp:Label runat="server" ID="SubmitByLbl" Text='<%# Eval("SubmitBy") %>' ></asp:Label>


The next is a asp:TextBox:

<asp:TextBox Text='<%# Bind("SubmitDT") %>' runat="server" ID="SubmitDTTextBox" />


Both of these are sitting inside of the asp:FormView with an ID="AddItemFv" which has a DataSourceID="AddInvAsset" (which is irrelevant for this discussion but just provided as supplemental info).

Here is the code behind where my problem is:

namespace CInTrac
{
public partial class AddAsset : System.Web.UI.Page
{
protected void Page_Preload(object sender,EventArgs e)
{

AddInvAsset.SelectParameters.Add("SubmitBy", Session["RegUser"].ToString());
AddInvAsset.SelectParameters.Add("SubmitDT", DateTime.Now.ToString("MM/dd/yyyy"));
}
protected void Page_Load(object sender, EventArgs e)
{

Label uname = (Label)AddItemFv.Row.FindControl("SubmitBy");
if (uname != null)
uname.Text = Session["RegUser"].ToString();

TextBox udate = (TextBox)AddItemFv.Row.FindControl("SubmitDT");
if (udate != null)
udate.Text = DateTime.Now.ToString();

Response.Write("<b>Submitted by should be:</b> = " + Session["RegUser"].ToString() + "<br>");
Response.Write("<b>Submit Date should be:</b> = " + DateTime.Now.ToString("MM/dd/yyyy") + "<br>");
}

}

}


The problem that I'm having is Just what you see in the example above; absolutely nothing! I'm new to C# so I am likely doing something wrong with my code here and in debug my trace indicates that as we check for null it then fails to assign value to .Text of each VAR and therefore our values never show up.

Update:
As IM pointed out I had missed the proper name of the controls in the code. Below are the corrections I made to the code above which works fine. Again THanks to Inquisitive_mind for pointing out my error. :

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

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

Answer

The ID of the controls is incorrect

Change

Label uname = (Label)AddItemFv.Row.FindControl("SubmitBy");
TextBox udate = (TextBox)AddItemFv.Row.FindControl("SubmitDT");

To

Label uname = (Label)AddItemFv.Row.FindControl("SubmitByLbl");
TextBox udate = (TextBox)AddItemFv.Row.FindControl("SubmitDTTextBox");

EDIT

You will have to move the FindControl to a FormView event.The controls will only be accessible after the FormView has been completely created and databound.Try moving the code from page_load to YourFormView_Databound event.

protected void YourFormView_DataBound(Object sender, EventArgs e)
{
    Label uname = (Label)AddItemFv.Row.FindControl("SubmitByLbl");
        if (uname != null)
            uname.Text = Session["RegUser"].ToString();

        TextBox udate = (TextBox)AddItemFv.Row.FindControl("SubmitDTTextBox");
        if (udate != null)
            udate.Text = DateTime.Now.ToString();
}
Comments