如何访问在txtRow(迭代)输入字段中输入的字符串值? [英] How do I access string values entered in txtRow(iterated) input field?

查看:69
本文介绍了如何访问在txtRow(迭代)输入字段中输入的字符串值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字符串值列表与外部数据源(Excel表格)进行比较并显示警告。我编写了代码来比较一个输入值和excelsheet。任何帮助多个输入值的帮助将非常有帮助。谢谢。这是我的代码。

I would like to compare a list of string values with an external data-source(Excelsheet) and show an alert.I have written the code to compare one input value with the excelsheet. Any help to do it for multiple input values will be very helpful. Thanks. Here is my code.

<body>
    <form id="form1"  runat="server">
    <div><small></small>
        <table class="style1">
           <tr>
              <td>
                 <table>
                    <tr>
                       <td>
                          <input style="cursor: hand" />
                        </td>
                        <td align="center">
                                <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="Submit_click" />
                        </td>
                     </tr>
                  </table>
                    <table id="tblCtnrInfo">
                        <tr>
                            <td>
                                <asp:Label ID="Label1" runat="server" Text="ContainerInfo: "></asp:Label>
                            </td>
                            <td>
                                <input id="txtRow1" name="txtRow1" />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>

    <script type="text/javascript" language="javascript">
        function addRowToTable() {
            var tbl = document.getElementById('tblCtnrInfo');
            var lastRow = tbl.rows.length;

            var iteration = lastRow;
            var row = tbl.insertRow(lastRow);

            // left cell
            var cellLeft = row.insertCell(0);
            var textNode = document.createTextNode('ContainerInfo: ');
            cellLeft.appendChild(textNode);

            // right cell cntrNum
            var cellRight = row.insertCell(1);
            var el = document.createElement('input');
            el.type = 'text';
            el.name = 'txtRow' + iteration;
            el.id = 'txtRow' + iteration;
            el.size = 20;
            cellRight.appendChild(el);
        } 
    </script>

    </form>
</body>




namespace ExcelSearchWebApp
{
    public partial class TextBoxCompare : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void Submit_click(object sender, EventArgs e)
        {
            // Create connection string variable. Modify the "Data Source"
            // parameter as appropriate for your environment.
            string path = @"C:\TEMP\ExcelSearchWebApp\OneWayContainers.xlsx";
            string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";

            // Create connection object by using the preceding connection string.
            OleDbConnection objConn = new OleDbConnection(connStr);

            // Open connection with the database.
            objConn.Open();

            // The code to follow uses a SQL SELECT command to display the data from the worksheet.

            // Create new OleDbCommand to return data from worksheet.
            OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM MyRange", objConn);

            // Create new OleDbDataAdapter that is used to build a DataSet
            // based on the preceding SQL SELECT statement.
            OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();

            // Pass the Select command to the adapter.
            objAdapter1.SelectCommand = objCmdSelect;

            // Create new DataSet to hold information from the worksheet.
            DataSet objDataset1 = new DataSet();

            // Fill the DataSet with the information from the worksheet.
            objAdapter1.Fill(objDataset1, "XLData");

            string strExcelContainerInfo;
            string strContainerInfoText = Convert.ToString(txtRow1.Value).ToUpper();


            if (objDataset1.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < objDataset1.Tables[0].Rows.Count; i++)
                {
                    strExcelContainerInfo = Convert.ToString(objDataset1.Tables[0].Rows[i]["OneWayContainers"].ToString());

                    if (strContainerInfoText == strExcelContainerInfo)
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('OneWayContainer');window.location ='RedirectPage.aspx';", true);

                    }
                    else
                    {
                        //Submit form logic;
                    }
                }
            }
            // Clean up objects.
            objConn.Close();
        }

        
    }
}



推荐答案

1。在html部分创建一个隐藏字段。

2.将迭代值放在该字段中。

3.在后面的代码中使用存储的迭代值在隐藏字段中循环并获取后面代码中文本框的值。



希望上述步骤有所帮助。
1. Create a hidden field in the html part.
2. Put the value of iteration in that field.
3. In the code behind use the value of iteration which is stored in the hidden field to loop through and get the value of the textbox in code behind.

Hope the above steps helps.


这篇关于如何访问在txtRow(迭代)输入字段中输入的字符串值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