Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I set default field values in a FormView with Cookie?

Status
Not open for further replies.

pbb72

Programmer
Mar 27, 2004
38
NO
Hi all,

I've got a FormView control with a few empty TextBoxes, among others one for name and email. Is it possible to fill these in with values retrieved from Cookies? All I get is "Object reference not set to an instance of an object".

Code:
protected void  FormView1_DataBound(object sender, System.EventArgs e)
{
	DataRowView row = (DataRowView)FormView1.DataItem;
	if (row["Reply"] == DBNull.Value)
	{
		FormView1.ChangeMode(FormViewMode.Edit);
		Trace.Write("changemode");
		// Error: Object reference not set to an instance of an object
		((TextBox)FormView1.FindControl("RepliedBy")).Text = Request.Cookies["Name"].Value;
	}
	else
		FormView1.ChangeMode(FormViewMode.ReadOnly);
}

Does anybody know how to do this?

Thanks, Peter
 
Code:
protected void  FormView1_DataBound(object sender, System.EventArgs e)
{
    DataRowView row = (DataRowView)FormView1.DataItem;
    if (row["Reply"] == DBNull.Value)
    {
        FormView1.ChangeMode(FormViewMode.Edit);
        Trace.Write("changemode");

        string replyValue = "";
        if (Request.Cookies["Name"] != null)
        {
            replyValue = Request.Cookies["Name"].Value
        }
        
        TextBox ctrl = (TextBox)FormView1.FindControl("RepliedBy");
        if(ctrl != null)
        {
             ctrl.Text = replyValue;
        }
    }
    else
    {
        FormView1.ChangeMode(FormViewMode.ReadOnly);
    }
}
step through this code and find out what object is not instanciated.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Jason, thank you so much!! I have spend so much time on this problem, and clearly don't know all the best ways to track bugs yet.

The problem was, that apparently the DataBound event is triggered TWICE! The first time, FindControl won't find any controls, but the second time everything is okay. So your check to see if a control was found solved my problem by skipping the first time and only performing the second time...

Thanks, it is so frustrating to spend hours and hours just fighting the same bit of code... ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top