我尝试使用GridView并获取被点击的行中的数据。我尝试了以下代码,当我点击行时,我收到所选索引,但是当我查看实际的GridView行时,它们为空。不确定我错过了什么。
.ASP让我的网格。
在每一行数据绑定时,我会确保点击应该设置所选索引。
.ASP让我的网格。
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True"
CssClass="datatables" Width="100%"
DataSourceID="SqlDataSource1"
GridLines="None" ShowFooter="True" AllowSorting="True"
onrowcreated="GridView1_RowCreated"
onrowdatabound="GridView1_RowDataBound" ShowHeaderWhenEmpty="True"
onrowcommand="GridView1_RowCommand"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<HeaderStyle CssClass="hdrow" />
<RowStyle CssClass="datarow" />
<PagerStyle CssClass="cssPager" />
</asp:GridView>
在每一行数据绑定时,我会确保点击应该设置所选索引。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex);
}
}
当选定的索引通过点击更改时,将触发此事件。我可以在第一行上设置断点,并查看所点击的索引存储在变量a中。然而,当我到达foreach循环时,它会直接跳过它,因为GridView1显示具有0行的计数。理论上,它应该有几百行,当索引匹配时,它应该获取第6个单元格中的数据并将其存储在字符串b中。为什么我在点击时没有获取到任何行呢?
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
int a = GridView1.SelectedIndex
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowIndex == a)
{
b = row.Cells[6].Text;
}
}
}
这里是我的页面加载。
protected void Page_Load(object sender, EventArgs e)
{
c = HttpContext.Current.Session["c"].ToString();
SqlDataSource1.ConnectionString = //My secret
string strSelect = "SELECT columnnames from tablenames where c in (@c)
SqlDataSource1.SelectParameters.Clear();
SqlDataSource1.SelectCommand = strSelect;
SqlDataSource1.SelectParameters.Add("c", c);
try
{
GridView1.DataBind();
}
catch (Exception e)
{
}
GridView1.AutoGenerateColumns = true;
}
AutoGenerateSelectButton
的问题是它实际上会在每个东西旁边放置“选择”一词。我只想要一个简单的点击,它会做出必要的响应。不幸的是,在GridView中没有列出onClick事件。 - John Wesley Gordon