We help IT Professionals succeed at work.

Open form to insert new record.

dodgerfan
dodgerfan asked
on
I have a gridview on a form that displays records. Next to each record is a Edit button. When this button is clicked, another form pops up to allow editing of that record. The form that pops up is another asp.net form with a asp:FormView control on it to display a record. I would like to have another button outside of the gridview that will open up the same popup form to insert a new record. So the popup form needs to open up to blank fields. The formview on that form has the EditItemTemplate, ItemTemplate and InsertItemTemplate on it. By defualt I set it to open up in Edit Mode. How can I get my button to open it in Insert Mode? Thanks.
Comment
Watch Question

GlobaLevelProgrammer

Commented:
If I get what your asking ...you can control this from a method gridview row updating function...

the method looks like this:protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtName");



TextBox txtCity = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCity");

//Your updation query pass the values like set city=' "+txtCity.Text+" '

GridView1.EditIndex = -1; //converts to normal mode from edit mode
// If having any bind method call here to see the updated rows

BindData();//(in this method write binding datasource to GridView1)


}
Carlos VillegasFull Stack .NET Developer

Commented:
Hello, you can use the FormView.DefaultMode property to accomplish that, example:
<asp:FormView ID="FormView1" runat="server" DefaultMode="Insert">
.....
</asp:FormView>

Open in new window

Full Stack .NET Developer
Commented:
If you want to reuse the same form that you use for edition, you can implement something like this:
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if (Request.QueryString["Insert"] == "1")
            FormView1.DefaultMode = FormViewMode.Insert;
    }
}

Open in new window


Then in your javascript method to open the popup window you can pass a parameter to indicate the Insert mode, example:
<script type="text/javascript">
    function OpenMyInsertForm() {
        window.open('MyForm.aspx?Insert=1');
    }
</script>

Open in new window

Author

Commented:
yv989c:
That looks lke exactly what I'm looking for. I'll ive it a shot tomorrow. Thanks.
SAMIR BHOGAYTATeam Lead
CERTIFIED EXPERT

Commented:
Hello, it is very easy to open the blank form into the click event of any button. You have to use the btn.Attributes.Add("OnClick","Value") through the javascript for open the form in popup window.

Author

Commented:
Perfect, again. Thanks.
Carlos VillegasFull Stack .NET Developer

Commented:
Glad to help

Explore More ContentExplore courses, solutions, and other research materials related to this topic.