Solved

OnClick event is not firing on asp.net web page

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

18 Experts available now in Live!

Get 1:1 Help Now