如何动态地将文本框中的数据绑定到列表视图中

时间:2013-03-15 06:49:09

标签: c# asp.net

我拥有textbox2中的所有链接,但当我点击转换按钮时,该链接应该出现在列表视图中。但是我无法获得带有data的列表视图。所以任何人都可以指导我

这是我的代码:

 protected void btn_createlink_Click(object sender, EventArgs e)
        {
            var links = TextBox1.Text.Split(new string[] { "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);
           // dt["Links"] = TextBox1.Text;
            foreach (var link in links)
            {
                if (!IsLinkWorking(link))
                {
                    //Here you can show the error. You don't specify how you want to show it.
                    TextBox2.ForeColor = System.Drawing.Color.Green;
                    TextBox2.Text += string.Format("{0}\nNot working\n\n ", link);
                }
                else
                {
                    TextBox2.Text += string.Format("{0}\n working\n\n", link);
                }
            }
        }



    protected void Button2_Click(object sender, 
                                 EventArgs e)[convert button click event]
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Links");
        dt.Columns.Add("Status");
        dt.Columns.Add("Error");
        dt.AcceptChanges();
        DataRow dr = dt.NewRow();
        dr["Links"] = "";
        dr["Status"] = "";
        dr["Error"] = "";
        dt.Rows.Add(dr);
        dt.AcceptChanges();

        myListView.DataSource = dt;
        myListView.DataBind();

        //var item = string.Format("[{0}, {1} ,{2}]", TextBox2.Text );           
    }

客户方:

<asp:ListView runat="server" ID="myListView">
        <LayoutTemplate>
            <table id="Table1" runat="server" border="1">
                <tr id="Tr1" runat="server">
                    <th id="Th1" runat="server">Links</th>
                    <th id="Th2" runat="server">Status</th>
                    <th id="Th3" runat="server">Error</th>
                </tr>
                <tr runat="server" id="ItemPlaceholder">
                </tr>
            </table>
        </LayoutTemplate>
        <ItemTemplate>
            <tr id="Tr2" runat="server">
                <td id="Td1" runat="server"> Eval("Links")</td>
                <td id="Td2" runat="server"> Eval("Status")></td>
                <td id="Td3" runat="server"> Eval("Error")</td>
            </tr>
        </ItemTemplate>
    </asp:ListView>

2 个答案:

答案 0 :(得分:2)

您没有将数据分配给DataTable行的列。在向datatable添加行之前分配值。

更改

dr["Links"] = "";
dr["Status"] = "";
dr["Error"] = "";

dr["Links"] = "some link";
dr["Status"] = "status ";
dr["Error"] = " no error";

dr["Links"] = txtLink.Text;
dr["Status"] = txtStatus.Text;
dr["Error"] = txtError.Text;

您的代码将是。

protected void Button2_Click(object sender, EventArgs e)[convert button click event]
{
    DataTable dt = new DataTable();
    dt.Columns.Add("Links");
    dt.Columns.Add("Status");
    dt.Columns.Add("Error");
    dt.AcceptChanges();
    DataRow dr = dt.NewRow();
    dr["Links"] = "some link";
    dr["Status"] = "status ";
    dr["Error"] = " no error";
    dt.Rows.Add(dr);
    dt.AcceptChanges();
    myListView.DataSource = dt;
    myListView.DataBind();
    //var item = string.Format("[{0}, {1} ,{2}]", TextBox2.Text );           
}

答案 1 :(得分:2)

我做了一些修改,如果有效,请告诉我。

<asp:ListView runat="server" ID="myListView">
        <LayoutTemplate>
            <table id="Table1" runat="server" border="1">
                <tr id="Tr1" runat="server">
                    <th id="Th1" runat="server">
                        Links
                    </th>
                    <th id="Th2" runat="server">
                        Status
                    </th>
                    <th id="Th3" runat="server">
                        Error
                    </th>
                </tr>
                <tr runat="server" id="ItemPlaceholder">
                </tr>
            </table>
        </LayoutTemplate>
        <ItemTemplate>
            <tr id="Tr2" runat="server">
                <td id="Td1" runat="server">
                    <%#Eval("Links") %>
                </td>
                <td id="Td2" runat="server">
                    <%#Eval("Status")%>
                </td>
                <td id="Td3" runat="server">
                    <%#Eval("Error")%>
                </td>
            </tr>
        </ItemTemplate>
    </asp:ListView>




DataTable dt = new DataTable();
        dt.Columns.Add("Links");
        dt.Columns.Add("Status");
        dt.Columns.Add("Error");
        dt.AcceptChanges();
        DataRow dr = dt.NewRow();
        dr["Links"] = "a";
        dr["Status"] = "b";
        dr["Error"] = "c";
        dt.Rows.Add(dr);
        dt.AcceptChanges();

        myListView.DataSource = dt;
        myListView.DataBind();