RE: Erzeugung dynamischer Controls - Beim Postback Daten weg
- From: plongo <plongo@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 12 Jan 2006 05:21:02 -0800
Ich kann es zwar net ganz glauben, aber ich habe jetzt einfach im Page_Load
im else-Zweig geschrieben (if not Page.IsPostBack....) das er mir die Tabelle
nochmal erneut erstellen soll. Also ruf ich einfach nochmal CreateTable auf.
Die Einträge in den Feldern stehen dann immer noch drin. Prima....
Und dafür habe ich jetzt stunden gesucht..... ARGH
Gruss plongo
P.S: Die Idee das Control erneut in den ViewState zu laden, hätte da nix
gebracht? Weiss ja nicht ob das so ohne weiteres funktioniert. Würde mich mal
intressieren!!
"plongo" wrote:
> Hallo Forum,
>
> ich stehe vor einem kleinen Problem.
>
> Ich habe mir ein eigenes UserControl geschrieben. Dieses UserControl enthält
> 4 Textbox und 2 HiddenInputField.
>
> Diese UserControl binde ich dynamisch zur Laufzeit an eine Tabelle (bzw.
> Tabellenzelle). Diese Tabelle soll mir später eine Eingabe der Anwesenheit
> von Personen innerhalb 1 ausgewählten Monats ermöglichen. D.h. das
> UserControl wird Xmal benutzt/erstellt.
>
> Siehe hierzu: Hatte dazu schonmal paar Fragen.
> http://www.aspforum.de/topic.asp?TOPIC_ID=28572
> http://www.aspforum.de/topic.asp?TOPIC_ID=28588
>
> Jetzt möchte ich später die eingegeben Daten abfragen und in der DB
> speichern. Dafür habe ich extra eine Button eingerichtet. Wenn ich nun auf
> diesen klicke, soll er die Controls durchlaufen und dann die Werte später in
> der DB eintragen. Problem ist nur, sobald ich auf den Button klicke, ist
> meine Tabelle mit den Controls nicht mehr vorhanden und somit kann ich
> natürlich keine Daten abfragen.
>
> Die Frage ist, wie kann ich es am besten verhindern, das die Tabelle mit den
> Controls + Daten noch vorhanden sind, beim Klicken auf den Button. Meine
> Befürchtung ist, das die Controls ja dynamisch erstellt werden und somit auch
> nicht dem ViewState bekannt sind. Kann ich dies irgendwie beeinflussen? Ist
> mal ne Vermutung von mir bzgl. des Viewstate!?
>
> Damit ihr nicht auf dem trocken liegt, gebe ich mal den relevanten Code
> frei. Der für das Erstellen der Tabelle + Usercontrols zuständig ist. Die
> Prozedur für den Button ist auch mit drin.
>
> 0001 Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
> 0002 Try
> 0003 '// Pruefen auf Berechtigung
> 0004 Call Check()
> 0005
> 0006 '// Kein Postback
> 0007 if not Page.IsPostBack then
> 0008 '// Nicht leer?
> 0009 if oLib.Nz(Request.QueryString("PalId"), true) = false and _
> 0010 oLib.Nz(Request.QueryString("j"), true) = false and _
> 0011 oLib.Nz(Request.QueryString("m"), true) = false then
> 0012
> 0013 '// String/Zahl?
> 0014 if oLib.CheckString(Replace(Request.QueryString("PalId"), ";", ""),
> "^([\w]*)$") = true and _
> 0015 IsNumeric(Request.QueryString("j")) and _
> 0016 IsNumeric(Request.QueryString("m")) then
> 0017
> 0018 '// UserControl - Eigenschaften setzen
> 0019 hdPerson.Value = Request.QueryString("PalId")
> 0020 hdMonat.Value = Request.QueryString("m")
> 0021 hdJahr.Value = Request.QueryString("j")
> 0022 CreateTable()
> 0023 end if
> 0024 end if
> 0025 end if
> 0026
> 0027 Catch Err As ThreadAbortException
> 0028 '// Do Nothing
> 0029
> 0030 Catch Err As Exception
> 0031 Library.LogFile(Err, "Page_Load", "AnwSimpleList")
> 0032 Call oLib.OnErrorAbortApp()
> 0033 End Try
> 0034 End Sub 'Page_Load
> 0035
> 0036 Public Sub btnSend_Click(ByVal sender As Object, ByVal e As EventArgs)
> Handles btnSend.Click
> 0037 Try
> 0038 Dim intCount As Integer
> 0039 Dim arrPaPeId As String()
> 0040 Dim strId, strtPaBezAnzStd, strtPaAb, strtPaBis, strtPaBem,
> strhdPaDatum, strhdPaPeId As String
> 0041
> 0042 intEndMonat = DateTime.DaysInMonth(hdJahr.Value, hdMonat.Value)
> 0043 arrPaPeId = Split(hdPerson.Value, ";")
> 0044
> 0045 '// Durchlaufen der uebergebenen Personen
> 0046 For intCount = 0 To arrPaPeId.Length-1
> 0047 '// Durchlaufen der Monate
> 0048 For intCountMonat = 1 To intEndMonat
> 0049 '// Id setzen
> 0050 strId = arrPaPeId(intCount) & intCountMonat.ToString &
> hdMonat.Value.ToString & hdJahr.Value.ToString
> 0051
> 0052 '// Controls finden
> 0053 'strtPaBezAnzStd = (CType(Page.FindControl("tPaBezAnzStd" & strId),
> TextBox)).Text
> 0054 'strhdPaDatum = (CType(Page.FindControl("hdPaDatum" & strId),
> HtmlInputHidden)).Value
> 0055 'strhdPaPeId = (CType(Page.FindControl("hdPaPeId" & strId),
> HtmlInputHidden)).Value
> 0056
> 0057 '// Optionale Controls finden, pruefen ob vorhanden sind
> 0058 'if oLib.Nz(Page.FindControl("tPaAb" & strId)) = false then strtPaAb =
> (CType(Page.FindControl("tPaAb" & strId), TextBox)).Text
> 0059 'if oLib.Nz(Page.FindControl("tPaBis" & strId)) = false then strtPaBis
> = (CType(Page.FindControl("tPaBis" & strId), TextBox)).Text
> 0060 'if oLib.Nz(Page.FindControl("tPaBem" & strId)) = false then strtPaBem
> = (CType(Page.FindControl("tPaBem" & strId), TextBox)).Text
> 0061
> 0062 '// Testausgabe
> 0063 'lbAnw.Text &= strId & "<br />" 'strtPaBezAnzStd & " - " &
> strhdPaDatum & " - " & strhdPaPeId & strtPaAb & " - " & strtPaBis & " - " &
> strtPaBem & "<br /><br />"
> 0064 Next
> 0065 Next
> 0066
> 0067 '// Weiterleitung
> 0068 'Response.Redirect("bestaetigung.aspx?" &
> Request.ServerVariables("QUERY_String"))
> 0069
> 0070 Catch Err As ThreadAbortException
> 0071 '// Do Nothing
> 0072
> 0073 Catch Err As Exception
> 0074 Library.LogFile(Err, "btnSend_Click", "AnwSimpleList")
> 0075 Call oLib.OnErrorAbortApp()
> 0076 End Try
> 0077 End Sub 'btnSend_Click
> 0078
> 0079 Public Sub CreateTable()
> 0080 Try
> 0081 intEndMonat = DateTime.DaysInMonth(hdJahr.Value, hdMonat.Value)
> 0082 lbMonthYear.Text = hdJahr.Value & ", " & MonthName(hdMonat.Value)
> 0083
> 0084 '// Mehrere oder einzelne Person?
> 0085 if UBound(Split(hdPerson.Value, ";")) >= 1 then
> 0086 CreateMultiTable()
> 0087 else
> 0088 CreateSingleTable()
> 0089 end if
> 0090
> 0091 Catch Err As ThreadAbortException
> 0092 '// Do Nothing
> 0093
> 0094 Catch Err As Exception
> 0095 Library.LogFile(Err, "CreateTable", "AnwSimpleList")
> 0096 Call oLib.OnErrorAbortApp()
> 0097 End Try
> 0098 End Sub 'CreateTable
> 0099
> 0100 Private Sub CreateSingleTable()
> 0101 Try
> 0102 Dim intCountCell As Integer
> 0103 Dim datTag As DateTime
> 0104
> 0105 '// Text festlegen (Name der Person)
> 0106 lbAnw.Text = "Anwesenheit: " & GetName(hdPerson.Value) & " (" &
> hdPerson.Value & ")<br /><br />"
> 0107
> 0108 '// Werte setzen der Tabelle
> 0109 taAnw.CellPadding = 5
> 0110 taAnw.CellSpacing = 0
> 0111
> 0112 '// Tabellenzeilen (<tr>)
> 0113 For intCountMonat = 1 To intEndMonat
> 0114 '// Deklaration
> 0115 Dim trAnw As New TableRow()
> 0116
> 0117 '// Tabellenzellen (<td>)
> 0118 For intCountCell = 1 To 2
> 0119 '// Deklaration
> 0120 Dim tcAnw As New TableCell()
> 0121
> 0122 '// Datum festlegen
> 0123 datTag = New DateTime(hdJahr.Value, hdMonat.Value, intCountMonat)
> 0124
> 0125 '// Von\Bis Datum der Person ermitteln
> 0126 GetPersDate(hdPerson.Value, datPersVon, datPersBis)
> 0127
> 0128 Select Case intCountCell
> 0129 Case 1
> 0130 '// Werte festlegen
> 0131 tcAnw.Width = Unit.Percentage(15)
> 0132 tcAnw.Text = datTag.ToShortDateString & " "
> 0133 tcAnw.CssClass = "h3"
> 0134
> 0135 Case 2
> 0136 '// Deklaration
> 0137 Dim otmpTagCtl As AnwTagCtl =
> CType(LoadControl("/base/usercontrol/AnwTagCtl.ascx"), AnwTagCtl)
> 0138
> 0139 '// Werte fuer Tag-Control festlegen
> 0140 GetInfo(hdPerson.Value, datTag, otmpTagCtl)
> 0141 otmpTagCtl.cId = hdPerson.Value & Replace(datTag.ToShortDateString,
> ".", "")
> 0142 otmpTagCtl.cVisible = true
> 0143 otmpTagCtl.Person = hdPerson.Value
> 0144 otmpTagCtl.Tag = New DateTime(hdJahr.Value, hdMonat.Value,
> intCountMonat).ToShortDateString
> 0145 otmpTagCtl.SetControls()
> 0146
> 0147 '// Control anfuegen an Tabellenzelle
> 0148 tcAnw.Controls.Add(otmpTagCtl)
> 0149
> 0150 '// Werte festlegen
> 0151 tcAnw.Width = Unit.Percentage(85)
> 0152 End Select
> 0153
> 0154 '// An Tabellenzeile anfuegen
> 0155 trAnw.Cells.Add(tcAnw)
> 0156 Next
> 0157
> 0158 '// Hintergrundfarbe
> 0159 if (intCountMonat MOD 2 = 0) then trAnw.BackColor =
> System.Drawing.Color.FromArgb(197, 214, 227)
> 0160
> 0161 '// An Tabelle anfuegen
> 0162 taAnw.Rows.Add(trAnw)
> 0163 Next
> 0164
> 0165 Catch Err As ThreadAbortException
> 0166 '// Do Nothing
> 0167
> 0168 Catch Err As Exception
> 0169 Library.LogFile(Err, "CreateSingleTable", "AnwSimpleList")
> 0170 Call oLib.OnErrorAbortApp()
> 0171 End Try
> 0172 End Sub 'CreateSingleTable
> 0173
> 0174 Private Sub CreateMultiTable()
> 0175 Try
> 0176 '// Deklaration
> 0177 Dim intCountRow, intCountCell As Integer
> 0178 Dim datTag As DateTime
> 0179 Dim arrPaPeId As String()
> 0180
> 0181 '// Text festlegen
> 0182 lbAnw.Text = "Anwesenheit eintragen zu folgenden Personen:<br /><br />"
> 0183
> 0184 '// Werte setzen der Tabelle
> 0185 taAnw.CellPadding = 0
> 0186 taAnw.CellSpacing = 2
> 0187
> 0188 '// Id's splitten
> 0189 arrPaPeId = Split(hdPerson.Value, ";")
> 0190
> 0191 '// Tabellenzeile (<tr>)
> 0192 For intCountRow = -1 To arrPaPeId.Length-1
> 0193 '// Deklaration
> 0194 Dim trAnw As New TableRow()
> 0195 Dim trEmpty As New TableRow()
> 0196 Dim tcEmpty As New TableCell()
> 0197
> 0198 '// Tabellenzelle (<td>)
> 0199 For intCountMonat = 0 To intEndMonat
> 0200 '// Deklaration
> 0201 Dim tcAnw As New TableCell()
> 0202
> 0203 '// Erste Zeile (Ausgabe der Tage)
> 0204 if intCountRow = -1 then
> 0205 '// Beginn ab Tag 1
> 0206 if intCountMonat > 0 then
> 0207 tcAnw.Text = intCountMonat
> 0208 tcAnw.HorizontalAlign = HorizontalAlign.Center
> 0209 tcAnw.BackColor = System.Drawing.Color.FromArgb(197, 214, 227)
> 0210 tcAnw.CssClass = "h3"
> 0211 end if
> 0212
> 0213 '// Ausgabe der Personen
> 0214 else
> 0215 '// Erste Zelle
> 0216 if intCountMonat = 0 then
> 0217 tcAnw.Text = GetName(arrPaPeId(intCountRow)) & " "
> 0218 tcAnw.CssClass = "h3"
> 0219 tcAnw.BackColor = System.Drawing.Color.FromArgb(197, 214, 227)
> 0220 else
> 0221 '// Deklaration
> 0222 Dim otmpTagCtl As AnwTagCtl =
> CType(LoadControl("/base/usercontrol/AnwTagCtl.ascx"), AnwTagCtl)
> 0223
> 0224 '// Datum festlegen
> 0225 datTag = New DateTime(hdJahr.Value, hdMonat.Value, intCountMonat)
> 0226
> 0227 '// Von\Bis Datum der Person ermitteln
> 0228 GetPersDate(arrPaPeId(intCountRow), datPersVon, datPersBis)
> 0229
> 0230 '// Werte fuer Tag-Control festlegen
> 0231 GetInfo(arrPaPeId(intCountRow), datTag, otmpTagCtl)
> 0232 otmpTagCtl.cId = arrPaPeId(intCountRow) &
> Replace(datTag.ToShortDateString, ".", "")
> 0233 otmpTagCtl.cVisible = false
> 0234 otmpTagCtl.Person = arrPaPeId(intCountRow)
> 0235 otmpTagCtl.Tag = datTag.ToShortDateString
> 0236 otmpTagCtl.SetControls()
> 0237
> 0238 '// Control anfuegen an Tabellenzelle
> 0239 tcAnw.Controls.Add(otmpTagCtl)
> 0240 end if
> 0241 end if
> 0242
> 0243 '// An Tabellenzeile anfuegen
> 0244 trAnw.Cells.Add(tcAnw)
> 0245 Next
> 0246
> 0247 '// An Tabelle anfuegen
.
- Follow-Ups:
- Re: Erzeugung dynamischer Controls - Beim Postback Daten weg
- From: Günter Prossliner
- Re: Erzeugung dynamischer Controls - Beim Postback Daten weg
- Prev by Date: Re: Schnelle Kompilierung von ASP.net 2 Webseiten (Precompilation abschalten)
- Next by Date: Re: andere Anwendung ausfuehren
- Previous by thread: Re: andere Anwendung ausfuehren
- Next by thread: Re: Erzeugung dynamischer Controls - Beim Postback Daten weg
- Index(es):