?
Solved

OnClick event is not firing on asp.net web page

Posted on 2013-06-26
4
Medium Priority
?
748 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
  • 2
  • 2
4 Comments
 
LVL 13

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 13

Accepted Solution

by:
Jitendra Patil earned 1500 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Integration Management Part 2
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month16 days, 19 hours left to enroll

862 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