How to set text in a TextBox in InsertItemTemplate in a FormView with DefaultMode="Insert" from code behind.
You have an FormView with DefaultMode set to insert. You want people to come to this page/component to enter data. But you also want to populate it with data already existing from some other part of your database. This means you need to set it from code behind. Sounds straight forward. Seems to be something most people would want to do, so it ought to be intuitive.
Here are some things that don't work:
1. You try to access the TextBox by it's ID, like you normally would access an element on the page. (assuming it's id is 'TitleTextBox')
TitleTextBox.Text = ...
hang on it can't be found from code behind, so:
2. In the Page_Load load method, find the text field and set it's value with something like this:
TextBox DataTitle = (TextBox)FormView1.FindControl("TitleTextBox");
DataTitle.Text = "Sometext retrieved from the database or whatever."
Seems obvious - you load the page then set the text in the text field but this gives the error:
Operator '==' incompatible with operand types 'Guid?' and 'Object'
It seems that some sort of comparison is going on behind that code and there is a type missmatch. Maybe it's because the value for the TextBox hasn't been created yet, or the TextBox itself hasn't been created, or it's all null still or something.
3. In your LinqDataSource markup, you try:
<asp:Parameter Name="Title" ConvertEmptyStringToNull="true" DefaultValue="Some default value" />
But that doesn't put any value in the text field that the user can see. (You also try setting that value from code behind as long shot, but that's no good either) Not really what you would expect, but let's assume Microsoft's intention was to use the DefaultValue if the user hadn't put any text in. That's not the issue at hand so forget about it.
4. Maybe it's all because of the page loading life cycle. You try doing the same thing as above:
TextBox DataTitle = (TextBox)FormView1.FindControl("TitleTextBox");
DataTitle.Text = "Sometext retrieved from the database or whatever."
In the OnLoad(EventArgs e) event.
No joy.
5. I've lost track, but at some point I also tried:
LinqDataSource_Docs.InsertParameters["Title"].DefaultValue = "asdfasdfasdf"
THIS DOES WORK:
6. What really works is listening to the OnDataBound event of the FormView (so it is a lifecycle issue). It makes sense really. Why didn't you think of that first? It would have been nice if that answer was more common than the above suggestions in the forums/blogs, so here it is:
The markup is something like this:
<asp:FormView ID="FormView1" runat="server" DataSourceID="LinqDataSource_Docs" DefaultMode="Insert"
DataKeyNames="SomeDBId" CellPadding="6" OnDataBound="FormView1_DataBound">
And the code behind:
protected void FormView1_DataBound( object sender, EventArgs e )
{
String SomeDBId = Page.Request.QueryString["SomeDBId"];
var res = (from r in context.MY_DB_TBLs
where String.Equals(r.SomeDBId.ToString(), SomeDBId)
select r).Single();
TextBox DataTitle = (TextBox)FormView1.FindControl("TitleTextBox");
DataTitle.Text = res.Title; //or any text you like, not necessarily from the db query
}
No comments:
Post a Comment