2
Answers

Problems with postbacks, visible/hidden controls

M Larsen

M Larsen

14y
2.1k
1

Greetings,
I am having some unusual issues with web forms on my public-facing internet servers.  I can run everything fine both locally and on my internal intranet servers.  I have forms that show or hide table rows depending on what step you are on in the form.  This form reads and writes to a SQL database via a Web Service.  I have eliminated AJAX or JavaScript issues by creating a simple test form that is showing the same erronious behavior...
Web Service:
 

using System;
using System.IO;
using System.Web.Services;
using System.Xml;
using taExternalTestTableAdapters;

namespace nsExternalTest
{
    [WebService(Namespace = "nsExternalTest")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class ExternalTest : System.Web.Services.WebService
    {
        public ExternalTest()
        {
        }
        [WebMethod]
        public int GetEmpID(string empLogin)
        {
            EmpTableTableAdapter taEmp = new EmpTableTableAdapter();
            return Convert.ToInt32(taEmp.GetEmpID(empLogin));
        }
        [WebMethod]
        public bool InsertMessage(string message, string empID)
        {
            bool success = true;
            try
            {
                TestTableTableAdapter taTestTable = new TestTableTableAdapter();
                taTestTable.Insert(Convert.ToInt32(empID), message);
            }
            catch (Exception)
            {
                success = false;
            }
            return success;
        }
        [WebMethod]
        public string GetMyMessages(int empID)
        {
            StringWriter sw = new StringWriter();
            XmlTextWriter writer = new XmlTextWriter(sw);
            writer.Formatting = Formatting.None;
            writer.WriteStartElement("TestTableMessages");
            try
            {
                TestTableTableAdapter taTestTable = new TestTableTableAdapter();
                taExternalTest.TestTableDataTable dtTestTable = new taExternalTest.TestTableDataTable();
                taTestTable.FillByEmpID(dtTestTable, empID);
                foreach (taExternalTest.TestTableRow row in dtTestTable)
                {
                    writer.WriteStartElement("TestMessage");
                    writer.WriteAttributeString("Message", row.TestMessage);
                    writer.WriteEndElement();
                }
            }
            catch (Exception ex)
            {
                writer.WriteStartElement("Error");
                writer.WriteAttributeString("message", ex.ToString());
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
            return sw.ToString();
        }
    }

ASPX form Code:
 

 <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ExternalTester.ascx.cs"
    Inherits="Controls_ExternalTester" %>
<table>
    <tr id="trGetEmpID" runat="server">
        <td>
            <table>
                <tr>
                    <td>
                        <h2>
                            Get Employee ID</h2>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="btnGetEmpID" runat="server" Text="Get Employee ID" OnClick="btnGetEmpID_Click" />
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <tr id="trInsertMessage" runat="server">
        <td>
            <table>
                <tr>
                    <td colspan="2">
                        <h2>
                            Add a Message</h2>
                    </td>
                </tr>
                <tr>
                    <td>
                        Employee ID:
                    </td>
                    <td>
                        <asp:Label ID="lblEmpID" runat="server" ForeColor="Blue" Font-Bold="true"></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td>
                        New Message:
                    </td>
                    <td>
                        <asp:TextBox ID="txtNewMessage" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:Button ID="btnAddMessage" runat="server" Text="Add Message" OnClick="btnAddMessage_Click" />
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:CheckBox ID="chkShowView" runat="server" Text="Show Message Button" OnCheckedChanged="chkShowView_CheckedChanged"
                            AutoPostBack="True" />
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:Button ID="btnViewMessages" runat="server" Text="View Your Messages" OnClick="btnViewMessages_Click" />
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <tr id="trGetMyMessages" runat="server">
        <td>
            <table>
                <tr>
                    <td>
                        <h2>
                            Message List</h2>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:GridView ID="grdMessages" runat="server">
                        </asp:GridView>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="btnAddAnother" runat="server" Text="Add Another Message" OnClick="btnAddAnother_Click" />
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>

Code-behind:
 

 using System;
using System.Data;
using System.IO;
public partial class Controls_ExternalTester : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            resetForm();
        }
    }
    private void resetForm()
    {
        trGetEmpID.Visible = true;
        trInsertMessage.Visible = false;
        trGetMyMessages.Visible = false;
    }
    private void resetAddMessage()
    {
        trGetEmpID.Visible = false;
        trInsertMessage.Visible = true;
        trGetMyMessages.Visible = false;
        lblEmpID.Text = "";
        txtNewMessage.Text = "";
        chkShowView.Checked = false;
        btnViewMessages.Visible = false;
        ExternalServiceTest.ExternalTest extTest = new ExternalServiceTest.ExternalTest();
        int empID = extTest.GetEmpID("mlarsen");
        lblEmpID.Text = empID.ToString();
    }
    private void resetMessageList()
    {
        int empID = Convert.ToInt32(lblEmpID.Text);
        trGetEmpID.Visible = false;
        trInsertMessage.Visible = false;
        trGetMyMessages.Visible = true;
        ExternalServiceTest.ExternalTest extTest = new ExternalServiceTest.ExternalTest();
        StringReader sr = new StringReader(extTest.GetMyMessages(empID));
        DataSet dsMember = new DataSet();
        dsMember.ReadXml(sr);
        grdMessages.DataSource = dsMember;
        grdMessages.DataBind();
    }
    protected void btnGetEmpID_Click(object sender, EventArgs e)
    {
        resetAddMessage();
    }
    protected void chkShowView_CheckedChanged(object sender, EventArgs e)
    {
        if (chkShowView.Checked)
        {
            btnViewMessages.Visible = true;
        }
        else
        {
            btnViewMessages.Visible = false;
        }
    }
    protected void btnAddMessage_Click(object sender, EventArgs e)
    {
        ExternalServiceTest.ExternalTest extTest = new ExternalServiceTest.ExternalTest();
        if (extTest.InsertMessage(txtNewMessage.Text, lblEmpID.Text))
        {
            txtNewMessage.Text = "";
        }
    }
    protected void btnViewMessages_Click(object sender, EventArgs e)
    {
        resetMessageList();
    }
    protected void btnAddAnother_Click(object sender, EventArgs e)
    {
        resetAddMessage();
    }
}

The form starts by showing the "Get Employee ID" section, hiding the other two sections.  I click it and it shows the "Add a Message" section, and seems to retrieve the Employee ID field and populate the label correctly.  However, at this point, the "Show Message Button" checkbox is supposed to post-back to unhide the "View Your Message" button, but instead, it unhides everything on the form, and clears out the Employee ID that it retrieved earlier.  None of the controls on the form work as expected at this point.
Like I said, the form works fine locally and on my intranet servers.  Also, all of my forms were working fine on my external servers until about a month ago when they suddenly began exhibiting this strange behavior.  Nothing was installed, patched, or updated on these servers when this started happening.  If you have seen this before or have any ideas of things I could try to fix this problem, I will be greatly appreciative.  Thank you!
 
Matt
Answers (2)
Next Recommended Forum