1

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

这是我的代码:

 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>
4

2 回答 2

2

您没有将数据分配给 的行的列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 );           
}
于 2013-03-15T06:53:21.683 回答
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();
于 2013-03-15T07:23:25.943 回答