Benutzer mit den meisten Antworten
Portierung von ASP.net 2.0 auf 4.0: Function aus Page.Master gibt keinen Wert zurück

Frage
-
Hallo zusammen
Eine Seite aus meiner Website sol einen Datensatz löschen.
Über die Function "deleteCondition()" aus der Page.Master soll eine Sicherheits-Abfrage warnen.
Mit "OK" soll ein "Yes" an den Aufrufer, zurückgegeben werden. Man landet dabei immer in der "Page_Load()" Function.
Dort is der erwartete Rückgabewert aber immer "", also leer.
Die Function aus Page.Master:
function deleteCondition() { var ans; ans=window.confirm('Möchten Sie diesen Zustand wirlich löschen?'); //alert (ans); if (ans==true) { //alert('Yes'); document.aspnetForm.hdnbox.value = 'Yes'; } else { //alert('No'); document.aspnetForm.hdnbox.value='No'; } return document.aspnetForm.hdnbox.value; }
Die "Page_Load() Function:
protected void Page_Load(object sender, EventArgs e) { //Session Control Response.Cache.SetNoStore(); if (Session["AdminMode"] == null) Response.Redirect("login.aspx"); customerBO = (CustomerBO)Session["ADMCustomer"]; if (customerBO == null) { Response.Redirect("ADMkundenvowahlen.aspx"); Context.ApplicationInstance.CompleteRequest(); } //Tx_PageIndex.Text = Gv_Condition.PageIndex.ToString(); ((Label)Master.FindControl("Lb_Welcome")).Text = "(ADM) Kunde: " + customerBO.name; ((Label)Master.FindControl("Lb_Welcome")).Visible = true; userCustomer = customerBO.userCustomer; if (!IsPostBack) { DataTable dt = conditionBC.getCondition("", "", "00010101", "00010101", Tx_QtdItems.Text, customerBO.id); Gv_Condition.DataSource = dt; Gv_Condition.DataBind(); sortDT = dt; } else { string resp = Request.Form["hdnbox"].ToString(); if (resp.Equals("Yes")) { deleteResp = true; } } }
Weiss bitte jemand Rat?
Grüsse
Helmut
Antworten
-
Hallo zusammen,
inzwischen habe ich doch noch die Lösung gefunden.
Leider waren es gleich drei Fehler hintereinandergeschaltet.
Daher auch der missverständliche Thread-Titel.
Hier nur die zwei wichtigsten Änderungen:Als Javascript musste ich die Function "document.aspnetForm.hdnbox.value='No';"
gegen "document.getElementById('hdnbox').value = 'Yes';"
ersetzen. Also auch entsprechende andere Aufrufe.
Dann benötigt man für das Gridview folgende Anweisung :
protected void Gv_Condition_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView gv = (GridView)sender;
GridViewRow gvr = gv.Rows[e.NewEditIndex];
e.Cancel = true; // Das hatte bei mir gefehlt. Unter .net 2.0 wars nicht nötig.
Pn_UpdateCondition.Visible = true;
TableCell tc = gvr.Cells[0];
tc = gvr.Cells[1];....
Habe nun auch einen link dazu gefunden.
Z.B.: https://social.msdn.microsoft.com/Forums/windows/en-US/7471dd6e-aced-4131-875b-ff2c645adbd9/how-to-disable-editing-in-the-existing-rows-in-a-datagridview-and-allow-the-user-to-add-new-row-to?forum=winformsdatacontrolsDanke an alle, die sich das durchgelesen haben.
Vielleicht hilft es ja einem anderen.
Alle Antworten
-
Ich hatte noch die aspx Seite nicht beachtet.
Der dortige Llink sieht folgendermassen aus:
<asp:Button ID="Bn_Delete" runat="server" Text="Löschen" OnClick="Bn_Delete_Click" OnClientClick="deleteCondition()" /><br />
Also eigentlich müsste die C# Funktion "Bn_Delete()" aufgerufen werden.
Das passiert aber nicht. Man bleibt in der Page_Load() Funktion hängen.
Und das bei jedem Klick auf einen Button.
Unter .NET 2.0 wurde die "Page_Load()" nur einmal durchlaufen.
Beim Klick auf "Löschen" kam zuerst das JavaScript dann die Lösche Function. zuerst die Page_Load() durchlaufen.
Wenn ich das JavaScript rausnehme, wird der Datensatz auch tatsächlich gelöscht.
Aber auch da wird die Page_Load() zuerst durchlaufen.
Und wenn ich mal auf "Abbrechen" klicke kommt eine Exception:
[HttpException (0x80004005): Die GridView Gv_Condition löste das Ereignis RowCancelingEdit aus, das nicht behandelt wurde.] System.Web.UI.WebControls.GridView.OnRowCancelingEdit(GridViewCancelEditEventArgs e) +2416891 System.Web.UI.WebControls.GridView.HandleCancel(Int32 rowIndex) +54 System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +1026 System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument) +201 System.Web.UI.WebControls.GridView.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +15 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +9884278 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639
Was ist unter .Net 4.0 anders?
Diese Meldung kommt wohl daher, dass sich dieses Grid-Control plötzlich selbstständig macht.
Nachdem ich das Element gelöscht habe (Das Javascript habe ich raus genommen.) öffnet sich das Grid-Control plötzlich im Bearbeitungs-Modus. Man kann die Zellen editieren,
Das war unter .NEt 2.0 nich!
Es kommt dann auch der "Abbrechen"-Button, den es vorher auch nicht gab. Den gibt es auch nirgends in meinem Quellcode.
- Bearbeitet Helmut19 Freitag, 1. März 2019 19:15
-
Ich setz hier nochmal den gesamten Quellcode der Seite rein:
Im aspx: (Teil mit Löschen-Button, danach Teil des GridViews)
</table> <asp:Label ID="Tx_PageIndex" runat="server" MaxLength="50" Visible="True"></asp:Label> <asp:Button ID="Bt_Update" runat="server" Text="Aktualisieren" OnClick="Bt_Update_Click" /> <asp:Button ID="Bn_Delete" runat="server" Text="Löschen" OnClick="Bn_Delete_Click" /><br /> <span style="color: #ffffff"> <asp:Label ID="Lb_Id" runat="server" Visible="False" Width="21px" ForeColor="WhiteSmoke"></asp:Label></span><br /> <asp:Label ID="Lb_Empty" runat="server" Text=" "></asp:Label> </asp:Panel> <asp:GridView ID="Gv_Condition" runat="server" AutoGenerateColumns="False" HtmlEncode="False" CellPadding="4" Font-Size="11pt" ForeColor="#333333" OnRowDataBound="GridView1_RowEvent" GridLines="None" OnSorting="GridView1_Sorting" AllowSorting="True" HorizontalAlign="Left" Width="830px" EmptyDataText="Keine Daten gefunden" OnRowEditing="Gv_Condition_RowEditing" AllowPaging="True" OnPageIndexChanging="Gv_Condition_PageIndexChanging" PageSize="30"> <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <Columns> <asp:BoundField DataField="machine_id" HeaderText="ID" SortExpression="ID" Visible="False" > <ItemStyle HorizontalAlign="Center" Font-Size="10pt" /> <HeaderStyle Font-Size="10pt" HorizontalAlign="Center" Wrap="False" /> </asp:BoundField> <asp:BoundField DataField="condition_id" HeaderText="Interne ID" SortExpression="condition_id" NullDisplayText=" " Visible="True"> <ItemStyle Font-Size="10pt" /> <HeaderStyle Font-Size="10pt" HorizontalAlign="Center" /> </asp:BoundField> <asp:BoundField DataField="InventoryNumber" HeaderText="Inventar-Nummer" SortExpression="InventoryNumber" NullDisplayText=" " > <ItemStyle HorizontalAlign="Center" Font-Size="10pt" /> <HeaderStyle Font-Size="10pt" HorizontalAlign="Center" Wrap="False" /> </asp:BoundField>
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class ADMzustand : System.Web.UI.Page { private DBConnection DBconn = new DBConnection(); private CustomerBO customerBO; private ConditionBC conditionBC = new ConditionBC(); private MachineBC machineBC = new MachineBC(); private static DataTable sortDT = null; private static string lastSorted = SortDirection.Ascending.ToString(); private static string idToDelete = ""; private bool deleteResp = false; protected string userCustomer; protected void Page_Load(object sender, EventArgs e) { //Session Control Response.Cache.SetNoStore(); if (Session["AdminMode"] == null) Response.Redirect("login.aspx"); customerBO = (CustomerBO)Session["ADMCustomer"]; if (customerBO == null) { Response.Redirect("ADMkundenvowahlen.aspx"); Context.ApplicationInstance.CompleteRequest(); } //Tx_PageIndex.Text = Gv_Condition.PageIndex.ToString(); ((Label)Master.FindControl("Lb_Welcome")).Text = "(ADM) Kunde: " + customerBO.name; ((Label)Master.FindControl("Lb_Welcome")).Visible = true; userCustomer = customerBO.userCustomer; if (!IsPostBack) { DataTable dt = conditionBC.getCondition("", "", "00010101", "00010101", Tx_QtdItems.Text, customerBO.id); Gv_Condition.DataSource = dt; Gv_Condition.DataBind(); sortDT = dt; } else { string resp = Request.Form["hdnbox"].ToString(); if (resp.Equals("Yes")) { deleteResp = true; } } } protected void Bt_Redefine_Click(object sender, EventArgs e) { this.clearPanel(); Lb_Erro.Text = ""; DateTime dateFrom = new DateTime(); DateTime dateUntil = new DateTime(); if (!Tx_DateFrom.Text.Equals("")) { dateFrom = conditionBC.getDateFormat(Tx_DateFrom.Text); } if (!Tx_DateUntil.Text.Equals("")) { dateUntil = conditionBC.getDateFormat(Tx_DateUntil.Text); } //Lb_Erro.Text = dateFrom.ToString("yyyy-MM-dd") + " / " + dateUntil.ToString("yyyy-MM-dd"); //Lb_Erro.Text = conditionBC.getCondition(Tx_InventaryNumber.Text, Tx_MachineName.Text, dateFrom.ToString("yyyy-MM-dd"), dateUntil.ToString("yyyy-MM-dd"), Tx_QtdItems.Text, customerBO.id); DataTable dt = conditionBC.getCondition(Tx_InventaryNumber.Text, Tx_MachineName.Text, dateFrom.ToString("yyyyMMdd"), dateUntil.ToString("yyyyMMdd"), Tx_QtdItems.Text, customerBO.id); Gv_Condition.DataSource = dt; Gv_Condition.PageIndex = 0; Gv_Condition.DataBind(); sortDT = dt; } protected void GridView1_RowEvent(object sender, GridViewRowEventArgs e) { string rowId = ""; string rowPhMin = ""; string rowPhMax = ""; string rowKonzMin = ""; string rowKonzMax = ""; string rowNiMin = ""; string rowNiMax = ""; string rowNaMin = ""; string rowNaMax = ""; string rowPh = ""; string rowKonz = ""; string rowNi = ""; if (e.Row.RowType == DataControlRowType.DataRow) { //lRowId = Convert.ToUInt32(e.Row.Cells[0].Text); DataRowView rowView = (DataRowView)e.Row.DataItem; rowId = rowView["machine_id"].ToString(); if (rowId != "") { DataSet ds = DBconn.executeSelectQuery(QueryRepositorie.getParameterByIdMachine(rowId)); DataTable dt = ds.Tables[0]; if (dt.Rows.Count > 0) { rowPhMin = dt.Rows[0]["pH_MinLim"].ToString(); rowPhMin = rowPhMin.Trim(); if (string.IsNullOrEmpty(rowPhMin)) rowPhMin = "0"; rowPhMin = rowPhMin.Replace(".", ","); rowPhMax = dt.Rows[0]["pH_MaxLim"].ToString(); rowPhMax = rowPhMax.Trim(); if (string.IsNullOrEmpty(rowPhMax)) rowPhMax = "0"; rowPhMax = rowPhMax.Replace(".", ","); rowKonzMin = dt.Rows[0]["Concentration_MinLim"].ToString(); rowKonzMin = rowKonzMin.Trim(); if (string.IsNullOrEmpty(rowKonzMin)) rowKonzMin = "0"; rowKonzMin = rowKonzMin.Replace(".", ","); rowKonzMax = dt.Rows[0]["Concentration_MaxLim"].ToString(); rowKonzMax = rowKonzMax.Trim(); if (string.IsNullOrEmpty(rowKonzMax)) rowKonzMax = "0"; rowKonzMax = rowKonzMax.Replace(".", ","); rowNiMin = dt.Rows[0]["Nitrite_MinLim"].ToString(); rowNiMin = rowNiMin.Trim(); if (string.IsNullOrEmpty(rowNiMin)) rowNiMin = "0"; rowNiMin = rowNiMin.Replace(".", ","); rowNiMax = dt.Rows[0]["Nitrite_MaxLim"].ToString(); rowNiMax = rowNiMax.Trim(); if (string.IsNullOrEmpty(rowNiMax)) rowNiMax = "0"; rowNiMax = rowNiMax.Replace(".", ","); rowNaMin = dt.Rows[0]["Nitrate_MinLim"].ToString(); rowNaMin = rowNaMin.Trim(); if (string.IsNullOrEmpty(rowNaMin)) rowNaMin = "0"; rowNaMin = rowNaMin.Replace(".", ","); rowNaMax = dt.Rows[0]["Nitrate_MaxLim"].ToString(); rowNaMax = rowNaMax.Trim(); if (string.IsNullOrEmpty(rowNaMax)) rowNaMax = "0"; rowNaMax = rowNaMax.Replace(".", ","); rowPh = e.Row.Cells[5].Text; rowPh = rowPh.Trim(); if (string.IsNullOrEmpty(rowPh)) rowPh = "0"; rowPh = rowPh.Replace(".", ","); rowKonz = e.Row.Cells[6].Text; rowKonz = rowKonz.Trim(); if (string.IsNullOrEmpty(rowKonz)) rowKonz = "0"; rowKonz = rowKonz.Replace(".", ","); rowNi = e.Row.Cells[7].Text; rowNi = rowNi.Trim(); if (string.IsNullOrEmpty(rowNi)) rowNi = "0"; rowNi = rowNi.Replace(".", ","); //-------------------------------------------------------- if (Convert.ToDecimal(rowPh) < Convert.ToDecimal(rowPhMin)) { e.Row.Cells[5].ForeColor = System.Drawing.Color.FromName("#f94646"); } else if (Convert.ToDecimal(rowPh) > Convert.ToDecimal(rowPhMax)) { e.Row.Cells[5].ForeColor = System.Drawing.Color.FromName("#4242ff"); } else { e.Row.Cells[5].ForeColor = System.Drawing.Color.FromName("#12b41e"); } //-------------------------------------------------------- if (Convert.ToDecimal(rowKonz) < Convert.ToDecimal(rowKonzMin)) { e.Row.Cells[6].ForeColor = System.Drawing.Color.FromName("#f94646"); } else if (Convert.ToDecimal(rowKonz) > Convert.ToDecimal(rowKonzMax)) { e.Row.Cells[6].ForeColor = System.Drawing.Color.FromName("#4242ff"); } else { e.Row.Cells[6].ForeColor = System.Drawing.Color.FromName("#12b41e"); } //-------------------------------------------------------- if (Convert.ToDecimal(rowNi) < Convert.ToDecimal(rowNiMin)) { e.Row.Cells[7].ForeColor = System.Drawing.Color.FromName("#f94646"); } else if (Convert.ToDecimal(rowNi) > Convert.ToDecimal(rowNiMax)) { e.Row.Cells[7].ForeColor = System.Drawing.Color.FromName("#4242ff"); } else { e.Row.Cells[7].ForeColor = System.Drawing.Color.FromName("#12b41e"); } //-------------------------------------------------------- } } } } protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { DataView dv = new DataView(sortDT); if (lastSorted == SortDirection.Ascending.ToString()) { dv.Sort = e.SortExpression + " " + "DESC"; lastSorted = SortDirection.Descending.ToString(); } else { dv.Sort = e.SortExpression + " " + "ASC"; lastSorted = SortDirection.Ascending.ToString(); } Gv_Condition.DataSource = dv; Gv_Condition.DataBind(); } protected void Gv_Condition_RowEditing(object sender, GridViewEditEventArgs e) { GridView gv = (GridView)sender; GridViewRow gvr = gv.Rows[e.NewEditIndex]; Pn_UpdateCondition.Visible = true; TableCell tc = gvr.Cells[0]; tc = gvr.Cells[1]; Lb_InventaryNumber.Text = "Inventar - Nummer: "; Lb_Id.Text = tc.Text; tc = gvr.Cells[4]; Tx_Datum.Text = tc.Text; Tx_Datum.Text = Tx_Datum.Text.Trim(); tc = gvr.Cells[5]; Tx_pH.Text = tc.Text; Tx_pH.Text = Tx_pH.Text.Trim(); tc = gvr.Cells[6]; Tx_Concentration.Text = tc.Text; Tx_Concentration.Text = Tx_Concentration.Text.Trim(); tc = gvr.Cells[7]; Tx_Nitrit.Text = tc.Text; Tx_Nitrit.Text = Tx_Nitrit.Text.Trim(); tc = gvr.Cells[8]; Tx_Nitrat.Text = tc.Text; Tx_Nitrat.Text = Tx_Nitrat.Text.Trim(); tc = gvr.Cells[9]; Tx_Kss.Text = tc.Text; Tx_Kss.Text = Tx_Kss.Text.Trim(); tc = gvr.Cells[10]; Tx_Oil.Text = tc.Text; Tx_Oil.Text = Tx_Oil.Text.Trim(); tc = gvr.Cells[11]; Tx_Notes.Text = tc.Text; Tx_Notes.Text = replaceSpecialChar(Tx_Notes.Text); //Tx_Notes.Text = replaceSpecialChar("s"); Gv_Condition.SelectedIndex = e.NewEditIndex; } private string replaceSpecialChar(string line) { string newLine = ""; bool flagChar = false; string asciiCode = ""; newLine = line; for (int i = 0; i < line.Length; i++) { if (line[i].Equals('&') && line[i+1].Equals('#')) { flagChar = true; } if (flagChar) { asciiCode = asciiCode + line[i].ToString(); } if (line[i].Equals(';') && flagChar) { flagChar = false; //get the ascII code string asciiStr = asciiCode.Replace("#", ""); asciiStr = asciiStr.Replace("&", ""); asciiStr = asciiStr.Replace(";", ""); char c = (char)Int32.Parse(asciiStr); //replace in the line newLine = newLine.Replace(asciiCode, c.ToString()); asciiCode = ""; } } return (newLine); } protected void Bt_Update_Click(object sender, EventArgs e) { string id = Lb_Id.Text; string date = conditionBC.invertDate(Tx_Datum.Text); string result = conditionBC.updateCondition(id, date, Tx_pH.Text.Replace(",", "."), Tx_Concentration.Text.Replace(",", "."), Tx_Nitrit.Text.Replace(",", "."), Tx_Nitrat.Text.Replace(",", "."), Tx_Kss.Text, Tx_Oil.Text, Tx_Notes.Text); if (result.Equals("1")) { Lb_Erro.Text = "Record updated"; Bt_Redefine_Click(null, null); // warum sollte das ausgeführt werden??? // if (Tx_PageIndex.Text != "") // Gv_Condition.PageIndex = Int32.Parse(Tx_PageIndex.Text); } } protected void Bn_Delete_Click(object sender, EventArgs e) { idToDelete = Lb_Id.Text; if (deleteResp) { string resp = conditionBC.deleteCondition(idToDelete); if (resp.Equals("1")) { Lb_Erro.Text = "Record deleted."; Bt_Redefine_Click(null, null); } } } private void clearPanel() { Gv_Condition.SelectedIndex = -1; Pn_UpdateCondition.Visible = false; Tx_Datum.Text = ""; Tx_Notes.Text = ""; Lb_Id.Text = ""; Tx_pH.Text = ""; Tx_Concentration.Text = ""; Tx_Nitrit.Text = ""; Tx_Nitrat.Text = ""; Tx_Oil.Text = ""; Tx_Kss.Text = ""; } protected void Gv_Condition_PageIndexChanging(object sender, GridViewPageEventArgs e) { Gv_Condition.PageIndex = e.NewPageIndex; Gv_Condition.DataSource = sortDT; Gv_Condition.DataBind(); } }
- Bearbeitet Helmut19 Freitag, 1. März 2019 19:48
-
Hallo zusammen,
inzwischen habe ich doch noch die Lösung gefunden.
Leider waren es gleich drei Fehler hintereinandergeschaltet.
Daher auch der missverständliche Thread-Titel.
Hier nur die zwei wichtigsten Änderungen:Als Javascript musste ich die Function "document.aspnetForm.hdnbox.value='No';"
gegen "document.getElementById('hdnbox').value = 'Yes';"
ersetzen. Also auch entsprechende andere Aufrufe.
Dann benötigt man für das Gridview folgende Anweisung :
protected void Gv_Condition_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView gv = (GridView)sender;
GridViewRow gvr = gv.Rows[e.NewEditIndex];
e.Cancel = true; // Das hatte bei mir gefehlt. Unter .net 2.0 wars nicht nötig.
Pn_UpdateCondition.Visible = true;
TableCell tc = gvr.Cells[0];
tc = gvr.Cells[1];....
Habe nun auch einen link dazu gefunden.
Z.B.: https://social.msdn.microsoft.com/Forums/windows/en-US/7471dd6e-aced-4131-875b-ff2c645adbd9/how-to-disable-editing-in-the-existing-rows-in-a-datagridview-and-allow-the-user-to-add-new-row-to?forum=winformsdatacontrolsDanke an alle, die sich das durchgelesen haben.
Vielleicht hilft es ja einem anderen.