簡體   English   中英

動態html的意外行為通過C#呈現包含數據的表

[英]Unexpected behaviour of dynamic html rendering a table with data via C#

我遇到了一個不熟悉的功能問題。 我認為它與循環的范圍有關,在渲染頁面時與服務器端代碼操作/操作有關。

假設我想重復一個表行 - 每個主持一個文本輸入,行和它們的文本框根據DATABASE“綁定”數據的內容呈現值。

一切都完美無缺,直到添加更多要求 - READONLY屬性和事件鍵(javascript小驗證任務)。

否則它確實有效,通過兩個分開的字符串交替行,我用字符串格式“注入”,條件是如果行計數是奇數甚至是偶數,那么我試圖過濾一些列以使keypress事件綁定到js函數和另一個屬性為字符串。

如果字符串為空,那么如果滿足條件,則元素“declaration”的結尾部分將為空,然后為該字符串賦值“ReadOnly”,並為js字符串分配keypress事件“調用函數代碼”。

這是代碼。 這種情況很奇怪,因為style屬性,當前列的信息,列名稱,一切都按預期運行,但那兩個READONLY屬性和event鍵(javascript小驗證任務)沒有。

呈現動態表格代碼

這是前面的代碼,后面的c#代碼主要用於(盡可能保留一些代碼客戶端)

`ControlsInteraction.WithTable.Design()`

`ControlsInteraction.WithTable.ExtractData()`

正在處理列名和值的渲染和轉換的動態函數

int count = 0;
bool TblOk = DebugTests.Sesseion.SeSn.Raised(DebugTests.Flag.HT_DB_CPA_Table_init_Complete);
    if (TblOk)
    {
        string TextBxRendr = "";//holds Renderd <TD> base String-code

        string AltrnatBgColor;
        string NoAttribute = "";
        string Js_NumericKprss = "onkeypress=\"return onlN(event)\""
        string ReadOnly = "READONLY";
        var TimesCol = ALLTablesDataSet.Tables[Tbl1.TableName].Columns;
     string DtrawTbl1 = Tbl1.TableName;
     ControlsInteraction.WithTable.Design Tbldz = 
     new ControlsInteraction.WithTable.Design();

     ControlsInteraction.WithTable.ExtractData DtExtrct =
     new ControlsInteraction.WithTable.ExtractData();

        foreach (System.Data.DataRow TimesRow in ALLTablesSet.Tables[DtrawTbl].Rows)
        {

         AltrnatBgColor= Tbldz.RowsBGColorAlternate(RDE.DataRowToInt(TimesRow, "RecordNum"),true);
         altBgColOnly = Tbldz.RowsBGColorAlternate(RDE.DataRowToInt(TimesRow, "RecordNum"), false);
        Response.Write(string.Format("<tr {0}>",AltrnatBgColor));

                for (int i = 0; i < TimesRow.ItemArray.Length; i++)
                {
                        if (i != (TimesRow.ItemArray.Length - 1))
                        {


                        Js_NumericKprss = "onkeypress=\"return onlN(event)\"";

                   //asking for: current row will Not be read only via its name
                   if (DtExtrct.CurrRowIs(TimesRow, MyDBSchema.DBs_Cols.TblCPAReport.Comments, DtExtrct.DataRowToInt(TimesRow, "RecordNum")))
                                Js_NumericKprss = NoAttribute; // same goes with the other manipulation i've needed to implement on each column
                        TextBxRendr = string.Format(
                        "<td><input type='text' id=\"{0}_{1}\" value=\"{2} \" style=\"width:50px;{3} border:none; \" class=\"RepTblDataTDs\" {4} {5} \\></td>", 
                        TimesCol[i], TimesRow["RecordNum"], TimesRow[i], AltrnatBgColor,Js_NumericKprss,ReadOnly
                                                    );

                        }

                        else
                        {
                        TextBxRendr = string.Format(
        "<td><input type='image' id=\"{0}_{1}\" src=\"images/Save.png\" style=\"width:25px;{2}\" style=\"width:25px\" onclick=\"UbpdateTblCPA(this, {1});\" /></td>",
            "img",i + 1, AltrnatBgColor
                            );
                          }


                        Response.Write(TextBxRendr);
                        count++;
                 }

        }
}

是正確注入和只讀部分READONLY屬性,和事件鍵 - (javascript小驗證任務)

無論是全部還是無功能

我究竟做錯了什么?

回答我自己的問題,最近答案是

...好吧一切,包括@Patrics評論都錯了

我只能說好好關注:如何使用DataTable DataRow,DataTable DataColumns以及關系和foreach變量范圍使用你的visual sudio調試器在每一行檢查你的代碼值

