我有一个Rad Grid,我将数据源绑定到Page Load中。我试图捕获用户单击行时选择的行。我已成功完成了一个Rad Grid,它在标记中设置了数据源。是否可以使用具有动态设置数据源的网格来执行此操作。
标记
<telerik:RadGrid ID="rgTable" runat="server" OnSelectedIndexChanged="rgTable_OnSelectedIndexChanged">
<ClientSettings EnableRowHoverStyle="true" EnablePostBackOnRowClick="true">
<Selecting AllowRowSelect="true" />
</ClientSettings>
</telerik:RadGrid>
C#
protected void Page_Load(object sender, EventArgs e)
{
int id = Convert.ToInt32(Request.QueryString["ID"]);
ConnString = Connections.Search(0, 999999, null, null, null, id, null, null, null, null, null)[0].Connection_String;
StoreTableAndViewNames();//This method sets arrTables with ArrayList
rgTable.DataSource = arrTables;
rgTable.DataBind();
}
protected void rgTable_OnSelectedIndexChanged(object sender, EventArgs e)
{
try
{
if (rgTable.SelectedItems.Count > 0)//This test is failing
{
GridDataItem item = (GridDataItem)rgTable.SelectedItems[0];
string table = item.Cells[0].ToString();
StoreColumnNames(table);
rgColumn.DataSource = arrColumns;
rgColumn.DataBind();
}
}
catch (Exception ex)
{
errorlbl.Text = ex.ToString();
}
}
答案 0 :(得分:1)
在您的代码中,Data会在页面加载时绑定到rgTable。
结果,当在客户端选择行时,数据在rgTable
被触发之前再次绑定到OnSelectedIndexChanged
。
protected void Page_Load(object sender, EventArgs e)
{
....
if (!IsPostBack)
{
rgTable.DataSource = arrTables;
rgTable.DataBind();
}
}
如果您绑定NeedDataSource
事件中的数据,RadGrid会知道何时&amp;如果需要,在哪里查找数据。
protected void rgTable_OnNeedDataSource(object sender,
GridNeedDataSourceEventArgs e)
{
rgTable.DataSource = arrTables;
}