单击选择列表视图行。 asp.net c#

时间:2016-08-12 05:29:48

标签: c# asp.net

我已经工作了两天,试图通过单击所需的行来创建一个可由sql数据库包含的列表视图。我正在使用listview,因为我可以使用css轻松地格式化它。我的应用程序有几个仪表板,将有几个列表视图用于选择报价,订单,发票等。我得到它几乎工作除了某些原因第一行不可选。第二行是可选择的,当选择时表示选择了索引1。当你尝试选择它时,我需要所有可选行和索引0或第一行。没有任何反应我会包含我正在使用的代码。

protected void Page_Load(object sender, EventArgs e)
{
    //string[] Item = new string[2];
    DataTable dt = new DataTable();
    var myConnection = Connection.GetConnection();
    var Sql = "select Invoice_Id, Description, Balance from BalanceDue where Vendor_Num =101";
    var Command = new SqlCommand(Sql, myConnection);
    myConnection.Open();
    SqlDataAdapter ada = new SqlDataAdapter(Command);
    ada.Fill(dt);
    //var reader = Command.ExecuteReader();
    ListView1.DataSource = (dt);
    ListView1.DataBind();
}

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        Button btn = e.Item.FindControl("Button1") as Button;
        string script = this.ClientScript.GetPostBackClientHyperlink(btn, "", true);
        Panel p = e.Item.FindControl("Panel1") as Panel;
        p.Attributes.Add("onclick", script);
    }
}

protected void ListView1_SelectedIndexChanging(object sender, ListViewSelectEventArgs e)
{
}

protected void ListView1_SelectedIndexChanged(object sender, EventArgs e)
{
    lblBad.Text = ListView1.SelectedIndex.ToString(); //just a label I use to see selected index
    lblBad.Visible = true;
}

ASPX:

<asp:ListView ID="ListView1" runat="server" DataKeyNames="Invoice_Id"
    OnItemDataBound="ListView1_ItemDataBound"
    OnSelectedIndexChanging="ListView1_SelectedIndexChanging" OnSelectedIndexChanged="ListView1_SelectedIndexChanged">
    <LayoutTemplate>
        <table class="body-cell-316">
            <tr>
                <th style="text-align: left">
                    <asp:LinkButton ID="lnkDesc" runat="server">Desc.</asp:LinkButton>
                </th>
                <th style="text-align: left">
                    <asp:LinkButton ID="lnkBalance" runat="server">Balance</asp:LinkButton>
                </th>
            </tr>
            <tr>
                <tr id="itemPlaceholder" runat="server"></tr>
            </tr>
        </table>
    </LayoutTemplate>

    <SelectedItemTemplate>
        <tr style="">
            <td>
                <asp:Label runat="server" ID="lblIdDesc"><%#Eval("Description") %></asp:Label>
            </td>
            <td>
                <asp:Label runat="server" ID="lblBalance"><%#Eval("Balance") %></asp:Label>
            </td>
        </tr>
    </SelectedItemTemplate>

    <ItemTemplate>
        <asp:Panel ID="Panel1" runat="server">
            <table class="body-cell-316">
                <tr>
                    <td>
                        <asp:Label runat="server" ID="lblIdDesc"><%#Eval("Description") %></asp:Label>
                    </td>
                    <td>
                        <asp:Label runat="server" ID="lblBalance"><%#Eval("Balance") %></asp:Label>
                    </td>
                    <asp:Button ID="Button1" CommandName="Select" runat="server" Text="Button" Visible="false" />
                </tr>
            </table>
        </asp:Panel>
    </ItemTemplate>
</asp:ListView>

0 个答案:

没有答案