如何在datalist中显示gridview的选定行

时间:2015-08-04 07:34:06

标签: c# asp.net gridview

我获取了GridView行的值,但是在将数据绑定到DataList之后。 DataList无法显示

ASPX

<asp:GridView ID ="gvresults" runat="server" 
       onselectedindexchanged="gvresults_SelectedIndexChanged">
   <Columns>
       <asp:ButtonField ButtonType="Button" CommandName="Select" Text="Select" />
   </Columns>
</asp:GridView>
<asp:DataList ID ="dlresults" runat="server"></asp:DataList>

aspx.cs

protected void gvresults_SelectedIndexChanged(object sender, EventArgs e)
{
    string Keywordid = gvresults.SelectedRow.Cells[1].Text;
    string val1 = gvresults.SelectedRow.Cells[2].Text;
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);
    DataSet ds = new DataSet();
    SqlCommand cmd = new SqlCommand("select * from Tbl_keywords where KeywordID ='" + Keywordid + "'",con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(ds);
    dlresults.DataSource = ds;
    dlresults.DataBind();
}

1 个答案:

答案 0 :(得分:0)

你这样写道:

dlresults.DataSource = ds.Tables[0];   
dlresults.DataBind();
dlresults.DataKeyField = "SomeKey";
dlresults.Visible = true;

在你的asp代码中:

<asp:DataList ID ="dlresults" runat="server">
      <ItemTemplate>
                <asp:Label runat='server' ID='lblItemName' Text='<%# DataBinder.Eval(Container.DataItem, "someVal") %>'>
                </asp:Label>
     </ItemTemplate>
</asp:DataList>