Solved

OnClick event is not firing on asp.net web page

Posted on 2013-06-26
4
721 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 12

Expert Comment

by:Jitendra Patil
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now