?
Solved

Problem using the same formview within a webusercontrol multiple times on a page

Posted on 2010-11-18
7
Medium Priority
?
492 Views
Last Modified: 2012-06-27
First I'm using VS 2005 with AJAX, and I have a created a SQL Server 2008 Database to track some of our computer componet hardware.

I've created "Web User Controls" for nearly all my objects, and have placed them inside my monitors page.  I have a button for editing a monitor and adding a monitor.  Both Buttons open a ModalPopupExtension UpdatePanel that contain my  WebUserControl.  The WebUserContrl has a modified FormView for Editing and Inserting records in the Monitors table.  I'm using Session parameters to of "MonitorEdit" to allow the webusercontrol on Page load to set the FormView1.DefaultMode = FormViewMode.????

My problem is getting the FormViews to switch from a "ReadOnly", "Edit", and "Insert" modes.  It seems that the webusercontrol is being set for both popups on the initial load, and I have no way of changing them on the fly.  I've read some articles about making the FormView properties public, but I'm unsure how to do that, and if that would even help.  Below is my code, and hope this makes sense.  Thanks for any help.

***********************************************************
<%@ Page Language="VB" MasterPageFile="~/AFRPA_Admin.master" AutoEventWireup="false" CodeFile="Test.aspx.vb" Inherits="Test" title="Untitled Page" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<%@ Register Src="Controls/MonitorEdit.ascx" TagName="MonitorEdit" TagPrefix="uc2" %>
<%@ Register Src="Controls/gvMonitors.ascx" TagName="gvMonitors" TagPrefix="uc3" %>
<%@ Register Src="Controls/gvMonitorModels.ascx" TagName="gvMonitorModels" TagPrefix="uc1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
            <asp:UpdatePanel ID="upEditMonitor" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                    <uc2:MonitorEdit ID="MonitorEdit2" runat="server" />
                </ContentTemplate>
            </asp:UpdatePanel>
            <asp:UpdatePanel ID="upAddMonitor" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                    <uc2:MonitorEdit ID="MonitorEdit1" runat="server" />
                </ContentTemplate>
            </asp:UpdatePanel>
    <asp:UpdatePanel ID="upMonitors" runat="server">
        <ContentTemplate>
            <asp:Button ID="btnAddMonitor" runat="server" Text="Add Monitor" OnClick="btnAddMonitor_Click" />
            <br />
            <br />
            <asp:Button ID="btnEditMonitor" runat="server" Text="Edit Monitor" OnClick="btnEditMonitor_Click" />
            <br />
            <br />
            <cc1:ModalPopupExtender ID="ModalPopupExtender1" runat="server"
                TargetControlID="btnAddMonitor"
                BackgroundCssClass="modalBackground"
                PopupControlID="upAddMonitor" >
            </cc1:ModalPopupExtender>
            <cc1:ModalPopupExtender ID="ModalPopupExtender2" runat="server"
                TargetControlID="btnEditMonitor"
                BackgroundCssClass="modalBackground"
                PopupControlID="upEditMonitor" >
            </cc1:ModalPopupExtender>
            <uc1:gvMonitorModels ID="GvMonitorModels1" runat="server" />
            <br />
            <br />
            <uc3:gvMonitors ID="GvMonitors1" runat="server" />
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>

************************************************************
Now Code behind on my Web Form:

Partial Class Test
    Inherits System.Web.UI.Page

    Protected Sub btnAddMonitor_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Session("MonitorEdit") = "Insert"
    End Sub

    Protected Sub btnEditMonitor_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Session("Monitor_ID") = "1"
        Session("MonitorEdit") = "View"
    End Sub

End Class

************************************************************
Now Code behind on my Web User Control:

Partial Class Controls_MonitorEdit
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Session("MonitorEdit") = "" Or Session("MonitorEdit") = "Insert" Then
            FormView1.DefaultMode = FormViewMode.Insert
        ElseIf Session("MonitorEdit") = "View" Then
            FormView1.DefaultMode = FormViewMode.ReadOnly
        ElseIf Session("MonitorEdit") = "Edit" Then
            FormView1.DefaultMode = FormViewMode.Edit
        End If

    End Sub
End Class

********************************************


0
Comment
Question by:afrpa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 7

Expert Comment

by:mr_nadger
ID: 34172050
in the webcontrol,
add another method to control the formview's mode (this is written on the fly but should give you the idea)

    Public Sub switchMode()
        Select Session("MonitorEdit")
            Case "", "Insert"
                FormView1.ChangeMode(FormViewMode.Insert)
            Case "View"
                FormView1.ChangeMode(FormViewMode.ReadOnly)
            Case "Edit"
                FormView1.ChangeMode(FormViewMode.Edit)
        End Select
    End Sub

Put the webcontrol inside an updatepanel set to trigger on the button being clicked, and this should get you what you want
(I prefer using a case statement here as it's easier to follow the logic, but If's just as valid)
0
 
LVL 7

Expert Comment

by:mr_nadger
ID: 34172071
doh forgot to mention calling webcontrol.switchMode() from the button being pressed!
0
 

Author Comment

by:afrpa
ID: 34174650
mr nadger,

Thanks for your help, but the webcontrol is not getting the new session variable that I am setting when I click on the button.  Could this be that the modalpopup is redering before the button onclick is run, or does the popupmodal null the button onclick?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 7

Accepted Solution

by:
mr_nadger earned 2000 total points
ID: 34182866
Yes I think the popup is rendering on the full page load.

Having reread the description, I think you really only need one webcontrol, as you can alter it's mode using the .ChangeMode method, which would simplify your form.
In your code behind, you're using the page_load to define the default behaviour, not what you want it to do at that point.

On your web page's load event (in a not postback clause), try
MonitorEdit.switchMode("View")
upMonitor.Update()

On the edit button
MonitorEdit.switchMode("Edit")
upEditMonitor.Update()

And on the add button
MonitorEdit.switchMode("Insert")
upEditMonitor.Update()

and amend the public method in your webcontrol to
    Public Sub switchMode(byval mymode as string)
        Select mymode
            Case "", "Insert"
                FormView1.ChangeMode(FormViewMode.Insert)
            Case "View"
                FormView1.ChangeMode(FormViewMode.ReadOnly)
            Case "Edit"
                FormView1.ChangeMode(FormViewMode.Edit)
        End Select
    End Sub
I think you were passing an ID parameter as well, it's probably simplest to keep that as the parameter for your webcontrol's datasource.
0
 

Author Comment

by:afrpa
ID: 34251697
Mr Nadger,

Thanks for the help.  I finally figured it out and you put me in the right direction.  I ended up having to set the modalpopupextender to open on a third, hidden box, so that the button would run first, then I would call the ModalPopupExtender1 to open.

        Session("MonitorEdit") = "Insert"
        MonitorEdit1.switchMode()
        upAddMonitor.Update()
        ModalPopupExtender1.Show()

0
 
LVL 29

Expert Comment

by:Kumaraswamy R
ID: 34434107
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question