webForumDet fria alternativet

Problem med sida som inte uppdateras

.NETur .NET

1 svar · 392 visningar · startad av Sanji

Medlem sedan juli 2009355 inlägg
Frågan#1

Hej! Jag har ett problem med en sida som jag bygger på just nu is asp.net, c#.

Jag har en tabell som byggs upp dynamiskt, dels med data från en databas och dels när användaren lägger in ny information. Jag har en knapp för att ta bort en rad i tabellen, och som samtidigt tar bort den datan i databasen. Problemet är att innehållet i tabellen på sidan inte hämtas om, men datan i databasen tas bort precis som den ska. ända sättet för mig att se ändringen på sidan är att ladda om den (F5). Finns det någon bra lösning till det här problemet?

Här är aspx-sidan:

<asp:Content ID="HeadContent" ContentPlaceHolderID="HeadContent" runat="server">

</asp:Content>
<asp:Content ID="MainContent" ContentPlaceHolderID="MainContent" runat="server">
    <asp:Panel CssClass="panel" ID="MainPanel" runat="server">
        <asp:DropDownList ID="Dropdownlistinquiries" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Dropdownlistinquiries_SelectedIndexChanged">
            <asp:ListItem value="-1" Selected="True">Choose an Inquiry:</asp:ListItem>
        </asp:DropDownList>
        <asp:Table ID="InquiryTable1" runat="server" BorderStyle="Solid" CellPadding="5" CssClass="center">
        <asp:TableHeaderRow CssClass="left"><asp:TableHeaderCell ColumnSpan="4"><h2><asp:Label runat="server" ID="ActionStatusLabel">Ny förfrågan</asp:Label></h2></asp:TableHeaderCell></asp:TableHeaderRow>
            <asp:TableRow>
                <asp:TableCell>Förfråganamn:</asp:TableCell>
                <asp:TableCell><asp:TextBox ID="addInquiryName" runat="server" /></asp:TableCell>
                <asp:TableCell>Ansvarig:</asp:TableCell>
                <asp:TableCell><asp:DropDownList runat="server" ID="addInquiryResponsible" /></asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell>Utskriftsdatum:</asp:TableCell>
                <asp:TableCell><asp:TextBox runat="server" ID="addInquiryPrintDate" /></asp:TableCell>
                <asp:TableCell>Svarsdatum:</asp:TableCell>
                <asp:TableCell><asp:TextBox runat="server" ID="addInquiryAnswerDate" /></asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell>Betalningsvilkor:</asp:TableCell>
                <asp:TableCell><asp:DropDownList ID="addInquiryPayterms" runat="server" /></asp:TableCell>
                <asp:TableCell>Fraktvilkor:</asp:TableCell>
                <asp:TableCell><asp:DropDownList ID="addInquiryShippingterms" runat="server" /></asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell ColumnSpan="2" CssClass="center">Kommentar till försäljare</asp:TableCell>
                <asp:TableCell ColumnSpan="2" CssClass="center">Egna kommentarer</asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell ColumnSpan="2"><asp:TextBox runat="server" ID="addInquiryComment" TextMode="MultiLine" Rows="8" Columns="35" /></asp:TableCell>
                <asp:TableCell ColumnSpan="2"><asp:TextBox runat="server" ID="addInquiryNotice" TextMode="MultiLine" Rows="8" Columns="35" /></asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell ColumnSpan="4">
                    
                    <% 
                        if (Dropdownlistinquiries.SelectedIndex != 0)
                        {
                    %>
                    <asp:Button runat="server" CssClass="button" ID="btnUpdateInquiry" Text="Spara" OnClick="btnUpdateInquiry_Click" />
                    <asp:Button runat="server" CssClass="button" ID="btnDownloadAsPdf" Text="Ladda ner" OnClick="btnDownloadAsPdf_Click" />
                    <a class="button" href="" onclick="javascript:window.open('PrintPage.aspx?inquiryID=<% Response.Write(Dropdownlistinquiries.SelectedValue); %>', 'Utskrift', 'resizable=0,width=595,height=842,scrollbars=1'); return false;">Skriv ut</a>
                    <%
                        }
                        else
                        {
                    %>
                    <asp:Button runat="server" CssClass="button" ID="btnAddInquiry" Text="Lägg till" OnClick="btnAddInquiry_Click" />
                    <%
                        } 
                    %>
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>

        <asp:Table ID="InquiryTableArticles" runat="server" CssClass="center">
            <asp:TableHeaderRow>
                <asp:TableHeaderCell>Artikelnummer</asp:TableHeaderCell>
                <asp:TableHeaderCell>Artikelnamn</asp:TableHeaderCell>
                <asp:TableHeaderCell>Antal</asp:TableHeaderCell>
                <asp:TableHeaderCell>Avrop</asp:TableHeaderCell>
                <asp:TableHeaderCell>Revisionsnummer</asp:TableHeaderCell>
                <asp:TableHeaderCell></asp:TableHeaderCell>
            </asp:TableHeaderRow>
            <asp:TableRow>
                <asp:TableCell ColumnSpan="2"><asp:DropDownList runat="server" ID="ddlAddInquiryArticleArticle" /></asp:TableCell>
                <asp:TableCell><asp:TextBox ID="txtAddInquiryArticlePcs" runat="server"></asp:TextBox></asp:TableCell>
                <asp:TableCell><asp:TextBox ID="txtAddInquiryArticleSuborder" runat="server"></asp:TextBox></asp:TableCell>                
                <asp:TableCell><asp:TextBox ID="txtAddInquiryArticleRevnumber" runat="server"></asp:TextBox></asp:TableCell>
                <asp:TableCell><asp:Button ID="btnInquiryArticleEdit" CssClass="button" Text="Lägg till" runat="server" OnClick="btnInquiryArticleEdit_Click" /></asp:TableCell>
            </asp:TableRow>
        </asp:Table>
    </asp:Panel>
