我有一个有子GridView的父GridView (下面的代码),我如何获取该子网格视图复选框的值?另外,我如何保存子网格视图的状态,即它是否显示?这是当按钮被按下时触发的函数,该按钮读取父网格以查看哪些出版物已被选中:
protected void DeleteSelectedProducts_Click(object sender, EventArgs e)
{
bool atLeastOneRowDeleted = false;
// Iterate through the Products.Rows property
foreach (GridViewRow row in GridView1.Rows)
{
// Access the CheckBox
CheckBox cb = (CheckBox)row.FindControl("PublicationSelector");
if (cb != null && cb.Checked)
{
// Delete row! (Well, not really...)
atLeastOneRowDeleted = true;
// First, get the ProductID for the selected row
int productID =
Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
// "Delete" the row
DeleteResults.Text += string.Format(
"This would have deleted ProductID {0}<br />", productID);
//DeleteResults.Text = "something";
}
// Show the Label if at least one row was deleted...
DeleteResults.Visible = atLeastOneRowDeleted;
}
}
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="PublicationID"
DataSourceID="ObjectDataSource1" Width="467px" OnRowDataBound="GridView1_RowDataBound"
Font-Names="Verdana" Font-Size="Small">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="PublicationSelector" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="NameAbbrev" HeaderText="Publication Name" SortExpression="NameAbbrev" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
<asp:TemplateField HeaderText="Owners">
<ItemTemplate>
<asp:Label ID="Owners" runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
<asp:TemplateField HeaderStyle-CssClass="hidden-column" ItemStyle-CssClass="hidden-column" FooterStyle-CssClass="hidden-column">
<ItemTemplate>
<tr>
<td colspan="8" >
<div id="<%# Eval("PublicationID") %>" style="display: none; position: relative;" >
<asp:GridView ID="GridView2_ABPubs" runat="server" AutoGenerateColumns="false" Width="100%"
DataKeyNames="PublicationID" Font-Names="Verdana" Font-Size="small">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="ChildPublicationSelector" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="NameAbbrev" HeaderText="Publication Name" SortExpression="NameAbbrev" />
</Columns>
</asp:GridView>
</div>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetData"
TypeName="shoom_dev._Default">
</asp:ObjectDataSource>
<p>
<asp:Button ID="DeleteSelectedProducts" runat="server"
Text="Delete Selected Products" onclick="DeleteSelectedProducts_Click" />
</p>
<p>
<asp:Label ID="DeleteResults" runat="server" EnableViewState="False" Visible="False"></asp:Label>
</p>发布于 2009-07-22 19:49:15
执行与GridView2_ABPubs控件的checkbox相同的row.FindControl()方法。这应该会给你一个网格视图,然后你可以在上面做一个find控件。
但是,在花了三天时间来启动和自定义一个GridView之后,使用子网格视图的最后一个模板列不需要和节点,因为这些节点将由GridView控件自动添加,这可能会使查找子控件变得更加棘手。
我还发现FindControl在堆栈中看起来并不是很远,所以我创建了一个扩展方法来递归地查找该控件:
public static T FindControl<T>(this Control parent, string controlName) where T: Control
{
T found = parent.FindControl(controlName) as T;
if (found != null)
return found;
foreach(Control childControl in parent.Controls)
{
found = childControl.FindControl<T>(controlName) as T;
if (found != null)
break;
}
return found;
}发布于 2009-07-23 01:38:28
大卫,这是正确的吗?
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
public static class FindControl<T>
{public static T FindControl<T>(this Control parent, string controlName) where T : Control
{
T found = parent.FindControl(controlName) as T;
if (found != null)
return found;
foreach (Control childControl in parent.Controls)
{ found = childControl.FindControl(controlName) as T;
if (found != null)
break;
}
return found; }}
该类将另存为FindControl.cs。如何调用该函数?
我觉得自己有点迟钝,但在我看来,这是我第一次使用extendor类。
嗯,我刚刚收到一个错误消息,非泛型方法必须在静态类中定义,所以我猜我有一些错误...LOL
谢谢。
https://stackoverflow.com/questions/1167682
复制相似问题