How to initialize textbox value in FormView in InsertItemMode?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I want to insert some value into textbox automaticly in FormView
InsertItemMode. This value may be a session value or to click a button to
insert it. I try to replace <%# bind("blah")%> description by
(String)Session.contains["blah"]. This method is work when showing on the
screen. But it will not store into the database source because I replace the
<%#bind %>. Is there any good way to achieve my thinking?
 
Bind the textbox to the data source where you want the data to go then put
the value in the text box by putting something based on the following code in
the ModeChanging event.

if (e.NewMode = DetailsViewMode.Insert)
{
Code to asign value to textbox...
}
 
Yuchang,

You can leave the binding <%# bind("blah")%> for what it is to preserve the
databinding functionality.
Use the FindControl method to automatically fill your control:

protected void FormView1_PreRender(object sender, EventArgs e)
{
string strDefaultProdName = "Product name";
if (FormView1.CurrentMode == FormViewMode.Insert &&
((TextBox)FormView1.FindControl("ProductNameTextBox")).Text.Length==0)
((TextBox)FormView1.FindControl("ProductNameTextBox")).Text =
strDefaultProdName;
}

Regards, Dustin.
 
That is what I really need!!
Thank you very much~~

Dustin van de Sande said:
Yuchang,

You can leave the binding <%# bind("blah")%> for what it is to preserve the
databinding functionality.
Use the FindControl method to automatically fill your control:

protected void FormView1_PreRender(object sender, EventArgs e)
{
string strDefaultProdName = "Product name";
if (FormView1.CurrentMode == FormViewMode.Insert &&
((TextBox)FormView1.FindControl("ProductNameTextBox")).Text.Length==0)
((TextBox)FormView1.FindControl("ProductNameTextBox")).Text =
strDefaultProdName;
}

Regards, Dustin.

yuchang said:
Hi,

I want to insert some value into textbox automaticly in FormView
InsertItemMode. This value may be a session value or to click a button to
insert it. I try to replace <%# bind("blah")%> description by
(String)Session.contains["blah"]. This method is work when showing on the
screen. But it will not store into the database source because I replace the
<%#bind %>. Is there any good way to achieve my thinking?
 
Back
Top