我沒有時間重命名變量,但如果你需要從數據庫表中創建一個動態的html表,這就是方法

   foreach (System.Data.DataRow TimesRow in ALLTablesSet.Tables[DrawTbl].Rows)
    {
        recordNum = RDE.DataRowToInt(TimesRow, "RecordNum");
        AltBgCol = Tbldz.RowsBGColorAlternate(RDE.DataRowToInt(TimesRow, "RecordNum"), true);
        altBgColOnly = Tbldz.RowsBGColorAlternate(RDE.DataRowToInt(TimesRow, "RecordNum"), false);
        Response.Write(string.Format("<tr {0}>", AltBgCol));
        for (int i = 0; i < TimesRow.ItemArray.Length; i++)
        {


            if (i != (TimesRow.ItemArray.Length - 1))
            {
                Js_NumericKprss = "onkeypress=\"return onlN(event)\""; ReadOnly = "";
                if (RDE.CurrRowIs(TimesRow, HentalDBSchema.HTDB_Cols.TblTimeCPAReport.Comments, i))
                {
                    Js_NumericKprss = ""; ReadOnly = "";
                }

                else if (RDE.CurrRowIs(TimesRow, HentalDBSchema.HTDB_Cols.TblTimeCPAReport.Fines, i)
                 || RDE.CurrRowIs(TimesRow, MyDBSchema.DBs_Cols.TblCPAReport.PhoneExpences, i)
                 || RDE.CurrRowIs(TimesRow, MyDBSchema.DBs_Cols.TblCPAReport.SalaryPerDay, i)
                 || RDE.CurrRowIs(TimesRow, MyDBSchema.DBs_Cols.TblCPAReport.SalaryPerMonth, i)
                 || RDE.CurrRowIs(TimesRow, MyDBSchema.DBs_Cols.TblCPAReport.TotalGrossWages, i)
                 || RDE.CurrRowIs(TimesRow, MyDBSchema.DBs_Cols.TblCPAReport.TravelFee, i))
                {
                    ReadOnly = "";
                    Js_NumericKprss = "onkeypress=\"return onlN(event)\"";
                }
                else
                    ReadOnly = "READONLY";

                TxtRndr = string.Format("<td><input type='text' id=\"{0}_{1}\" value=\"{2} \" style=\"width:50px;{3} border:none; \" class=\"RepTblDataTDs\" {5} {6} \\></td>{4}", TimesCol[i], TimesRow["RecordNum"], TimesRow[i], altBgColOnly, Environment.NewLine + "\t\t\t", Js_NumericKprss, ReadOnly);
            }

            else
            {
                TxtRndr = string.Format("<td><input type='image' id=\"{0}_{1}\" src=\"images/Save.png\" style=\"width:25px;{3}\" style=\"width:25px\" onclick=\"UbpdateTblCPA(this, {1});\" /></td>{4}", "imgBut", i + 1, TimesRow[i], altBgColOnly, Environment.NewLine + "\t\t\t");
            }


            Response.Write(TxtRndr);




            count++;
        }

    }

我正在添加我所做的所有研究,以便更容易在data提取和我已經工作的一些更多方法,所以如果你想使用它隨意...

    public class ControlsInteraction
    {
        public class WithDDL
        {
            public class GetSelVal
            {
                public string AsString(DropDownList DDLToCollectValusFrom)
                {

                    return DDLToCollectValusFrom.SelectedValue;
                }
                public int AsInt(DropDownList DDLToCollectValusFrom)
                { 
                    if(DDLToCollectValusFrom.SelectedValue != null)
                    return Convert.ToInt32(DDLToCollectValusFrom.SelectedValue);
                    return 666;
                }
            }
            public List<string> GetListItems_Values(DropDownList DDLToCollectValusFrom)
            {
                List<string> LST_DDLValues = new List<string>();
                foreach (ListItem item in DDLToCollectValusFrom.Items)
                {
                    LST_DDLValues.Add(item.Value);


                }
                return LST_DDLValues;
            }
            public List<string> GetListItems_Text(DropDownList DDLToCollectTextFrom)
            {
                List<string> LST_DDLTEXT = new List<string>();
                foreach (ListItem item in DDLToCollectTextFrom.Items)
                {
                    LST_DDLTEXT.Add(item.Text);


                }
                return LST_DDLTEXT;
            }
        }
        public static class WithPlcHldr
        {
            public static void AddCtrl(PlaceHolder PlcHldrID, Control CntrID)
            {
                PlcHldrID.Controls.Add(CntrID);
            }
        }
        public class WithTable
        {
            public class Design
            {
                public string RowsBGColorAlternate(int RowCounter, bool AddWithStyleAsStandAlone = false)
                {
                    string BgCol = ""; bool bgclaltrnator;
                    if (RowCounter > 0)
                    {
                        RowCounter++;
                        bgclaltrnator = (RowCounter % 2) == 0;
                        if (bgclaltrnator)
                            BgCol = "#70878F";
                        else BgCol = "#E6E6B8";
                    }
                    if (AddWithStyleAsStandAlone)
                    return string.Format("style=\"background-color:{0};\"", BgCol);
                    return string.Format("background-color:{0};", BgCol);
                }
            }
            public class ExtractData
            {
                public string ColumnValueFromCurrRow(DataRow DtRow, string RequestedColName)
               {
                    return "";
                }
                public string DataRows_ColumnToString(DataRow Data_RowToActOn, string keyColName)
                {
                    var tmp = Data_RowToActOn[keyColName];
                    return Data_RowToActOn[keyColName].ToString();

                }
                public int DataRowToInt(DataRow Data_RowToActOn, string keyColName)
                {
                    string tmp = Data_RowToActOn[keyColName].ToString();
                    return Convert.ToInt32(tmp);

                }
                public bool CurrColumnIs(DataColumn Data_RowToQuestion, string ColumnName)
                {
                    string tmp = Data_RowToQuestion.ToString();
                    return tmp == ColumnName;
                }
                public bool CurrRowIs(DataRow Data_RowToQuestion, string RowName, int CurrIndex)
                {
                    string ColsName = Data_RowToQuestion.Table.Columns[CurrIndex].ToString();
                    return ColsName == RowName;
                    //this is curent value - by index 

                    //string currentColumn = Data_RowToQuestion.ItemArray[CurrIndex].ToString();

                }
            }

        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM