views:

2133

answers:

1

I have a UpdatePanel with a PlaceHold contained in it. I create some controls with Labels and Buttons, when Button is clicked it fires an Event that clears PlaceHolder and adds some Textboxes and a Button with an Event. Problem is when this Button is click it appears to do a PostBack and does not fire Event associated with Button. I thought that since these controls are all contained within the UpdatePanel the would be no PostBack, am I missing the flow.

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<%--<%@ Register assembly="System.Web.DynamicData, Version=3.5.0.0, Culture=neutral,  PublicKeyToken=31bf3856ad364e35" namespace="System.Web.DynamicData" tagprefix="cc1" %>
--%>

<div id="content" >  <!--start content div-->
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <br />
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <br />
    <asp:UpdatePanel ID="upBlog" runat="server" UpdateMode="Conditional">
    <ContentTemplate >

         <asp:PlaceHolder ID="phBlog" runat="server"></asp:PlaceHolder>  
    </ContentTemplate>
    <Triggers >
 <%--   <asp:AsyncPostBackTrigger EventName="Click" ControlID = "btnSave" />--%>
    </Triggers>
    </asp:UpdatePanel>
    <br />
 </div> <!--ends content div-->

</asp:Content>

//Code Behind

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    FirstView();
}
protected void Page_Load(object sender, EventArgs e)
{

}

private void FirstView()
{
    FileStream fs = new FileStream(Server.MapPath(GlobalVar.compathver), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    DataSet dset = new DataSet();
    dset.ReadXml(fs);
    fs.Close();
//other controls
        Button btnComments = new Button();
        btnComments.ID = "mybtnComments" + i.ToString();
        btnComments.BorderWidth = 0;
        btnComments.Text = MyFunc.CountComments(row["ID"].ToString(), dset) + " Comments";
        phBlog.Controls.Add(btnComments);
        btnComments.CommandArgument = row["ID"].ToString();
        btnComments.BorderWidth = 0;

        btnComments.Command += new CommandEventHandler(Button1_Click)

}


private void CommentView(string  ID)   /// THIS DOES not FIRE
{
    DataView myCommentView = GetCommentView(ID);

       Button btnCommentSave = new Button();
        btnCommentSave.ID = "mySavebtnComments" + i.ToString();
        btnCommentSave.Text = "Publish";
        btnCommentSave.BackColor = Color.Aquamarine;
        phBlog.Controls.Add(btnCommentSave);
        btnCommentSave.CommandArgument = row["ID"].ToString();
        btnCommentSave.Click  += new EventHandler(btnSave_Click);


}




protected void Button1_Click(object sender, CommandEventArgs e)
{
    CommentView(e.CommandArgument.ToString());
}

protected void btnSave_Click(object sender, EventArgs e)
//protected void btnSave_Click(object sender, CommandEventArgs e)
{
    FileStream fsxml = new FileStream(Server.MapPath(GlobalVar.compathver), FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);

    //other code
    // XML Document Saved
    xmldoc.Save(fsxml);
 }
A: 

The UpdatePanel still does a postback. The whole page life-cycle is repeated when the button is clicked, and therefore the Page_Init and Page_Load for example will be executed again. AJAX does not remove the postbox, just hides it from the client.

I can't obviously see something wrong with the code you've posted however I would suggest you place a break point in the Page_Init as a start. This will be able to tell you if the postback is actually happening or not, since this will be fired if the button is indeed trying to fire the button click event. Keep in mind any click events is fired after the page life cycle completes (Theoretically).

On second look I would suggest that you change the Button event in the FirstView() method to a Click rather then a Command and see if this triggers the event.

Diago