Solved

OnClick event is not firing on asp.net web page

Posted on 2013-06-26
4
727 Views
Last Modified: 2013-06-28
I have webpage where comments are entered and then displayed in a gridview below.  I had it working where when the Add Comment to Grid button was clicked, the comment was entered in the grid.  Now it isn't working and I have no idea what I changed to prevent it from working.  I'm using an UpdatePanel with AsyncPostBackTrigger to update the GridView.  Here is the relevant code:

ASPX Page
        <div id="tabs-Comments">
            <h3>New Comment</h3>
            <table border="0" width="50%" cellpadding="5" cellspacing="5">
                <tr>
                    <td><b>Comment:</b></td>
                    <td>
                        <asp:TextBox ID="txtComments" runat="server" Rows="5" Columns="75" 
                            TextMode="MultiLine" ClientIDMode="Static"></asp:TextBox>
                    </td>
                </tr>
            </table>
            <table border="0" width="50%">
                <tr>
                    <td width="25%"><asp:Button ID="btnAddComment" runat="server" Text="Add Comment to Grid" onclick="btnAddComment_Click" /></td>
                </tr>
            </table>
            <hr />
            <h3>Comments</h3>
            <div class="comments">
                <asp:UpdatePanel ID="CommentsUpdatePanel" runat="server" ClientIDMode="Static">
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="btnAddComment" EventName="Click" />
                    </Triggers>
                    <ContentTemplate>
                        <asp:GridView ID="gvComments" OnRowDataBound="gvComments_RowDataBound" runat="server" Width="100%" ClientIDMode="Static">
                        </asp:GridView>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </div>
       </div>
    </div>

Open in new window


Code Behind
        protected void InitializeCommentsTab(ref EMPDatabase EMPData)
        {
            // Bind Comments to Grid View
            if (dtComments != null && dtComments.Rows.Count > 0)
            {
                gvComments.DataSource = dtComments;
                gvComments.CellPadding = 3;
                gvComments.CellSpacing = 3;
                gvComments.DataBind();
            }
            else
            {
                gvComments.DataSource = null;
                gvComments.DataBind();
            }
            txtComments.Text = null;
        }

        protected void gvComments_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // Hide CommentsID and EmployeeID columns
            e.Row.Cells[0].Visible = false;
            e.Row.Cells[1].Visible = false;

            // Change Column Names
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[2].Text = "Comments";
                e.Row.Cells[3].Text = "Date";
            }

        }

        protected void btnAddComment_Click(object sender, EventArgs e)
        {
            // Add new comment to Comments Data Table to refresh GridView
            DataRow dr = dtComments.NewRow();
            dr["Comments"] = txtComments.Text;
            dr["LastUpdateTimeStamp"] = DateTime.Now;
            dtComments.Rows.Add(dr);
            gvComments.DataSource = dtComments;
            gvComments.CellPadding = 3;
            gvComments.CellSpacing = 3;
            gvComments.DataBind();
        }

Open in new window


The DataTable dtComments has one record in it when the page loads and the comment is displayed as expected in the GridView.  It is when I add a new comment that the comment doesn't display.  When debugging, the btnAddComment_Click event is never called.  Any help is greatly appreciated!
0
Comment
Question by:dyarosh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 12

Expert Comment

by:jitendra patil
ID: 39280450
your gridview and button "btnAddComment" should be inside the update panel <contentTemplate> tab as below.

<div class="comments">

                <asp:UpdatePanel ID="CommentsUpdatePanel" runat="server" ClientIDMode="Static">
                    <ContentTemplate>
 <table border="0" width="50%">
                <tr>
                    <td width="25%"><asp:Button ID="btnAddComment" runat="server" Text="Add Comment to Grid" onclick="btnAddComment_Click" /></td>
                </tr>
            </table>
                        <asp:GridView ID="gvComments" OnRowDataBound="gvComments_RowDataBound" runat="server" Width="100%" ClientIDMode="Static">
                        </asp:GridView>
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="btnAddComment" EventName="Click" />
                    </Triggers>
                </asp:UpdatePanel>

you may also refer to the following links below.
UpdatePanel not doing Asyc postback on client id mode set to Static
Does Static ClientIDMode always defeat UpdatePanel?
Possible solution to UpdatePanel and ClientIDMode=“Static”

hope this helps.
0
 

Author Comment

by:dyarosh
ID: 39281110
I have another area on my page that does the same thing with IDs that I want to do with Comments and it is working fine.  Here is the code for that:

Working IDs
        <div id="tabs-IDS">
            <h3>New ID</h3>
            <table border="0" cellpadding="5" cellspacing="5">
                <tr>
                    <td><b>ID Type:</b></td>
                    <td>
                        <asp:DropDownList ID="ddIDType" runat="server" AppendDataBoundItems="True" 
                            CausesValidation="True" ClientIDMode="Static">
                            <asp:ListItem Value="">-- Choose ID Type</asp:ListItem>
                        </asp:DropDownList>
                    </td>
                </tr>
                <tr>
                    <td><b>Value:</b></td>
                    <td><asp:TextBox ID="txtNewID" runat="server" MaxLength="30" Text="" ClientIDMode="Static"></asp:TextBox></td>
                        <asp:RequiredFieldValidator ID="IDInsertRequiredValidator" ControlToValidate="txtNewID"
                                Display="Dynamic" Text="Please enter an ID." ToolTip="Please enter an ID." runat="server" />
                </tr>
            </table>
            <table border="0" width="25%">
                <tr>
                    <td width="25%"><asp:Button ID="btnAddID" runat="server" Text="Add ID to Grid" onclick="btnAddID_Click" ClientIDMode="Static" /></td>
                </tr>
            </table>
            <hr />
            <h3>IDs</h3>
            <asp:UpdatePanel ID="IDsUpdatePanel" runat="server" ClientIDMode="Static">
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="btnAddID" EventName="Click" />
                </Triggers>
                <ContentTemplate>
                    <asp:GridView ID="gvIDs" OnRowDataBound="gvIDs_RowDataBound" runat="server" ClientIDMode="Static">
                    </asp:GridView>
                </ContentTemplate>
            </asp:UpdatePanel>

