User Server control nested in DataGrid

Discussion in 'ASP .Net Datagrid Control' started by TokNor, Nov 27, 2003.

  1. TokNor

    TokNor Guest

    Hi all,

    I am trying to create a web page where im displaying some information
    in a datagrid and use a nested custom control in EditItemTemplate. In
    Normal Modus it shows my Data as Label. If i go to Edit Modus it shows
    me the DropDownList. That works fine and I can see the List.

    If I want to Update (use the UpdateCommand) and store the Selected
    Item to the Database I get an Error (System.NullReferenceException).

    I have written the following code:

    test.aspx

    <%@ Register TagPrefix="tom" TagName="lizenzart"
    Src="modules/lizenzart_liste.ascx" %>
    <%@ Page language="c#" Codebehind="test2.aspx.cs"
    Inherits="Lizenzdialog.Test2" Debug="true" %>

    <ASP:DATAGRID id="LizenzListeDG" runat="server"
    OnEditCommand="LizenzListeDG_OnEditCommand"
    OnDeleteCommand="LizenzListeDG_OnDeleteCommand"
    OnUpdateCommand="LizenzListeDG_OnUpdateCommand"
    OnCancelCommand="LizenzListeDG_OnCancelCommand"
    AutoGenerateColumns="False">
    ....
    ....
    <asp:TemplateColumn HeaderText="LIZENZART">
    <ItemTemplate>
    <asp:Label runat="server" Text='<%#
    DataBinder.Eval(Container.DataItem, "LIZENZART") %>'/>
    </ItemTemplate>
    <EditItemTemplate>
    <tom:lizenzart runat="server" id="ctl_lizenzart" />
    </EditItemTemplate>
    </asp:TemplateColumn>
    </ASP:DATAGRID>


    test.aspx.cs

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

    namespace Lizenzdialog
    {
    public class Test : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Label Message;
    protected System.Web.UI.WebControls.DataGrid LizenzListeDG;
    protected Lizenzdialog.modules.lizenzart_liste ctl_lizenzart;

    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!Page.IsPostBack)
    {
    BindData();
    }
    }
    private void BindData()
    {
    LizenzData _LizenzData = new LizenzData();
    DataView LizenzListeDataView = _LizenzData.GetPPNRfromPPVR("A001",
    "0");
    LizenzListeDG.DataSource = LizenzListeDataView;
    LizenzListeDG.DataBind();
    }

    ....
    ....
    public void LizenzListeDG_OnUpdateCommand(Object sender,
    DataGridCommandEventArgs e)
    {
    // Here is the Problem
    Message.Text = ctl_lizenzart.ddl_lizenzart.SelectedItem.Value.Trim();

    ((DataGrid)sender).EditItemIndex = -1;
    BindData();
    }
    ....
    }
    }



    lizenzart.ascx

    using System;
    using System.Data;
    using System.Drawing;
    using System.Collections;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    namespace Lizenzdialog.modules
    {
    public class lizenzart_liste : System.Web.UI.UserControl
    {
    public System.Web.UI.WebControls.DropDownList ddl_lizenzart;

    private void Page_Load(object sender, System.EventArgs e)
    {
    BindData();
    }

    public void BindData()
    {
    ArrayList values = new ArrayList();

    values.Add ("HL");
    values.Add ("NL");
    values.Add ("FL");

    ddl_lizenzart.DataSource = values;
    ddl_lizenzart.DataBind();
    }
    }
    }


    Thanks & Regards,

    Tom
     
    TokNor, Nov 27, 2003
    #1
    1. Advertising

  2. Hi,

    I foud a error in the your code.

    Message.Text = ctl_lizenzart.ddl_lizenzart.SelectedItem.Value.Trim(); -

    This code is not correct.
    You have to find the control in the current row.

    public void LizenzListeDG_OnUpdateCommand(Object sender,
    DataGridCommandEventArgs e)
    {
    Message.Text =
    ((Lizenzdialog.modules.lizenzart_liste)LizenzListeDG.Items[e.Item.ItemIndex]
    ..Cells[2].FindControl("ctl_lizenzart")).SelectedItem.Value.Trim();
    }

    This code is correct. check it.
    Your answer you can to send me to my e-mail. Also if you have any questions,
    you can ask me at anytime.

    mailto:

    bye-bye.

    The Best Regards,
    Web Developer
    Michael Tkachev

    "TokNor" <> wrote in message
    news:...
    > Hi all,
    >
    > I am trying to create a web page where im displaying some information
    > in a datagrid and use a nested custom control in EditItemTemplate. In
    > Normal Modus it shows my Data as Label. If i go to Edit Modus it shows
    > me the DropDownList. That works fine and I can see the List.
    >
    > If I want to Update (use the UpdateCommand) and store the Selected
    > Item to the Database I get an Error (System.NullReferenceException).
    >
    > I have written the following code:
    >
    > test.aspx
    >
    > <%@ Register TagPrefix="tom" TagName="lizenzart"
    > Src="modules/lizenzart_liste.ascx" %>
    > <%@ Page language="c#" Codebehind="test2.aspx.cs"
    > Inherits="Lizenzdialog.Test2" Debug="true" %>
    >
    > <ASP:DATAGRID id="LizenzListeDG" runat="server"
    > OnEditCommand="LizenzListeDG_OnEditCommand"
    > OnDeleteCommand="LizenzListeDG_OnDeleteCommand"
    > OnUpdateCommand="LizenzListeDG_OnUpdateCommand"
    > OnCancelCommand="LizenzListeDG_OnCancelCommand"
    > AutoGenerateColumns="False">
    > ...
    > ...
    > <asp:TemplateColumn HeaderText="LIZENZART">
    > <ItemTemplate>
    > <asp:Label runat="server" Text='<%#
    > DataBinder.Eval(Container.DataItem, "LIZENZART") %>'/>
    > </ItemTemplate>
    > <EditItemTemplate>
    > <tom:lizenzart runat="server" id="ctl_lizenzart" />
    > </EditItemTemplate>
    > </asp:TemplateColumn>
    > </ASP:DATAGRID>
    >
    >
    > test.aspx.cs
    >
    > using System;
    > using System.Data;
    > using System.Web;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.Web.UI.HtmlControls;
    > using System.Drawing;
    >
    > namespace Lizenzdialog
    > {
    > public class Test : System.Web.UI.Page
    > {
    > protected System.Web.UI.WebControls.Label Message;
    > protected System.Web.UI.WebControls.DataGrid LizenzListeDG;
    > protected Lizenzdialog.modules.lizenzart_liste ctl_lizenzart;
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > if(!Page.IsPostBack)
    > {
    > BindData();
    > }
    > }
    > private void BindData()
    > {
    > LizenzData _LizenzData = new LizenzData();
    > DataView LizenzListeDataView = _LizenzData.GetPPNRfromPPVR("A001",
    > "0");
    > LizenzListeDG.DataSource = LizenzListeDataView;
    > LizenzListeDG.DataBind();
    > }
    >
    > ...
    > ...
    > public void LizenzListeDG_OnUpdateCommand(Object sender,
    > DataGridCommandEventArgs e)
    > {
    > // Here is the Problem
    > Message.Text = ctl_lizenzart.ddl_lizenzart.SelectedItem.Value.Trim();
    >
    > ((DataGrid)sender).EditItemIndex = -1;
    > BindData();
    > }
    > ...
    > }
    > }
    >
    >
    >
    > lizenzart.ascx
    >
    > using System;
    > using System.Data;
    > using System.Drawing;
    > using System.Collections;
    > using System.Web;
    > using System.Web.UI.WebControls;
    > using System.Web.UI.HtmlControls;
    >
    > namespace Lizenzdialog.modules
    > {
    > public class lizenzart_liste : System.Web.UI.UserControl
    > {
    > public System.Web.UI.WebControls.DropDownList ddl_lizenzart;
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > BindData();
    > }
    >
    > public void BindData()
    > {
    > ArrayList values = new ArrayList();
    >
    > values.Add ("HL");
    > values.Add ("NL");
    > values.Add ("FL");
    >
    > ddl_lizenzart.DataSource = values;
    > ddl_lizenzart.DataBind();
    > }
    > }
    > }
    >
    >
    > Thanks & Regards,
    >
    > Tom
     
    Michael Tkachev, Nov 28, 2003
    #2
    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. Roy
    Replies:
    1
    Views:
    880
    Scott Allen
    Feb 10, 2005
  2. Leeor Chernov
    Replies:
    2
    Views:
    798
    Leeor Chernov
    Oct 16, 2005
  3. Mad Scientist Jr
    Replies:
    0
    Views:
    659
    Mad Scientist Jr
    Mar 22, 2006
  4. Jonathan Wood

    User Control to Control Other User Control

    Jonathan Wood, Jan 24, 2010, in forum: ASP .Net
    Replies:
    4
    Views:
    568
    Jonathan Wood
    Feb 2, 2010
  5. Andreas Klemt
    Replies:
    0
    Views:
    320
    Andreas Klemt
    Oct 8, 2003
Loading...

Share This Page