By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 1,027 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

Postback,Master page,User control problem

P: n/a
Hello .Net Gurus. This is my first post here so I apologize in advance
if I have not provided the right information. I hope someone has seen
this problem before and can tell me what I am doing wrong. I have
simplified the problem with the following example:

Using asp.net 2.0, I have a Master page template used on default.aspx.
From within the Content tags on that page I placed a Datalist. It is

iterating a User control "Inner" with one property, Name. The user
control has a checkbox with OnChanged event firing a postback.

The problem is on postback to default.aspx, I need to know the state of
the Checkbox that was clicked on in the "Inner" user control. Alas,
the value is always FALSE. Only when the User control's OnChanged
event executes, do I see the new value.

When I do this without a Master page, it works as expected, and I can
see the new value of the Checkbox at the Page_Load of the default.aspx.
Any ideas what I am doing wrong here? (Thanks)

The code below is from default.aspx

<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default"
MasterPageFile="~/templates/eDonations.master" %>

<%@ Register Src="inner.ascx" TagName="inner" TagPrefix="uc2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<uc2:inner id="Inner1" runat="server" Name=<%#
Container.DataItem %> >
</uc2:inner>
</ItemTemplate>
</asp:DataList>

</ASP:Content>
---------------------------------------
And here is the Code behind:

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ArrayList list = new ArrayList();
list.Add("1");
list.Add("2");

DataList1.DataSource = list;
DataList1.DataBind();
}
else
{
inner _inItem = new inner();
int ItemIndex = 0;
foreach (DataListItem _item in DataList1.Items)
{
++ItemIndex;
_inItem = (inner)_item.FindControl("inner1");
string hello = _inItem.Name;
CheckBox chk =
(CheckBox)_inItem.FindControl("Checkbox1");
if (chk.Checked)
{
//Do something
}
}
}
}
}

May 18 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Some tips...

// After the @Page directive put this into your file...
<%@ MasterType VirtualPath="~/MasterPage.master" %>

Enable trace for the page in the @Page directive. Load the page and observe
the hierarchical location of the Checkbox you want to find. When referencing
controls in a MasterPage we have to use the entire hierarchy with the
FindControl method.

It gets a bit tricky. Search the web for referencing controls in a
MasterPage.

<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/

<yo*****@gmail.com> wrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
Hello .Net Gurus. This is my first post here so I apologize in advance
if I have not provided the right information. I hope someone has seen
this problem before and can tell me what I am doing wrong. I have
simplified the problem with the following example:

Using asp.net 2.0, I have a Master page template used on default.aspx.
From within the Content tags on that page I placed a Datalist. It is

iterating a User control "Inner" with one property, Name. The user
control has a checkbox with OnChanged event firing a postback.

The problem is on postback to default.aspx, I need to know the state of
the Checkbox that was clicked on in the "Inner" user control. Alas,
the value is always FALSE. Only when the User control's OnChanged
event executes, do I see the new value.

When I do this without a Master page, it works as expected, and I can
see the new value of the Checkbox at the Page_Load of the default.aspx.
Any ideas what I am doing wrong here? (Thanks)

The code below is from default.aspx

<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default"
MasterPageFile="~/templates/eDonations.master" %>

<%@ Register Src="inner.ascx" TagName="inner" TagPrefix="uc2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<uc2:inner id="Inner1" runat="server" Name=<%#
Container.DataItem %> >
</uc2:inner>
</ItemTemplate>
</asp:DataList>

</ASP:Content>
---------------------------------------
And here is the Code behind:

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ArrayList list = new ArrayList();
list.Add("1");
list.Add("2");

DataList1.DataSource = list;
DataList1.DataBind();
}
else
{
inner _inItem = new inner();
int ItemIndex = 0;
foreach (DataListItem _item in DataList1.Items)
{
++ItemIndex;
_inItem = (inner)_item.FindControl("inner1");
string hello = _inItem.Name;
CheckBox chk =
(CheckBox)_inItem.FindControl("Checkbox1");
if (chk.Checked)
{
//Do something
}
}
}
}
}

May 19 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.