Why does DetailsView become read-only after insert?

Discussion in 'ASP .Net' started by mpaine, Jun 10, 2008.

  1. mpaine

    mpaine Guest

    Hello,

    I am completely lost as to why I can't update a DropDownList inside a
    DetailsView after I perform an insert into an object datasource. I tried to
    simply it down to the core demostration:



    default.aspx:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
    Inherits="TestDetailsView.Default" %>
    <!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" >
    <body>
    <form id="form1" runat="server">
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
    DataObjectTypeName="TestDetailsView.User"
    TypeName="TestDetailsView.UserAdapter" InsertMethod="InsertUser"
    />

    <asp:DetailsView ID="DetailsView1" runat="server"
    AutoGenerateRows="False" DataSourceID="ObjectDataSource1"
    DefaultMode="Insert" OnItemInserting="DetailsView1_ItemInserting">
    <Fields>
    <asp:TemplateField HeaderText="Command" runat="server">
    <ItemTemplate>
    <asp:DropDownList ID="dv_ddlCommand" runat="server"
    AutoPostBack="false">
    <asp:ListItem Text="add"
    Value="add"></asp:ListItem>
    <asp:ListItem Text="delete"
    Value="delete"></asp:ListItem>
    </asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateField>

    <asp:CommandField ButtonType="Button"
    ShowInsertButton="True" InsertText="Add"></asp:CommandField>
    </Fields>
    </asp:DetailsView>
    </form>
    </body>
    </html>



    default.aspx.cs:

    using System;
    using System.Drawing;
    using System.Web.UI.WebControls;

    namespace TestDetailsView
    {

    public class User
    {
    private string _Command = "new";
    public string Command
    {
    get { return _Command; }
    set { _Command = value; }
    }
    public User() { }
    public User(string Command)
    {
    this.Command = Command;
    }
    }

    public class UserAdapter
    {
    public void InsertUser(User User)
    {
    }
    }

    public partial class Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    DropDownList ddl =
    (DropDownList)DetailsView1.FindControl("dv_ddlCommand");
    if (ddl != null)
    {
    ddl.BackColor = Color.Red;
    }
    }

    protected void DetailsView1_ItemInserting(object sender,
    DetailsViewInsertEventArgs e)
    {
    DropDownList ddl =
    (DropDownList)DetailsView1.FindControl("dv_ddlCommand");
    if (ddl != null)
    {
    e.Values["Command"] = ddl.SelectedValue;
    ddl.BackColor = Color.Red;
    }
    }

    }
    }


    Any help would be extremely helpful.


    Thank you!



    --
    msdn premium subscriber
     
    mpaine, Jun 10, 2008
    #1
    1. Advertising

  2. mpaine

    mpaine Guest

    oops, I guess I should have mentioned that the sample should set
    ddl.BackColor = Color.Red, within Page_Load(), after the insert occurs but it
    doesn't.

    --
    msdn premium subscriber


    "mpaine" wrote:

    > Hello,
    >
    > I am completely lost as to why I can't update a DropDownList inside a
    > DetailsView after I perform an insert into an object datasource. I tried to
    > simply it down to the core demostration:
    >
    >
    >
    > default.aspx:
    >
    > <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
    > Inherits="TestDetailsView.Default" %>
    > <!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" >
    > <body>
    > <form id="form1" runat="server">
    > <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
    > DataObjectTypeName="TestDetailsView.User"
    > TypeName="TestDetailsView.UserAdapter" InsertMethod="InsertUser"
    > />
    >
    > <asp:DetailsView ID="DetailsView1" runat="server"
    > AutoGenerateRows="False" DataSourceID="ObjectDataSource1"
    > DefaultMode="Insert" OnItemInserting="DetailsView1_ItemInserting">
    > <Fields>
    > <asp:TemplateField HeaderText="Command" runat="server">
    > <ItemTemplate>
    > <asp:DropDownList ID="dv_ddlCommand" runat="server"
    > AutoPostBack="false">
    > <asp:ListItem Text="add"
    > Value="add"></asp:ListItem>
    > <asp:ListItem Text="delete"
    > Value="delete"></asp:ListItem>
    > </asp:DropDownList>
    > </ItemTemplate>
    > </asp:TemplateField>
    >
    > <asp:CommandField ButtonType="Button"
    > ShowInsertButton="True" InsertText="Add"></asp:CommandField>
    > </Fields>
    > </asp:DetailsView>
    > </form>
    > </body>
    > </html>
    >
    >
    >
    > default.aspx.cs:
    >
    > using System;
    > using System.Drawing;
    > using System.Web.UI.WebControls;
    >
    > namespace TestDetailsView
    > {
    >
    > public class User
    > {
    > private string _Command = "new";
    > public string Command
    > {
    > get { return _Command; }
    > set { _Command = value; }
    > }
    > public User() { }
    > public User(string Command)
    > {
    > this.Command = Command;
    > }
    > }
    >
    > public class UserAdapter
    > {
    > public void InsertUser(User User)
    > {
    > }
    > }
    >
    > public partial class Default : System.Web.UI.Page
    > {
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > DropDownList ddl =
    > (DropDownList)DetailsView1.FindControl("dv_ddlCommand");
    > if (ddl != null)
    > {
    > ddl.BackColor = Color.Red;
    > }
    > }
    >
    > protected void DetailsView1_ItemInserting(object sender,
    > DetailsViewInsertEventArgs e)
    > {
    > DropDownList ddl =
    > (DropDownList)DetailsView1.FindControl("dv_ddlCommand");
    > if (ddl != null)
    > {
    > e.Values["Command"] = ddl.SelectedValue;
    > ddl.BackColor = Color.Red;
    > }
    > }
    >
    > }
    > }
    >
    >
    > Any help would be extremely helpful.
    >
    >
    > Thank you!
    >
    >
    >
    > --
    > msdn premium subscriber
     
    mpaine, Jun 10, 2008
    #2
    1. Advertising

  3. Hi mpaine,

    From your description, you're using the ASP.NET DetailsView control for
    database record inserting. However, you encountered some problems to modify
    some control in the Detailsview after inserting, correct?

    According to the aspx template and code snippet you provided, I have the
    following question need to confirm:

    1. You're perform inserting in the Detailsview, however, the template you
    used (for the template column) is "ItemTemplate", why didn't you use the
    "InsertItemTemplate" since that's the correct template for putting markup
    displayed at insert mode:

    <<<<<<<<<<<
    <InsertItemTemplate>
    this is insert template.<br />
    <asp:DropDownList ID="dv_ddlCommand"
    runat="server" AutoPostBack="false">
    <asp:ListItem Text="add"
    Value="add"></asp:ListItem>
    <asp:ListItem Text="delete"
    Value="delete"></asp:ListItem>
    </asp:DropDownList>
    </InsertItemTemplate>
    </asp:TemplateField>
    >>>>>>>>>>>>>>>



    2. For inserting, "Item_Inserting" event fires before the inserting is done
    to database. If you want the event fired after record has been correctly
    inserted, you should use "Item_Inserted" event.

    Also, I'm not sure why will you need to modify the dropdownlist after
    inserting, by default, after inserting, the DetailsView will change its
    mode to its default template so that the controls are get reloaded(that may
    cause any changes you made in the above events not be persisted). Are you
    going to display some message or mark some flag to let the user know that
    the item has been inserted? If so, I think you can put an external Label
    control on the page and set some message to the Label in the DetailsView's
    "Item_Inserted" event. e.g.

    <<<<<<<<<<<<<
    protected void DetailsView1_ItemInserted(object sender,
    DetailsViewInsertedEventArgs e)
    {
    lblMessage.Text = "New Item has been inserted!";
    }
    >>>>>>>>>>>>>>>


    Please feel free to let me know if there is any particular requirement or
    concerns in your scenario.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    .

    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications.

    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.
    ==================================================
    This posting is provided "AS IS" with no warranties, and confers no rights.



    --------------------
    >From: =?Utf-8?B?bXBhaW5l?= <>
    >References: <>
    >Subject: RE: Why does DetailsView become read-only after insert?
    >Date: Mon, 9 Jun 2008 21:28:03 -0700


    >
    >oops, I guess I should have mentioned that the sample should set
    >ddl.BackColor = Color.Red, within Page_Load(), after the insert occurs but

    it
    >doesn't.
    >
    >--
    >msdn premium subscriber
    >
    >
    >"mpaine" wrote:
    >
    >> Hello,
    >>
    >> I am completely lost as to why I can't update a DropDownList inside a
    >> DetailsView after I perform an insert into an object datasource. I

    tried to
    >> simply it down to the core demostration:
    >>
    >>
    >>
    >> default.aspx:
    >>
    >> <%@ Page Language="C#" AutoEventWireup="true"

    CodeBehind="Default.aspx.cs"
    >> Inherits="TestDetailsView.Default" %>
    >> <!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" >
    >> <body>
    >> <form id="form1" runat="server">
    >> <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
    >> DataObjectTypeName="TestDetailsView.User"
    >> TypeName="TestDetailsView.UserAdapter"

    InsertMethod="InsertUser"
    >> />
    >>
    >> <asp:DetailsView ID="DetailsView1" runat="server"
    >> AutoGenerateRows="False" DataSourceID="ObjectDataSource1"
    >> DefaultMode="Insert"

    OnItemInserting="DetailsView1_ItemInserting">
    >> <Fields>
    >> <asp:TemplateField HeaderText="Command" runat="server">
    >> <ItemTemplate>
    >> <asp:DropDownList ID="dv_ddlCommand"

    runat="server"
    >> AutoPostBack="false">
    >> <asp:ListItem Text="add"
    >> Value="add"></asp:ListItem>
    >> <asp:ListItem Text="delete"
    >> Value="delete"></asp:ListItem>
    >> </asp:DropDownList>
    >> </ItemTemplate>
    >> </asp:TemplateField>
    >>
    >> <asp:CommandField ButtonType="Button"
    >> ShowInsertButton="True" InsertText="Add"></asp:CommandField>
    >> </Fields>
    >> </asp:DetailsView>
    >> </form>
    >> </body>
    >> </html>
    >>
    >>
    >>
    >> default.aspx.cs:
    >>
    >> using System;
    >> using System.Drawing;
    >> using System.Web.UI.WebControls;
    >>
    >> namespace TestDetailsView
    >> {
    >>
    >> public class User
    >> {
    >> private string _Command = "new";
    >> public string Command
    >> {
    >> get { return _Command; }
    >> set { _Command = value; }
    >> }
    >> public User() { }
    >> public User(string Command)
    >> {
    >> this.Command = Command;
    >> }
    >> }
    >>
    >> public class UserAdapter
    >> {
    >> public void InsertUser(User User)
    >> {
    >> }
    >> }
    >>
    >> public partial class Default : System.Web.UI.Page
    >> {
    >> protected void Page_Load(object sender, EventArgs e)
    >> {
    >> DropDownList ddl =
    >> (DropDownList)DetailsView1.FindControl("dv_ddlCommand");
    >> if (ddl != null)
    >> {
    >> ddl.BackColor = Color.Red;
    >> }
    >> }
    >>
    >> protected void DetailsView1_ItemInserting(object sender,
    >> DetailsViewInsertEventArgs e)
    >> {
    >> DropDownList ddl =
    >> (DropDownList)DetailsView1.FindControl("dv_ddlCommand");
    >> if (ddl != null)
    >> {
    >> e.Values["Command"] = ddl.SelectedValue;
    >> ddl.BackColor = Color.Red;
    >> }
    >> }
    >>
    >> }
    >> }
    >>
    >>
    >> Any help would be extremely helpful.
    >>
    >>
    >> Thank you!
    >>
    >>
    >>
    >> --
    >> msdn premium subscriber

    >
     
    Steven Cheng [MSFT], Jun 10, 2008
    #3
  4. mpaine

    mpaine Guest

    Thanks for the response! I tried changing things as you mentioned, but I
    guess I still have the same problem. The idea is that I am using a
    DetailsView as an insert into a collection class. I want to reuse the same
    detailsview for multiple inserts.

    What I am trying to do is hide/unhide the DropDownList for certain types of
    users (using Membership Roles). The problem is that after the insert, the
    DropDownList becomes the default (either hidden always or not hidden always)
    and I can't dynamically change it after the insert (as I was trying to
    demonstrate using the BackColor).

    For example, in my real code, I do the following:

    <asp:TemplateField HeaderText="Command" runat="server" Visible="true">
    <ItemTemplate>
    <asp:DropDownList ID="dv_ddlCommand" runat="server"
    AutoPostBack="false" Enabled="false"
    Visible="false">
    <asp:ListItem Text="add" Value="add"></asp:ListItem>
    <asp:ListItem Text="reschedule"
    Value="reschedule"></asp:ListItem>
    </asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateField>

    and my code behind uses:

    DropDownList ddl =
    (DropDownList)DetailsView1.FindControl("dv_ddlCommand");
    if (ddl != null)
    {
    ddl.Enabled = User.IsInRole("clk_admin");
    ddl.Visible = ddl.Enabled;
    }

    ...here, when I step debug through it, ddl.Enabled and ddl.Visible change but
    the changes aren't persisted during the Render cycle.

    Do you have any suggestions? Perhaps I am using the wrong thing here --
    maybe there is something better to use than a DetailsView, although it does
    work nicely otherwise.

    Thanks again!
    Michael
    --
    msdn premium subscriber


    "Steven Cheng [MSFT]" wrote:

    > Hi mpaine,
    >
    > From your description, you're using the ASP.NET DetailsView control for
    > database record inserting. However, you encountered some problems to modify
    > some control in the Detailsview after inserting, correct?
    >
    > According to the aspx template and code snippet you provided, I have the
    > following question need to confirm:
    >
    > 1. You're perform inserting in the Detailsview, however, the template you
    > used (for the template column) is "ItemTemplate", why didn't you use the
    > "InsertItemTemplate" since that's the correct template for putting markup
    > displayed at insert mode:
    >
    > <<<<<<<<<<<
    > <InsertItemTemplate>
    > this is insert template.<br />
    > <asp:DropDownList ID="dv_ddlCommand"
    > runat="server" AutoPostBack="false">
    > <asp:ListItem Text="add"
    > Value="add"></asp:ListItem>
    > <asp:ListItem Text="delete"
    > Value="delete"></asp:ListItem>
    > </asp:DropDownList>
    > </InsertItemTemplate>
    > </asp:TemplateField>
    > >>>>>>>>>>>>>>>

    >
    >
    > 2. For inserting, "Item_Inserting" event fires before the inserting is done
    > to database. If you want the event fired after record has been correctly
    > inserted, you should use "Item_Inserted" event.
    >
    > Also, I'm not sure why will you need to modify the dropdownlist after
    > inserting, by default, after inserting, the DetailsView will change its
    > mode to its default template so that the controls are get reloaded(that may
    > cause any changes you made in the above events not be persisted). Are you
    > going to display some message or mark some flag to let the user know that
    > the item has been inserted? If so, I think you can put an external Label
    > control on the page and set some message to the Label in the DetailsView's
    > "Item_Inserted" event. e.g.
    >
    > <<<<<<<<<<<<<
    > protected void DetailsView1_ItemInserted(object sender,
    > DetailsViewInsertedEventArgs e)
    > {
    > lblMessage.Text = "New Item has been inserted!";
    > }
    > >>>>>>>>>>>>>>>

    >
    > Please feel free to let me know if there is any particular requirement or
    > concerns in your scenario.
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    > Delighting our customers is our #1 priority. We welcome your comments and
    > suggestions about how we can improve the support we provide to you. Please
    > feel free to let my manager know what you think of the level of service
    > provided. You can send feedback directly to my manager at:
    > .
    >
    > ==================================================
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    > ications.
    >
    > Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    > where an initial response from the community or a Microsoft Support
    > Engineer within 1 business day is acceptable. Please note that each follow
    > up response may take approximately 2 business days as the support
    > professional working with you may need further investigation to reach the
    > most efficient resolution. The offering is not appropriate for situations
    > that require urgent, real-time or phone-based interactions or complex
    > project analysis and dump analysis issues. Issues of this nature are best
    > handled working with a dedicated Microsoft Support Engineer by contacting
    > Microsoft Customer Support Services (CSS) at
    > http://msdn.microsoft.com/subscriptions/support/default.aspx.
    > ==================================================
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
    >
    > --------------------
    > >From: =?Utf-8?B?bXBhaW5l?= <>
    > >References: <>
    > >Subject: RE: Why does DetailsView become read-only after insert?
    > >Date: Mon, 9 Jun 2008 21:28:03 -0700

    >
    > >
    > >oops, I guess I should have mentioned that the sample should set
    > >ddl.BackColor = Color.Red, within Page_Load(), after the insert occurs but

    > it
    > >doesn't.
    > >
    > >--
    > >msdn premium subscriber
    > >
    > >
    > >"mpaine" wrote:
    > >
    > >> Hello,
    > >>
    > >> I am completely lost as to why I can't update a DropDownList inside a
    > >> DetailsView after I perform an insert into an object datasource. I

    > tried to
    > >> simply it down to the core demostration:
    > >>
    > >>
    > >>
    > >> default.aspx:
    > >>
    > >> <%@ Page Language="C#" AutoEventWireup="true"

    > CodeBehind="Default.aspx.cs"
    > >> Inherits="TestDetailsView.Default" %>
    > >> <!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" >
    > >> <body>
    > >> <form id="form1" runat="server">
    > >> <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
    > >> DataObjectTypeName="TestDetailsView.User"
    > >> TypeName="TestDetailsView.UserAdapter"

    > InsertMethod="InsertUser"
    > >> />
    > >>
    > >> <asp:DetailsView ID="DetailsView1" runat="server"
    > >> AutoGenerateRows="False" DataSourceID="ObjectDataSource1"
    > >> DefaultMode="Insert"

    > OnItemInserting="DetailsView1_ItemInserting">
    > >> <Fields>
    > >> <asp:TemplateField HeaderText="Command" runat="server">
    > >> <ItemTemplate>
    > >> <asp:DropDownList ID="dv_ddlCommand"

    > runat="server"
    > >> AutoPostBack="false">
    > >> <asp:ListItem Text="add"
    > >> Value="add"></asp:ListItem>
    > >> <asp:ListItem Text="delete"
    > >> Value="delete"></asp:ListItem>
    > >> </asp:DropDownList>
    > >> </ItemTemplate>
    > >> </asp:TemplateField>
    > >>
    > >> <asp:CommandField ButtonType="Button"
    > >> ShowInsertButton="True" InsertText="Add"></asp:CommandField>
    > >> </Fields>
    > >> </asp:DetailsView>
    > >> </form>
    > >> </body>
    > >> </html>
    > >>
    > >>
    > >>
    > >> default.aspx.cs:
    > >>
    > >> using System;
    > >> using System.Drawing;
    > >> using System.Web.UI.WebControls;
    > >>
    > >> namespace TestDetailsView
    > >> {
    > >>
    > >> public class User
    > >> {
    > >> private string _Command = "new";
    > >> public string Command
    > >> {
    > >> get { return _Command; }
    > >> set { _Command = value; }
    > >> }
    > >> public User() { }
    > >> public User(string Command)
    > >> {
    > >> this.Command = Command;
    > >> }
    > >> }
    > >>
    > >> public class UserAdapter
    > >> {
    > >> public void InsertUser(User User)
    > >> {
    > >> }
    > >> }
    > >>
    > >> public partial class Default : System.Web.UI.Page
    > >> {
    > >> protected void Page_Load(object sender, EventArgs e)
    > >> {
    > >> DropDownList ddl =
    > >> (DropDownList)DetailsView1.FindControl("dv_ddlCommand");
    > >> if (ddl != null)
    > >> {
    > >> ddl.BackColor = Color.Red;
    > >> }
    > >> }
    > >>
    > >> protected void DetailsView1_ItemInserting(object sender,
    > >> DetailsViewInsertEventArgs e)
    > >> {
    > >> DropDownList ddl =
    > >> (DropDownList)DetailsView1.FindControl("dv_ddlCommand");
    > >> if (ddl != null)
    > >> {
    > >> e.Values["Command"] = ddl.SelectedValue;
    > >> ddl.BackColor = Color.Red;
    > >> }
    > >> }
    > >>
    > >> }
    > >> }
    > >>
    > >>
    > >> Any help would be extremely helpful.
    > >>
    > >>
    > >> Thank you!
    > >>
    > >>
    > >>
    > >> --
    > >> msdn premium subscriber

    > >

    >
    >
     
    mpaine, Jun 10, 2008
    #4
  5. Hi Michael,

    If what you want to do is adjust the visiblity of the Dropdownlist depend
    on the current user's roles. I think you should use DetailsView's
    "ItemCreated" event to check the user roles and set DropDownList's
    visiblity. The "ItemCreated" event will fire in each page request and when
    the DetailsView has just populated its control collection. Here is a
    simple example to do this:

    #here is my test code check the querystring parameter to determine the
    visibility, for your case, you can change it to check the currnet user's
    roles
    >>>>>>>>>>>>>>>>>>>>>>>>>

    protected void DetailsView1_ItemCreated(object sender, EventArgs e)
    {

    DropDownList ddl = DetailsView1.FindControl("dv_ddlCommand") as
    DropDownList;
    if (ddl != null)
    {
    if(string.IsNullOrEmpty(Request.QueryString["enable"]))
    {
    ddl.Visible = false;
    }
    }
    }
    >>>>>>>>>>>>>>>>>>>>>>>>>>>


    Hope this helps.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    .

    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications.

    ==================================================
    This posting is provided "AS IS" with no warranties, and confers no rights.

    --------------------
    >From: =?Utf-8?B?bXBhaW5l?= <>
    >Subject: RE: Why does DetailsView become read-only after insert?
    >Date: Tue, 10 Jun 2008 09:57:01 -0700
    >
    >Thanks for the response! I tried changing things as you mentioned, but I
    >guess I still have the same problem. The idea is that I am using a
    >DetailsView as an insert into a collection class. I want to reuse the

    same
    >detailsview for multiple inserts.
    >
    >What I am trying to do is hide/unhide the DropDownList for certain types

    of
    >users (using Membership Roles). The problem is that after the insert, the
    >DropDownList becomes the default (either hidden always or not hidden

    always)
    >and I can't dynamically change it after the insert (as I was trying to
    >demonstrate using the BackColor).
    >
    >For example, in my real code, I do the following:
    >
    ><asp:TemplateField HeaderText="Command" runat="server" Visible="true">
    > <ItemTemplate>
    > <asp:DropDownList ID="dv_ddlCommand" runat="server"
    >AutoPostBack="false" Enabled="false"
    > Visible="false">
    > <asp:ListItem Text="add"

    Value="add"></asp:ListItem>
    > <asp:ListItem Text="reschedule"
    >Value="reschedule"></asp:ListItem>
    > </asp:DropDownList>
    > </ItemTemplate>
    > </asp:TemplateField>
    >
    >and my code behind uses:
    >
    > DropDownList ddl =
    >(DropDownList)DetailsView1.FindControl("dv_ddlCommand");
    > if (ddl != null)
    > {
    > ddl.Enabled = User.IsInRole("clk_admin");
    > ddl.Visible = ddl.Enabled;
    > }
    >
    >..here, when I step debug through it, ddl.Enabled and ddl.Visible change

    but
    >the changes aren't persisted during the Render cycle.
    >
    >Do you have any suggestions? Perhaps I am using the wrong thing here --
    >maybe there is something better to use than a DetailsView, although it

    does
    >work nicely otherwise.
    >
    >Thanks again!
    >Michael
    >--
    >msdn premium subscriber
    >
    >
    >"Steven Cheng [MSFT]" wrote:
    >
    >> Hi mpaine,
    >>
    >> From your description, you're using the ASP.NET DetailsView control for
    >> database record inserting. However, you encountered some problems to

    modify
    >> some control in the Detailsview after inserting, correct?
    >>
    >> According to the aspx template and code snippet you provided, I have the
    >> following question need to confirm:
    >>
    >> 1. You're perform inserting in the Detailsview, however, the template

    you
    >> used (for the template column) is "ItemTemplate", why didn't you use the
    >> "InsertItemTemplate" since that's the correct template for putting

    markup
    >> displayed at insert mode:
    >>
    >> <<<<<<<<<<<
    >> <InsertItemTemplate>
    >> this is insert template.<br />
    >> <asp:DropDownList ID="dv_ddlCommand"
    >> runat="server" AutoPostBack="false">
    >> <asp:ListItem Text="add"
    >> Value="add"></asp:ListItem>
    >> <asp:ListItem Text="delete"
    >> Value="delete"></asp:ListItem>
    >> </asp:DropDownList>
    >> </InsertItemTemplate>
    >> </asp:TemplateField>
    >> >>>>>>>>>>>>>>>

    >>
    >>
    >> 2. For inserting, "Item_Inserting" event fires before the inserting is

    done
    >> to database. If you want the event fired after record has been correctly
    >> inserted, you should use "Item_Inserted" event.
    >>
    >> Also, I'm not sure why will you need to modify the dropdownlist after
    >> inserting, by default, after inserting, the DetailsView will change its
    >> mode to its default template so that the controls are get reloaded(that

    may
    >> cause any changes you made in the above events not be persisted). Are

    you
    >> going to display some message or mark some flag to let the user know

    that
    >> the item has been inserted? If so, I think you can put an external

    Label
    >> control on the page and set some message to the Label in the

    DetailsView's
    >> "Item_Inserted" event. e.g.
    >>
    >> <<<<<<<<<<<<<
    >> protected void DetailsView1_ItemInserted(object sender,
    >> DetailsViewInsertedEventArgs e)
    >> {
    >> lblMessage.Text = "New Item has been inserted!";
    >> }
    >> >>>>>>>>>>>>>>>

    >>
    >> Please feel free to let me know if there is any particular requirement

    or
    >> concerns in your scenario.
    >>
    >> Sincerely,
    >>
    >> Steven Cheng
    >>
    >> Microsoft MSDN Online Support Lead
    >>
    >>
    >> Delighting our customers is our #1 priority. We welcome your comments

    and
    >> suggestions about how we can improve the support we provide to you.

    Please
    >> feel free to let my manager know what you think of the level of service
    >> provided. You can send feedback directly to my manager at:
    >> .
    >>
    >> ==================================================
    >> Get notification to my posts through email? Please refer to
    >>

    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    >> ications.
    >>
    >> Note: The MSDN Managed Newsgroup support offering is for non-urgent

    issues
    >> where an initial response from the community or a Microsoft Support
    >> Engineer within 1 business day is acceptable. Please note that each

    follow
    >> up response may take approximately 2 business days as the support
    >> professional working with you may need further investigation to reach

    the
    >> most efficient resolution. The offering is not appropriate for

    situations
    >> that require urgent, real-time or phone-based interactions or complex
    >> project analysis and dump analysis issues. Issues of this nature are

    best
    >> handled working with a dedicated Microsoft Support Engineer by

    contacting
    >> Microsoft Customer Support Services (CSS) at
    >> http://msdn.microsoft.com/subscriptions/support/default.aspx.
    >> ==================================================
    >> This posting is provided "AS IS" with no warranties, and confers no

    rights.
    >>
    >>
    >>
    >> --------------------
    >> >From: =?Utf-8?B?bXBhaW5l?= <>
    >> >References: <>
    >> >Subject: RE: Why does DetailsView become read-only after insert?
    >> >Date: Mon, 9 Jun 2008 21:28:03 -0700

    >>
    >> >
    >> >oops, I guess I should have mentioned that the sample should set
    >> >ddl.BackColor = Color.Red, within Page_Load(), after the insert occurs

    but
    >> it
    >> >doesn't.
    >> >
    >> >--
    >> >msdn premium subscriber
    >> >
    >> >
    >> >"mpaine" wrote:
    >> >
    >> >> Hello,
    >> >>
    >> >> I am completely lost as to why I can't update a DropDownList inside a
    >> >> DetailsView after I perform an insert into an object datasource. I

    >> tried to
    >> >> simply it down to the core demostration:
    >> >>
    >> >>
    >> >>
    >> >> default.aspx:
    >> >>
    >> >> <%@ Page Language="C#" AutoEventWireup="true"

    >> CodeBehind="Default.aspx.cs"
    >> >> Inherits="TestDetailsView.Default" %>
    >> >> <!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" >
    >> >> <body>
    >> >> <form id="form1" runat="server">
    >> >> <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
    >> >> DataObjectTypeName="TestDetailsView.User"
    >> >> TypeName="TestDetailsView.UserAdapter"

    >> InsertMethod="InsertUser"
    >> >> />
    >> >>
    >> >> <asp:DetailsView ID="DetailsView1" runat="server"
    >> >> AutoGenerateRows="False" DataSourceID="ObjectDataSource1"
    >> >> DefaultMode="Insert"

    >> OnItemInserting="DetailsView1_ItemInserting">
    >> >> <Fields>
    >> >> <asp:TemplateField HeaderText="Command"

    runat="server">
    >> >> <ItemTemplate>
    >> >> <asp:DropDownList ID="dv_ddlCommand"

    >> runat="server"
    >> >> AutoPostBack="false">
    >> >> <asp:ListItem Text="add"
    >> >> Value="add"></asp:ListItem>
    >> >> <asp:ListItem Text="delete"
    >> >> Value="delete"></asp:ListItem>
    >> >> </asp:DropDownList>
    >> >> </ItemTemplate>
    >> >> </asp:TemplateField>
    >> >>
    >> >> <asp:CommandField ButtonType="Button"
    >> >> ShowInsertButton="True" InsertText="Add"></asp:CommandField>
    >> >> </Fields>
    >> >> </asp:DetailsView>
    >> >> </form>
    >> >> </body>
    >> >> </html>
    >> >>
    >> >>
    >> >>
    >> >> default.aspx.cs:
    >> >>
    >> >> using System;
    >> >> using System.Drawing;
    >> >> using System.Web.UI.WebControls;
    >> >>
    >> >> namespace TestDetailsView
    >> >> {
    >> >>
    >> >> public class User
    >> >> {
    >> >> private string _Command = "new";
    >> >> public string Command
    >> >> {
    >> >> get { return _Command; }
    >> >> set { _Command = value; }
    >> >> }
    >> >> public User() { }
    >> >> public User(string Command)
    >> >> {
    >> >> this.Command = Command;
    >> >> }
    >> >> }
    >> >>
    >> >> public class UserAdapter
    >> >> {
    >> >> public void InsertUser(User User)
    >> >> {
    >> >> }
    >> >> }
    >> >>
    >> >> public partial class Default : System.Web.UI.Page
    >> >> {
    >> >> protected void Page_Load(object sender, EventArgs e)
    >> >> {
    >> >> DropDownList ddl =
    >> >> (DropDownList)DetailsView1.FindControl("dv_ddlCommand");
    >> >> if (ddl != null)
    >> >> {
    >> >> ddl.BackColor = Color.Red;
    >> >> }
    >> >> }
    >> >>
    >> >> protected void DetailsView1_ItemInserting(object sender,
    >> >> DetailsViewInsertEventArgs e)
    >> >> {
    >> >> DropDownList ddl =
    >> >> (DropDownList)DetailsView1.FindControl("dv_ddlCommand");
    >> >> if (ddl != null)
    >> >> {
    >> >> e.Values["Command"] = ddl.SelectedValue;
    >> >> ddl.BackColor = Color.Red;
    >> >> }
    >> >> }
    >> >>
    >> >> }
    >> >> }
    >> >>
    >> >>
    >> >> Any help would be extremely helpful.
    >> >>
    >> >>
    >> >> Thank you!
    >> >>
    >> >>
    >> >>
    >> >> --
    >> >> msdn premium subscriber
    >> >

    >>
    >>

    >
     
    Steven Cheng [MSFT], Jun 11, 2008
    #5
  6. Hi Michael,

    Any further progress on this or does the information in my last reply help
    you some?

    If there is anything else we can help, please feel free to let me know.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    .

    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications.
    ==================================================
    This posting is provided "AS IS" with no warranties, and confers no rights.


    --------------------

    >Date: Wed, 11 Jun 2008 04:58:07 GMT
    >Subject: RE: Why does DetailsView become read-only after insert?


    >
    >Hi Michael,
    >
    >If what you want to do is adjust the visiblity of the Dropdownlist depend
    >on the current user's roles. I think you should use DetailsView's
    >"ItemCreated" event to check the user roles and set DropDownList's
    >visiblity. The "ItemCreated" event will fire in each page request and when
    >the DetailsView has just populated its control collection. Here is a
    >simple example to do this:
    >
    >#here is my test code check the querystring parameter to determine the
    >visibility, for your case, you can change it to check the currnet user's
    >roles
    >>>>>>>>>>>>>>>>>>>>>>>>>>

    >protected void DetailsView1_ItemCreated(object sender, EventArgs e)
    > {
    >
    > DropDownList ddl = DetailsView1.FindControl("dv_ddlCommand") as
    >DropDownList;
    > if (ddl != null)
    > {
    > if(string.IsNullOrEmpty(Request.QueryString["enable"]))
    > {
    > ddl.Visible = false;
    > }
    > }
    > }
    >>>>>>>>>>>>>>>>>>>>>>>>>>>>

    >
    >Hope this helps.
    >
    >Sincerely,
    >
    >Steven Cheng
    >
    >Microsoft MSDN Online Support Lead
    >
     
    Steven Cheng [MSFT], Jun 13, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    1,101
    Mark E. Hansen
    Oct 11, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,258
    Smokey Grindel
    Dec 2, 2006
  3. Feege
    Replies:
    0
    Views:
    503
    Feege
    Dec 20, 2005
  4. PerlFAQ Server
    Replies:
    0
    Views:
    612
    PerlFAQ Server
    Feb 11, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    601
    PerlFAQ Server
    Mar 9, 2011
Loading...

Share This Page