[英]selectedindexchanged on dynamically created gridview
I've created a gridview dynamically, now i want to fire an event when the selectedindex has changed. 我已经动态创建了一个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);
The RowDataBound works but it doesn't produce the right postback URL i guess. RowDataBound有效,但我猜它不能产生正确的回发URL。 In the RowDataBound i have the following code:
在RowDataBound中,我有以下代码:
GridView sendingGridView = (GridView)sender;
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex);
this produces the following code: 这将产生以下代码:
javascript:__doPostBack('SubGridView4','Select$0')
Only this doesn't lead to the postback to this function: 仅此不会导致此函数的回发:
void NewDg_SelectedIndexChanged(object sender, EventArgs e)
{
GridView sendingGridView = (GridView)sender;
ViewDetails(Convert.ToInt32(sendingGridView.SelectedDataKey["logentry_id"].ToString()));
}
Does anyone know what i'm doing wrong? 有人知道我在做什么错吗?
First, you are recreating the grid on every page load? 首先,您是在每次页面加载时重新创建网格吗? This is a requirement for creating the grid this way.
这是以这种方式创建网格的要求。 Second, try tapping into RowCommand, and looking for the command name that way;
其次,尝试进入RowCommand,然后以这种方式查找命令名称; maybe that will fire successfully;
也许那将成功触发; you get a reference to the command through the command argument as in :
您可以通过command参数获得对命令的引用,如下所示:
void rowcmd(..) {
if (e.CommandName != null && e.CommandName.StartsWith("Select")) {
//Dothis
}
}
I found the answer to my question on Code Project : 我找到了答案,我的问题代码项目 :
I now use a gridview in my gridview 我现在在我的GridView控件使用GridView
<asp:TemplateField>
<ItemTemplate>
<asp:GridView ID="SubGridView"
Because of the extender on GridView, The gridview will be displayed when I click on the plus sign (see link) 由于使用了GridView的扩展程序,因此当我单击加号时将显示gridview(请参阅链接)
on page load i perform the following: 在页面加载时,我执行以下操作:
protected void Page_Load(object sender, EventArgs e)
{
GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);
GridView1.DataSource = dc.GetLogEntriesWithUsername();
GridView1.DataBind();
I already have a DataBound and a Selected Index Changed event on this gridview. 我已经在此gridview上有一个DataBound和一个Selected Index Changed事件。
On the row created event i perform the following: 该行创建的事件我执行以下操作:
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;
}
}
}
On the subgridview i also have a DataBound and a SelectedIndex Changed event. 在子网格视图上,我还具有DataBound和SelectedIndex Changed事件。 This works now!
现在可以使用!
I use this DataBound event: 我使用此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);
And the selected index changed event: 并选择索引更改事件:
protected void GridView_SelectedIndexChanged(object sender, EventArgs e)
{
GridView sendingGridView = (GridView)sender;
ViewDetails(Convert.ToInt32(sendingGridView.SelectedDataKey["logentry_id"].ToString()));
}
The ViewDetails function show the details of the selected logentry in a different div. 该ViewDetails功能:显示在不同的DIV选择logentry的细节。 Now i'm busy on the final step, that is keep showing the data as it was before i clicked on a row.
现在我正忙于最后一步,那就是继续显示数据,就像我单击一行之前一样。
Thanks for the help, but this is the solution to my problem. 感谢您的帮助,但这是解决我的问题的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.