Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Postback,Master page,User control problem

Reply
Thread Tools

Postback,Master page,User control problem

 
 
yoknows@gmail.com
Guest
Posts: n/a
 
      05-18-2006
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">
<aspataList ID="DataList1" runat="server">
<ItemTemplate>
<uc2:inner id="Inner1" runat="server" Name=<%#
Container.DataItem %> >
</uc2:inner>
</ItemTemplate>
</aspataList>

</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
}
}
}
}
}

 
Reply With Quote
 
 
 
 
clintonG
Guest
Posts: n/a
 
      05-19-2006
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/

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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">
> <aspataList ID="DataList1" runat="server">
> <ItemTemplate>
> <uc2:inner id="Inner1" runat="server" Name=<%#
> Container.DataItem %> >
> </uc2:inner>
> </ItemTemplate>
> </aspataList>
>
> </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
> }
> }
> }
> }
> }
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User Control to Control Other User Control Jonathan Wood ASP .Net 4 02-02-2010 03:41 PM
Control, Web Control and Composite Control shapper ASP .Net 6 12-26-2007 02:49 PM
persisting changes to a control outside a user control from the user control? Mad Scientist Jr ASP .Net 0 03-22-2006 08:02 AM
HTML Client Control versus. HTML Server Control versus. Web Server Control Matthew Louden ASP .Net 1 10-11-2003 07:09 PM
Using Table control in a custom composite control. Control does not render properly in design time. jb_in_marietta@yahoo.com ASP .Net 0 07-01-2003 09:26 PM



Advertisments