Open in new window


Not working Comments
        <div id="tabs-Comments">
            <h3>New Comment</h3>
            <table border="0" cellpadding="5" cellspacing="5">
                <tr>
                    <td><b>Comment:</b></td>
                    <td><asp:TextBox ID="txtComments" runat="server" Rows="5" Colulmns="75" TextMode="MultiLine" 
                        Text="" ClientIDMode="Static"></asp:TextBox></td>
                </tr>
            </table>
            <table border="0" width="50%">
                <tr>
                    <td><asp:Button ID="btnAddComment" runat="server" Text="Add Comment to Grid" onclick="btnAddComment_Click" ClientIDMode="Static" /></td>
                </tr>
            </table>
            <hr />
            <h3>Comments</h3>
            <asp:UpdatePanel ID="CommentsUpdatePanel" runat="server" ClientIDMode="Static">
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="btnAddComment" EventName="Click" />
                </Triggers>
                <ContentTemplate>
                    <asp:GridView ID="gvComments" OnRowDataBound="gvComments_RowDataBound" runat="server" ClientIDMode="Static">
                    </asp:GridView>
                </ContentTemplate>
            </asp:UpdatePanel>
       </div>

Open in new window


Working IDs Code Behind
        protected void InitializeIDsTab(ref EMPDatabase EMPData)
        {
            // Bind IDs to Grid View
            if (dtIDs != null && dtIDs.Rows.Count > 0)
            {
                gvIDs.DataSource = dtIDs;
                gvIDs.CellPadding = 3;
                gvIDs.CellSpacing = 3;
                gvIDs.DataBind();
            }
            else
            {
                gvIDs.DataSource = null;
                gvIDs.DataBind();
            }
            txtNewID.Text = null;
        }

        protected void gvIDs_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // Hide IDsID, EmployeeID and IDTypeID columns
            e.Row.Cells[0].Visible = false;
            e.Row.Cells[1].Visible = false;
            e.Row.Cells[2].Visible = false;

            // Change Column Names
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[3].Text = "ID Type";
                e.Row.Cells[4].Text = "Value";
            }

        }

        protected void btnAddID_Click(object sender, EventArgs e)
        {
            // Add new ID to ID Data Table to refresh GridView
            DataRow dr = dtIDs.NewRow();
            dr["IDTypeID"] = ddIDType.SelectedValue;
            dr["IDType"] = ddIDType.SelectedItem;
            dr["IDValue"] = txtNewID.Text;
            dtIDs.Rows.Add(dr);
            gvIDs.DataSource = dtIDs;
            gvIDs.CellPadding = 3;
            gvIDs.CellSpacing = 3;
            gvIDs.DataBind();
        }

Open in new window


I don't even get to the Code Behind for the Comments when debugging:
        protected void InitializeCommentsTab(ref EMPDatabase EMPData)
        {
            // Bind Comments to Grid View
            if (dtComments != null && dtComments.Rows.Count > 0)
            {
                gvComments.DataSource = dtComments;
                gvComments.CellPadding = 3;
                gvComments.CellSpacing = 3;
                gvComments.DataBind();
            }
            else
            {
                gvComments.DataSource = null;
                gvComments.DataBind();
            }
            txtComments.Text = null;
        }

        protected void gvComments_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // Hide CommentsID and EmployeeID columns
            e.Row.Cells[0].Visible = false;
            e.Row.Cells[1].Visible = false;

            // Change Column Names
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[2].Text = "Comments";
                e.Row.Cells[3].Text = "Date";
            }

        }

        protected void btnAddComment_Click(object sender, EventArgs e)
        {
            // Add new comment to Comments Data Table to refresh GridView
            DataRow dr = dtComments.NewRow();
            dr["Comments"] = txtComments.Text;
            dr["LastUpdateTimeStamp"] = DateTime.Now;
            dtComments.Rows.Add(dr);
            gvComments.DataSource = dtComments;
            gvComments.CellPadding = 3;
            gvComments.CellSpacing = 3;
            gvComments.DataBind();
        }

Open in new window

0
 
LVL 12

Accepted Solution

by:
jitendra patil earned 500 total points
ID: 39281339
if you are not getting your debug point that means your code is not firing a partial postback as expected.

did you tried the code changes given by me?
0
 

Author Closing Comment

by:dyarosh
ID: 39285469
Based on your feedback I have started looking at the code and I believe the problem is with an ASP:RequiredFieldValidator that isn't coded to display the error in the Summary area.  I am in the process of narrowing down the culprit.  Thanks for your help and pointing me in the right direction.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question