Solved

OnClick event is not firing on asp.net web page

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Replace &lt; with < 14 59
Checkbox in a dynamic GridVIew is not clickable? 5 26
mail.To.Add 5 26
asp.net input html code have it display the content 2 12
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 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