我动态创建了一个gridview,现在我想在selectedindex发生变化时触发一个事件。
GridView NewDg = new GridView();
NewDg.ID = "SubGridView" + e.Row.RowIndex.ToString();
NewDg.DataKeyNames = new string[]{"logentry_id"};
NewDg.SelectedIndexChanged += new EventHandler(NewDg_SelectedIndexChanged);
NewDg.RowDataBound += new GridViewRowEventHandler(NewDg_RowDataBound);
RowDataBound有效,但我猜它不会产生正确的回发网址。 在RowDataBound中,我有以下代码:
GridView sendingGridView = (GridView)sender;
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex);
这会产生以下代码:
javascript:__doPostBack('SubGridView4','Select$0')
只有这不会导致回复此功能:
void NewDg_SelectedIndexChanged(object sender, EventArgs e)
{
GridView sendingGridView = (GridView)sender;
ViewDetails(Convert.ToInt32(sendingGridView.SelectedDataKey["logentry_id"].ToString()));
}
有谁知道我做错了什么?
答案 0 :(得分:0)
首先,您是在每次加载页面时重新创建网格吗?这是以这种方式创建网格的要求。其次,尝试点击RowCommand,然后寻找命令名称;也许这会成功解雇;您可以通过命令参数获得对命令的引用,如下所示:
void rowcmd(..) {
if (e.CommandName != null && e.CommandName.StartsWith("Select")) {
//Dothis
}
}
答案 1 :(得分:0)
我在Code Project上找到了我的问题的答案:
我现在在gridview中使用gridview
<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID="SubGridView"
由于GridView上的扩展器,当我点击加号时会显示gridview(参见链接)
在页面加载时执行以下操作:
protected void Page_Load(object sender, EventArgs e)
{
GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);
GridView1.DataSource = dc.GetLogEntriesWithUsername();
GridView1.DataBind();
我已经在此gridview上有一个DataBound和一个Selected Index Changed事件。
在行创建的事件中,我执行以下操作:
void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView SubGridView = e.Row.FindControl("SubGridView") as GridView;
List<GetLogEntriesWithUsernameByParentIdResult> subLogEntries = dc.GetLogEntriesWithUsernameByParentId(((GetLogEntriesWithUsernameResult)e.Row.DataItem).logentry_id).ToList();
if (subLogEntries.Count > 0)
{
SubGridView.DataSource = subLogEntries;
SubGridView.DataBind();
(e.Row as ExtGridViewRow).ShowExpand = SubGridView.Rows.Count > 0;
}
}
}
在subgridview上,我还有一个DataBound和一个SelectedIndex Changed事件。现在可以了!
我使用此DataBound事件:
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{ // only apply changes if its DataRow
GridView sendingGridView = (GridView)sender;
if (e.Row.RowType == DataControlRowType.DataRow)
{
// when mouse is over the row, save original color to new attribute, and change it to highlight yellow color
e.Row.Attributes.Add("onmouseover",
"this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#C0C0C0';this.style.cursor='pointer';");
// when mouse leaves the row, change the bg color to its original value
e.Row.Attributes.Add("onmouseout",
"this.style.backgroundColor=this.originalstyle;this.style.cursor='cursor'");
//e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex));
e.Row.Cells[1].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex);
e.Row.Cells[2].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex);
e.Row.Cells[3].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex);
e.Row.Cells[4].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex);
e.Row.Cells[5].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex);
所选索引改变了事件:
protected void GridView_SelectedIndexChanged(object sender, EventArgs e)
{
GridView sendingGridView = (GridView)sender;
ViewDetails(Convert.ToInt32(sendingGridView.SelectedDataKey["logentry_id"].ToString()));
}
ViewDetails函数在不同的div中显示所选择的logentry的详细信息。 现在我正忙着做最后一步,那就是在点击一行之前继续显示数据。
感谢您的帮助,但这是解决我问题的方法。