User Server control nested in DataGrid

T

TokNor

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
 
M

Michael Tkachev

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:[email protected]

bye-bye.

The Best Regards,
Web Developer
Michael Tkachev
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top