开发者

Render User Control Conditionally

I have a simple user control to insert the data into the database. I made a public property called isInModifyMode for the user cont开发者_C百科rol.I set the property on the usercontrols pageload to true if edit button is clicked on parent form control's click event to true and then on the page load event of parent page I checked the property IsinModifymode and render the control in modify mode with the values filled in text boxes and all other controls from Database(i.e I change the text property of a button to update instead of save)

but the code does not work. if any body can help thanks in advance.


It's still not quite clear what you're trying to do and what exactly doesn't work. But I'll take a stab at answering with a few assumptions:

  • You want the form to change based off of isInModifyMode only on a simple postback event
  • By "parent form control's click event" you mean a single button that triggers a postback
  • You're using C# and code behinds

The principle is very simple and there are a number of ways to do what I think you're asking. One way would be to basically bind an event to the button to set the isInModifyMode property of your control. And in the control itself, have the property's setter call a method that updates the display. This is illustrated in the following very rudimentary and less-than-perfect example:

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <uc1:WebUserControl ID="WebUserControl1" runat="server" isInModifyMode="false" />
        <asp:Button ID="EditButton" runat="server" Text="Edit" />
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    protected override void OnInit(EventArgs e)
    {
        // Assign the handler for the edit button click event
        EditButton.Click += new EventHandler(EditButton_Click);

        base.OnInit(e);
    }

    /// <summary>
    /// Edit button click handler
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void EditButton_Click(object sender, EventArgs e)
    {
        WebUserControl1.isInModifyMode = true;
    }
}

WebUserControl1.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>

<asp:PlaceHolder ID="ReadOnlyModePlaceholder" runat="server">
    <p>Read Only Mode</p>
</asp:PlaceHolder>

<asp:PlaceHolder ID="EditModePlaceholder" runat="server" Visible="false">
    <p>Edit Mode</p>
</asp:PlaceHolder>

WebUserControl1.ascx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class WebUserControl : System.Web.UI.UserControl
{
    /// <summary>
    /// Changes the display mode by toggling the appropriate controls on or off
    /// based on the isInModifyMode property
    /// </summary>
    private void SetDisplayMode()
    {
        if (!this.isInModifyMode)
        {
            // Render as not modify
            ReadOnlyModePlaceholder.Visible = true;
            EditModePlaceholder.Visible = false;
        }
        else
        {
            // Render as modify
            ReadOnlyModePlaceholder.Visible = false;
            EditModePlaceholder.Visible = true;
        }
    }

    /// <summary>
    /// Determines whether or not the control should be displayed in edit mode or not.
    /// </summary>
    public bool isInModifyMode
    {
        get
        {
            if (ViewState["isInModifyMode"] != null)
                return (bool)ViewState["isInModifyMode"];
            else
                return false;
        }
        set
        {
            ViewState["isInModifyMode"] = value;
            // Since we're possibly changing modes, call
            // the SetDisplayMode() method to update the display
            SetDisplayMode();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        // Set the display mode on the initial load
        if (!IsPostBack)
            SetDisplayMode();
    }
}


Have a look at the ASP.Net page life cycle and the sequence of events.

Page Load and Control Load events are fired before the ButtonClick. Move your rendering logic to the PreRender event.

If this does not help, add some code for better understanding of the question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