简体   繁体   English

动态html的意外行为通过C#呈现包含数据的表

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

I'm encountering an unfamiliar problem with functionality. 我遇到了一个不熟悉的功能问题。 I think it has something to do with scope of a loop, and server-side code operations/manipulation when rendering a page. 我认为它与循环的范围有关,在渲染页面时与服务器端代码操作/操作有关。

Say I want to repeat a Table Row - each hosts a text input, rows and their textboxes are rendered with values according to content of DATABASE "binded" Data. 假设我想重复一个表行 - 每个主持一个文本输入,行和它们的文本框根据DATABASE“绑定”数据的内容呈现值。

Everything works perfectly until more requirements are added - READONLY Attribute And event Key (javascript small validation task). 一切都完美无缺,直到添加更多要求 - READONLY属性和事件键(javascript小验证任务)。

Otherwise it does work, alternating rows via two separated strings that I "inject" with string format on a condition of if row count is odd vs even, then I tried to filter some of columns to have a keypress event bound to a js function and another attribute as a string. 否则它确实有效,通过两个分开的字符串交替行,我用字符串格式“注入”,条件是如果行计数是奇数甚至是偶数,那么我试图过滤一些列以使keypress事件绑定到js函数和另一个属性为字符串。

If the string is empty, then end part of the element "declaration" will be empty if condition was met, then that string is assigned with value "ReadOnly" and js string is assigned with keypress event "calling a function code". 如果字符串为空,那么如果满足条件,则元素“declaration”的结尾部分将为空,然后为该字符串赋值“ReadOnly”,并为js字符串分配keypress事件“调用函数代码”。

Here's the code. 这是代码。 The situation is weird as style attributes, information of current column, columns names, everything does function as expected but those two READONLY Attribute And event Key (javascript small validation task) that do not. 这种情况很奇怪,因为style属性,当前列的信息,列名称,一切都按预期运行,但那两个READONLY属性和event键(javascript小验证任务)没有。

Render a dynamic Table Code 呈现动态表格代码

This is the front code, c# code behind is used mostly (to keep a little code client-side as possible) 这是前面的代码,后面的c#代码主要用于(尽可能保留一些代码客户端)

`ControlsInteraction.WithTable.Design()`

AND

`ControlsInteraction.WithTable.ExtractData()`

are dealing with dynamic functions of rendering and translation of columns names and values 正在处理列名和值的渲染和转换的动态函数

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++;
                 }

        }
}

Is injected properly and the read only part READONLY Attribute, and event Key - (javascript small validation task) 是正确注入和只读部分READONLY属性,和事件键 - (javascript小验证任务)

Either functions on all or none 无论是全部还是无功能

What am I doing wrong? 我究竟做错了什么?

answering my own Question aventually answer is 回答我自己的问题,最近答案是

...well everything , including @Patrics Comment Was Wrong ...好吧一切,包括@Patrics评论都错了

i can just say put good attention to : how to work with DataTable DataRow, DataTable DataColumns and the relations for and foreach variables scope use your visual sudio debugger on every line to check on your codes values 我只能说好好关注:如何使用DataTable DataRow,DataTable DataColumns以及关系和foreach变量范围使用你的visual sudio调试器在每一行检查你的代码值

i did not have the time to rename variables but if you need to make a dynamic html table out of a DB table this is the way 我没有时间重命名变量,但如果你需要从数据库表中创建一个动态的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++;
        }

    }

i am adding all researches i have made to be more easy on the data extraction and some more methods i have worked on so if u like to use it feel free to ... 我正在添加我所做的所有研究,以便更容易在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