</asp:Content>

här är codebehind filen:

public partial class Inquiries : System.Web.UI.Page
    {
        private bool WriteTable
        {
            get
            {
                if (ViewState["WriteTable"] == null || !(ViewState["WriteTable"] is bool))
                {
                    return false;
                }
                return (bool)ViewState["WriteTable"];
            }
            set { ViewState["WriteTable"] = value; }
        }
        

        private bool NewItem
        {
            get
            {
                if (ViewState["NewItem"] == null || !(ViewState["NewItem"] is bool))
                {
                    return true;
                }
                return (bool)ViewState["NewItem"];
            }
            set { ViewState["NewItem"] = value; }
        }

        #region Page_Load
        protected void Page_Load(object sender, EventArgs e)
        {          
            User currentUser = ((User)Session["user"]);
            if (!IsPostBack && NewItem)
            {
                addInquiryPrintDate.Text = DateTime.Now.ToShortDateString();
                DateTime TenDaysLater = DateTime.Now.AddDays(10);
                addInquiryAnswerDate.Text = TenDaysLater.ToShortDateString();

                foreach (Term t in InquiriesDatabase.Inquiries.GetBillingTermsByCompanyID(currentUser.CompanyID))
                {
                    addInquiryPayterms.Items.Add(new ListItem(t.Description, t.ID.ToString()));
                }
                foreach (Term tt in InquiriesDatabase.Inquiries.GetShippingTermsByCompanyID(currentUser.CompanyID))
                {
                    addInquiryShippingterms.Items.Add(new ListItem(tt.Description, tt.ID.ToString()));
                }
                foreach (User u in UsersDatabase.Users.GetUsersByCompanyID(currentUser.CompanyID))
                {
                    addInquiryResponsible.Items.Add(new ListItem(u.Name, u.ID.ToString()) { Selected = (u.ID == currentUser.ID) });
                }

                foreach (Inquiry i in InquiriesDatabase.Inquiries.GetInquiriesByCompany(currentUser.CompanyID))
                {
                    ListItem li = new ListItem(i.Name, i.ID.ToString());
                    Dropdownlistinquiries.Items.Add(li);
                }
                ddlAddInquiryArticleArticle.Items.Clear();
                Article[] temparticle = Database.ArticlesDatabase.Articles.GetArticlesByCompanyID(currentUser.CompanyID);
                foreach (Article b in ArticlesDatabase.Articles.GetArticlesByCompanyID(currentUser.CompanyID))
                {
                    ddlAddInquiryArticleArticle.Items.Add(new ListItem(b.ArticleNumber + " - (" + b.Term + ")", b.ID.ToString()));
                }
            }
            if (WriteTable)
            {
                DoWriteTable();
            }
        }
        #endregion
        #region Skriv ut tabellen
        private void DoWriteTable()
        {
            int articledropdownID = 0;
            if (Dropdownlistinquiries.SelectedIndex != 0)
            {
                Inquiry tempInquiry = InquiriesDatabase.Inquiries.GetInquiry(int.Parse(Dropdownlistinquiries.Items[Dropdownlistinquiries.SelectedIndex].Value));

                foreach (InquiryArticle ia in tempInquiry.Articles)
                {
                    TableCell tcArticleNumber = new TableCell();
                    TableCell tcPieces = new TableCell();
                    TableCell tcSuborder = new TableCell();
                    TableCell tcTerm = new TableCell();
                    TableCell tcRevisionNumber = new TableCell();
                    TableCell tcAction = new TableCell();
                    TableRow trInquiryArticle = new TableRow();

                    tcArticleNumber.Text = ia.Article.ArticleNumber;

                    tcPieces.Text = ia.Pieces.ToString();
                    tcSuborder.Text = ia.SubOrder.ToString();
                    tcTerm.Text = ia.Article.Term;
                    tcRevisionNumber.Text = ia.RevisionNumber;

                    trInquiryArticle.Cells.Add(tcArticleNumber);
                    trInquiryArticle.Cells.Add(tcTerm);
                    trInquiryArticle.Cells.Add(tcPieces);
                    trInquiryArticle.Cells.Add(tcSuborder);
                    trInquiryArticle.Cells.Add(tcRevisionNumber);

                    Button btnDeleteInquiryArticle = new Button();
                    btnDeleteInquiryArticle.Click += new EventHandler(btnDeleteInquiryArticle_Click);
                    btnDeleteInquiryArticle.Text = "Ta bort";
                    btnDeleteInquiryArticle.CssClass = "button";
                    btnDeleteInquiryArticle.ID = "btnDeleteInquiryArticle" + ia.ID.ToString();
                    
                    tcAction.Controls.Add(btnDeleteInquiryArticle);
                    trInquiryArticle.Cells.Add(tcAction);

                    InquiryTableArticles.Rows.Add(trInquiryArticle);

                    articledropdownID++;
                }
                Dropdownlistinquiries.SelectedIndex = Dropdownlistinquiries.SelectedIndex;
            }
        }
        #endregion
        #region Valt i dropdownlista (Inquiries)
        protected void Dropdownlistinquiries_SelectedIndexChanged(object sender, EventArgs e)
        {
            User currentUser = (User)Session["user"];
            if (Dropdownlistinquiries.SelectedIndex != 0)
            {
                Inquiry tempInquiry = InquiriesDatabase.Inquiries.GetInquiry(int.Parse(Dropdownlistinquiries.Items[Dropdownlistinquiries.SelectedIndex].Value));

                addInquiryComment.Text = tempInquiry.Comment;
                addInquiryName.Text = tempInquiry.Name;
                addInquiryNotice.Text = tempInquiry.Notice;
                addInquiryPrintDate.Text = tempInquiry.PrintDate;
                addInquiryAnswerDate.Text = tempInquiry.AnswerDate;

                addInquiryPayterms.Items.Clear();
                foreach (Term t in InquiriesDatabase.Inquiries.GetBillingTermsByCompanyID(currentUser.CompanyID))
                {
                    addInquiryPayterms.Items.Add(new ListItem(t.Description, t.ID.ToString()) { Selected = (tempInquiry.BillingTermID == t.ID) });
                }
                addInquiryShippingterms.Items.Clear();
                foreach (Term t in InquiriesDatabase.Inquiries.GetShippingTermsByCompanyID(currentUser.CompanyID))
                {
                    addInquiryShippingterms.Items.Add(new ListItem(t.Description, t.ID.ToString()) { Selected = (tempInquiry.ShippingTermID == t.ID) });
                }
                addInquiryResponsible.Items.Clear();
                foreach (User u in UsersDatabase.Users.GetUsersByCompanyID(currentUser.CompanyID))
                {
                    addInquiryResponsible.Items.Add(new ListItem(u.Name, u.ID.ToString()) { Selected = (tempInquiry.ResponsibleUserID == u.ID) });
                }

                DoWriteTable();

                ddlAddInquiryArticleArticle.Items.Clear();
                Article[] temparticle = Database.ArticlesDatabase.Articles.GetArticlesByCompanyID(currentUser.CompanyID);

                foreach (Article b in ArticlesDatabase.Articles.GetArticlesByCompanyID(currentUser.CompanyID))
                {
                    ddlAddInquiryArticleArticle.Items.Add(new ListItem(b.ArticleNumber + " - (" + b.Term + ")", b.ID.ToString()));
                }
                
                Dropdownlistinquiries.SelectedIndex = Dropdownlistinquiries.SelectedIndex;
                ActionStatusLabel.Text = "<h3>Redigera förfrågan</h3>";

                NewItem = false;
                WriteTable = true;
            }
            else
            {
                Response.Redirect("Inquiry.aspx");
            }

        }
        #endregion
        #region Lägg till Förfrågan
        protected void btnAddInquiry_Click(object sender, EventArgs e)
        {
            Inquiry tempInquiry = new Inquiry();

            tempInquiry.AnswerDate = addInquiryAnswerDate.Text;
            tempInquiry.BillingTermID = int.Parse(addInquiryPayterms.Text);
            tempInquiry.Comment = addInquiryComment.Text;
            tempInquiry.CompanyID = ((User)Session["user"]).CompanyID;
            tempInquiry.Name = addInquiryName.Text;
            tempInquiry.Notice = addInquiryNotice.Text;
            tempInquiry.PrintDate = addInquiryPrintDate.Text;
            tempInquiry.ResponsibleUserID = int.Parse(addInquiryResponsible.SelectedItem.Value);
            tempInquiry.ShippingTermID = int.Parse(addInquiryShippingterms.SelectedItem.Value);

            InquiriesDatabase.Inquiries.AddInquiry(tempInquiry);
            Response.Redirect("Inquiry.aspx");
        }
        #endregion
        #region Ändra förfrågan
        protected void btnUpdateInquiry_Click(object sender, EventArgs e)
        {
            Inquiry tempinquiry = new Inquiry();

            tempinquiry.AnswerDate = addInquiryAnswerDate.Text;
            tempinquiry.BillingTermID = int.Parse(addInquiryPayterms.Text);
            tempinquiry.Comment = addInquiryComment.Text;
            tempinquiry.CompanyID = ((User)Session["user"]).CompanyID;
            tempinquiry.Name = addInquiryName.Text;
            tempinquiry.Notice = addInquiryNotice.Text;
            tempinquiry.PrintDate = addInquiryPrintDate.Text;
            tempinquiry.ResponsibleUserID = int.Parse(addInquiryResponsible.SelectedItem.Value);
            tempinquiry.ShippingTermID = int.Parse(addInquiryShippingterms.SelectedItem.Value);
            tempinquiry.ID = int.Parse(Dropdownlistinquiries.SelectedItem.Value);

            InquiriesDatabase.Inquiries.UpdateInquiry(tempinquiry);
        }
        #endregion
        #region Lägg till InquiryArticle
        protected void btnInquiryArticleEdit_Click(object sender, EventArgs e)
        {
            InquiryArticle tempInquiryArticle = new InquiryArticle();

            tempInquiryArticle.Article.ID = int.Parse(ddlAddInquiryArticleArticle.SelectedValue);
            tempInquiryArticle.InquiryID = int.Parse(Dropdownlistinquiries.SelectedValue);
            tempInquiryArticle.Pieces = int.Parse(txtAddInquiryArticlePcs.Text);
            tempInquiryArticle.RevisionNumber = txtAddInquiryArticleRevnumber.Text;
            tempInquiryArticle.SubOrder = int.Parse(txtAddInquiryArticleSuborder.Text);

            InquiriesDatabase.Inquiries.AddInquiryarticle(tempInquiryArticle);

            DoWriteTable();
        }
        #endregion
        
        protected void btnDeleteInquiryArticle_Click(object sender, EventArgs e)
        {
            if (sender is Button)
            {
                Button btnSender = (Button)sender;
                int inquiryArticleID = int.Parse(btnSender.ID.Replace("btnDeleteInquiryArticle", ""));

                InquiriesDatabase.Inquiries.DeleteInquiryArticle(inquiryArticleID);
                WriteTable = true;

                InquiryTableArticles.Rows.Clear();
                DoWriteTable();
            }
        }

        #region Download as PDF

        protected void btnDownloadAsPdf_Click(object sender, EventArgs e)
        {
            User currentUser = ((User)Session["user"]);
            Company userCompany = CompaniesDatabase.Companies.GetCompany(currentUser.CompanyID);
            if (Dropdownlistinquiries.SelectedIndex > 0)
            {
                string urlToConvert = "http://" + Request.ServerVariables["SERVER_NAME"] + "/Memberpages/PrintPage.aspx?inquiryID=" + Dropdownlistinquiries.SelectedValue;

                PdfConverter pdfConverter = new PdfConverter();
              
                pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4;
                pdfConverter.PdfDocumentOptions.PdfCompressionLevel = PdfCompressionLevel.Normal;
                pdfConverter.PdfDocumentOptions.PdfPageOrientation = PDFPageOrientation.Portrait;
                pdfConverter.PdfDocumentOptions.GenerateSelectablePdf = false;
                pdfConverter.PdfDocumentInfo.AuthorName = userCompany.Name;

                byte[] pdfBytes = pdfConverter.GetPdfBytesFromUrl(urlToConvert);

                // send the PDF document as a response to the browser for download
                System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
                response.Clear();
                response.AddHeader("Content-Type", "binary/octet-stream");
                response.AddHeader("Content-Disposition",
                    "attachment; filename=ConversionResult.pdf; size=" + pdfBytes.Length.ToString());
                response.Flush();
                response.BinaryWrite(pdfBytes);
                response.Flush();
                response.End();
            }
        }

        #endregion
    }

Ursäkta mig om det blev lite mycket kod, jag vet bara inte vad jag ska göra... jag har suttit med det här problemet i en hel dag nu :(

Medlem sedan juli 2009355 inlägg
#2

Jag löste det helt enkelt genom att ladda om sidan... jag orkar inte krångla mer

145 ms totalt · 3 externa anrop · v20260731065814-full.b746b907
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
142 ms — hämta tråd, inlägg och bilagor (db)