Postback,Master page,User control problem

Discussion in 'ASP .Net' started by yoknows@gmail.com, May 19, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. clintonG Guest

    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/

    <> wrote in message
    news:...
    > 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
    > }
    > }
    > }
    > }
    > }
    >
     
    clintonG, May 19, 2006
    #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. Doug McCann
    Replies:
    1
    Views:
    1,156
    William Brogden
    Aug 5, 2004
  2. Zeba
    Replies:
    1
    Views:
    547
    Mark Rae
    Feb 22, 2007
  3. Salim
    Replies:
    2
    Views:
    1,614
    SerpentKiss2010
    Jun 15, 2011
  4. Nathan Sokalski
    Replies:
    25
    Views:
    1,509
    Nathan Sokalski
    Mar 4, 2010
  5. SerpentKiss2010
    Replies:
    0
    Views:
    2,549
    SerpentKiss2010
    Apr 6, 2011
Loading...

Share This Page