jestem swiezy w asp.net i mam taka zgrywostke...
potrzebuje zrobic cos takiego: uzywajac updatepanel potrzebuje miec mozliwosc dodawania textboxow i ich pozniejszego usuwania, czyli klikam butona "dodaj" i w updatepanelu dodaje mi sie textbox wraz z butonem do jego pozniejszego usuniecia
na stronce umiescilem tez zwykly przycisk "postback" ktory symuluje tylko i wylacznie postback w celach testowych(narazie)
info o butonach trzymam narazie w sesji -> docelowo w viewstate
ale zaczely sie schody
jak to wszystko odpale i zrobie taka kombinacje:
1. otworze strone z "paska adresu" - swieza strona otwierana pierwszy raz
2. dodam - nacisne "dodaj"
3.odswieze - f5 w przegladarce albo przycisk "odswiez" w przegladarce
4. klikne na butona "postback" to sie stronka wywala i malomowiacy blad z serwera dostaje - zrzut ponizej; albo jak klikne na butona "dodaj" zamiast "postback" to sie nic nie dzieje
jak zapobiedz takim kwiatkom jak user odswiezy sobie strone w ten sposob - najlepiej zeby po wcisnieciu f5 w przegladarce strona zaladowala sie od nowa, jak za pierwszym razem
teraz pytania zwiazane z samym cyklem zycie stronki w asp.net
rozumiem ze jak klikne przycisk "dodaj" to robi sie postback tylko w updatepanelu, wywoluje sie funkcja page_load w ktorej tworze poprzednio stworzone textboxy i pozniej wywoluje sie funkcja obslugujaca event klikniecia butona "dodaj", w ktorej zwiekszam numer butona dla potrzeb nadania unikalnego id i wsadzam nowego butona do updatepanela i zapamietuje numer nowego butona w sesji/viewstate - czyli to wszystko dzieje sie jeszcze zanim stronka zostanie zrenderowana
to teraz w takim raize pytanie o usuwanie
jakos nie jestem w stanie zrobic zeby usuwanie bylo w funkcji obslugujacej klikniecie na butona do usuwania
udalo mi sie to obsluzyc wykrywajac klikniecie danego butona "usun" w metodzie page_load gdzie z listy z numerami butonow usuwam butona o danym numerze i on sie poprostu nie tworzy
o ile w funkcji obslugujacej klikniecie na butona potrafie wykryc ktory buton zostal klikniety, usunac z listy numerow textboxow odpowiedni numer to nie wiem jak programistycznie usunac textboxa i inne elementy z nim zwiazane
da sie rozwiazac moje problemy? ;)
kod ponizej:
AutoCompleteExtender powylaczalem, zakomentowalem
Default.aspx
Kod: Zaznacz cały
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="act" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head id="Head1" runat="server">
<title>UpdateProgress Tutorial</title>
</head>
<body>
<form id="form" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager" runat="server">
<Services>
<asp:ServiceReference Path="AutoComplete.asmx" />
</Services>
</asp:ScriptManager>
<asp:Button ID="dodajNazwa" runat="server" Text="Dodaj" onclick="dodajNazwa_Click" />
<asp:Button ID="Button1" runat="server" Text="PostBack" />
<asp:UpdatePanel ID="upNazwa" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:PlaceHolder ID="phNazwa" runat="server">
<asp:Panel ID="panelNazwa1" runat="server">
<asp:TextBox runat="server" ID="nazwa1" Width="300" autocomplete="off" EnableViewState="False" />
<act:AutoCompleteExtender
runat="server"
ID="autoCompleteNazwa1"
TargetControlID="nazwa1"
ServicePath="AutoComplete.asmx"
ServiceMethod="GetCompletionList"
MinimumPrefixLength="1"
CompletionInterval="100"
EnableCaching="False"
CompletionSetCount="20"
Enabled="False"
FirstRowSelected="True" />
</asp:Panel>
</asp:PlaceHolder>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="dodajNazwa" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<br />
<hr />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
plik cs
Kod: Zaznacz cały
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using AjaxControlToolkit;
using System.Web.Extensions;
public partial class _Default : System.Web.UI.Page
{
protected ArrayList NazwaInfo
{
get { return (ArrayList)(Session["NazwaInfo"] ?? new ArrayList()); }
set { Session["NazwaInfo"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Session["NazwaInfo"] = null;
}
ArrayList NazwaInfo = this.NazwaInfo;
Label1.Text = "";
Label2.Text = "";
if(Page.IsAsync==true)
Label1.Text += " isasync ";
if (Page.IsCallback == true)
Label1.Text += " iscallback ";
if (Page.IsPostBack == true)
Label1.Text += " ispostback ";
if (upNazwa.IsInPartialRendering == true)
Label1.Text += " isinpartialrendering";
if (ScriptManager.IsInAsyncPostBack == true)
Label1.Text += " isasyncpostback "+ScriptManager.AsyncPostBackSourceElementID;
if (Page.IsAsync == false)
Label1.Text += " !!isasync ";
if (Page.IsCallback == false)
Label1.Text += " !!iscallback ";
if (Page.IsPostBack == false)
Label1.Text += " !!ispostback ";
if (upNazwa.IsInPartialRendering == false)
Label1.Text += " !!isinpartialrendering";
if (ScriptManager.IsInAsyncPostBack == false)
Label1.Text += " !!isasyncpostback " + ScriptManager.AsyncPostBackSourceElementID;
if (Page.IsAsync == true)
Label2.Text += " isasync ";
if (Page.IsCallback == true)
Label2.Text += " iscallback ";
if (Page.IsPostBack == true)
Label2.Text += " ispostback ";
if (upNazwa.IsInPartialRendering == true)
Label2.Text += " isinpartialrendering";
if (ScriptManager.IsInAsyncPostBack == true)
Label2.Text += " isasyncpostback " + ScriptManager.AsyncPostBackSourceElementID;
if (Page.IsAsync == false)
Label2.Text += " !!isasync ";
if (Page.IsCallback == false)
Label2.Text += " !!iscallback ";
if (Page.IsPostBack == false)
Label2.Text += " !!ispostback ";
if (upNazwa.IsInPartialRendering == false)
Label2.Text += " !!isinpartialrendering";
if (ScriptManager.IsInAsyncPostBack == false)
Label2.Text += " !!isasyncpostback " + ScriptManager.AsyncPostBackSourceElementID;
if (ScriptManager.GetCurrent(this).AsyncPostBackSourceElementID.StartsWith("usunNazwa"))
{
string usunNazwa = "usunNazwa";
string id = ScriptManager.GetCurrent(this).AsyncPostBackSourceElementID;
int usunNr = Int32.Parse(id.Substring(usunNazwa.Length, id.Length - usunNazwa.Length));
NazwaInfo.Remove(usunNr);
}
for (int i = 0; i < NazwaInfo.Count; i++)
{
TextBox tbNazwa = new TextBox();
tbNazwa.ID = "nazwa" + NazwaInfo[i].ToString();
tbNazwa.EnableViewState = false;
((IAttributeAccessor)tbNazwa).SetAttribute("autocomplete", "off");
Panel panelNazwa = new Panel();
panelNazwa.ID = "panelNazwa" + NazwaInfo[i].ToString();
/*
AutoCompleteExtender ac = new AutoCompleteExtender();
ac.ID = "autoCompleteNazwa" + NazwaInfo[i].ToString();
ac.TargetControlID = "nazwa" + NazwaInfo[i].ToString();
ac.ServicePath = "AutoComplete.asmx";
ac.ServiceMethod = "GetCompletionList";
ac.MinimumPrefixLength = 1;
ac.CompletionInterval = 100;
ac.EnableCaching = false;
ac.CompletionSetCount = 20;
ac.Enabled = true;
ac.FirstRowSelected = true;
*/
Button usun = new Button();
usun.ID = "usunNazwa" + NazwaInfo[i].ToString();
usun.Text = "usun" + NazwaInfo[i];
//usun.Click += new EventHandler(usun_Click);
panelNazwa.Controls.Add(tbNazwa);
// panelNazwa.Controls.Add(ac);
panelNazwa.Controls.Add(usun);
phNazwa.Controls.Add(panelNazwa);
}
}
protected void dodajNazwa_Click(object sender, EventArgs e)
{
ArrayList NazwaInfo = this.NazwaInfo;
int nowyNr=1;
if (NazwaInfo.Count != 0)
nowyNr = (int)NazwaInfo[NazwaInfo.Count - 1];
nowyNr++;
NazwaInfo.Add(nowyNr);
TextBox tbNazwa = new TextBox();
tbNazwa.ID = "nazwa"+ nowyNr.ToString();
tbNazwa.EnableViewState = false;
((IAttributeAccessor)tbNazwa).SetAttribute("autocomplete", "off");
/*
AutoCompleteExtender ac = new AutoCompleteExtender();
ac.ID = "autoCompleteNazwa" + nowyNr.ToString();
ac.TargetControlID = "nazwa" + nowyNr.ToString();
ac.ServicePath = "AutoComplete.asmx";
ac.ServiceMethod = "GetCompletionList";
ac.MinimumPrefixLength = 1;
ac.CompletionInterval = 100;
ac.EnableCaching = false;
ac.CompletionSetCount = 20;
ac.Enabled = true;
ac.FirstRowSelected = true;*/
Button usun = new Button();
usun.ID = "usunNazwa" + nowyNr.ToString();
usun.Text = "usun" + nowyNr;
usun.Click += new EventHandler(usun_Click);
Panel panelNazwa = new Panel();
panelNazwa.ID = "panelNazwa" + nowyNr.ToString();
panelNazwa.Controls.Add(tbNazwa);
// panelNazwa.Controls.Add(ac);
panelNazwa.Controls.Add(usun);
phNazwa.Controls.Add(panelNazwa);
this.NazwaInfo = NazwaInfo;
//upNazwa.Update();
}
void usun_Click(object sender, EventArgs e)
{
/*
string usunNazwa = "usunNazwa";
string id = ((Button)sender).ID.ToString();
if (id.StartsWith(usunNazwa))
{
int usunNr = Int32.Parse(id.Substring(usunNazwa.Length, id.Length - usunNazwa.Length));
ArrayList NazwaInfo = this.NazwaInfo;
NazwaInfo.Remove(usunNr);
this.NazwaInfo = NazwaInfo;
phNazwa.Controls.Remove(phNazwa.FindControl("nazwa" + id));
phNazwa.Controls.Remove(phNazwa.FindControl("usunNazwa" + id));
phNazwa.Controls.Remove(phNazwa.FindControl("panelNazwa" + id));
}
*/
}
}
Błąd serwera w aplikacji '/test'.
Informacje o stanie są nieprawidłowe dla tej strony i mogą być uszkodzone.
Opis: Podczas wykonywania bieżącego żądania sieci Web wystąpił nieobsługiwany wyjątek. Aby uzyskać dodatkowe informacje o błędzie i miejscu jego występowania w kodzie, przejrzyj ślad stosu.
Szczegóły wyjątku: System.Web.HttpException: Informacje o stanie są nieprawidłowe dla tej strony i mogą być uszkodzone.
Błąd źródła:
[Brak istotnych wierszy źródłowych]
Plik źródłowy: c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\test\08487438\3f64ba7e\App_Web_q83pveis.0.cs Wiersz: 0
Ślad stosu:
[ViewStateException: Nieprawidłowy stan wyświetlania.
Client IP: 127.0.0.1
Port: 4558
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.12) Gecko/20070508 Firefox/1.5.0.12
ViewState: /wEWBAKQ6NZ3Au/zn+MDAoznisYGAu/b7M0EzcmKA/emVb4i2ua+zI8bZ8xI61A=
Referer: http://localhost/test/Default.aspx
Path: /test/Default.aspx]
[HttpException (0x80004005): Informacje o stanie są nieprawidłowe dla tej strony i mogą być uszkodzone.]
System.Web.UI.ViewStateException.ThrowError(Exception inner, String persistedState, String errorPageMessage, Boolean macValidationError) +267
System.Web.UI.ClientScriptManager.EnsureEventValidationFieldLoaded() +2133067
System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument) +66
System.Web.UI.Control.ValidateEvent(String uniqueID, String eventArgument) +108
System.Web.UI.WebControls.TextBox.LoadPostData(String postDataKey, NameValueCollection postCollection) +36
System.Web.UI.WebControls.TextBox.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection) +12
System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +353
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +7350
System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +213
System.Web.UI.Page.ProcessRequest() +86
System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +18
System.Web.UI.Page.ProcessRequest(HttpContext context) +49
ASP.default_aspx.ProcessRequest(HttpContext context) in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\test\08487438\3f64ba7e\App_Web_q83pveis.0.cs:0
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +358
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +64
Informacje o wersji: Wersja Microsoft .NET Framework:2.0.50727.1433; Wersja ASP.NET:2.0.50727.1433