Print Page | Close Window

FormView: Change DefaultMode Insert on empty data

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=27830
Printed Date: 28 March 2026 at 2:20pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: FormView: Change DefaultMode Insert on empty data
Posted By: PrivateEye
Subject: FormView: Change DefaultMode Insert on empty data
Date Posted: 13 September 2009 at 11:17am

Good day to all!

I am using FormView on my asp.net page where users can add data. The problem is that when no data is present in the SQL Server table, by default "Insert" link button is not displayed. So users can't insert data in this scnario. I want FormView control to be in "Insert" mode when no data is present in table and when there is data, its mode should be "ReadOnly". Can anyone please help me to solve this problem. Many thanks in advance.



Replies:
Posted By: mpdotnet
Date Posted: 13 September 2009 at 12:41pm
Good day!
 
Try this:
 
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=118291" rel="nofollow - http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=118291
 
protected void FormView1_DataBound(object sender, EventArgs e)
{
    if (FormView1.DataItemCount == 0)
        FormView1.ChangeMode(FormViewMode.Insert);
}
 
If that doesn't work, try the SqlDataSource Selected event:
 
http://www.koocow.com/blog/post/Formview-default-mode.aspx" rel="nofollow - http://www.koocow.com/blog/post/Formview-default-mode.aspx
 
...
 
if (e.AffectedRows < 1)
{
    FormView1.ChangeMode(FormViewMode.Insert);
}
else
{
    FormView1.ChangeMode(FormViewMode.Edit);
}
 
Hope this helps. I haven't tested it.  
 
Regards,
mp
 
(As a newbie, my posts are delayed pendng approval.)
 


Posted By: PrivateEye
Date Posted: 14 September 2009 at 5:28pm
I needed the code in VB.Net but C# is also understandable you know Smile Thank you for the support. I have not tested your suggestions. However, I copied new link button code from ItemTemplate to EmptyDataTempate like this:

<EmptyDataTemplate>
                    No data found.<br />
                    <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
                        CommandName="New" Text="New" />
</EmptyDataTemplate>

and It is working. I am not sure that my approach is professional or not, any idea?


-------------
The Judgement Day



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net