【发布时间】:2012-09-03 08:18:59
【问题描述】:
我有一个 ajax 选项卡,里面有一个网格,在这个网格上有一个复选框,用户可以检查它并将其分配给技术人员,但由于某种原因我无法引用控件,网格总共有 4 行,所以那里有数据,这是我的代码,它是通过单击按钮运行的
protected void btnAllocate_click(object sender, EventArgs e)
{
foreach (System.Web.UI.Control s in tbHappyCall.Controls)
{
foreach (GridViewRow Row in gvHappyCall.Rows)
{
CheckBox chkAllocate = (CheckBox)Row.FindControl("chkAllocate");
if (chkAllocate.Checked)
{
HyperLink lblOrderID = (HyperLink)Row.FindControl("hyperOrderID");
HappyCallManager objHappyCallManager = new HappyCallManager();
objHappyCallManager.HappyCallAllocated(Convert.ToInt64(lblOrderID.Text), objWebuser.ShortAbbr, ddlAllocateTo.SelectedValue);
//Update database with person details thats are assigned to the Welcome Call
}
}
}
}
当它继续到 Foreach gridviewrow 等时,即使有 4 行显示信息,计数也是 1?
任何人都可以对此有所了解吗?甚至更好的解决方案?
感谢您的宝贵时间。
更新
<ajaxToolkit:TabPanel ID="tbHappyCall" runat="server" HeaderText="Welcome Calls" TabIndex="10">
<ContentTemplate>
<%--OnRowDataBound="gvConfirmOrder_rowDataBound"--%>
<div id="divHappyCall">
<asp:GridView ID="gvHappyCall" runat="server" AutoGenerateColumns="false" class="tablesorter"
EmptyDataText="There is no Record to display." DataKeyNames="OrderID" EnableViewState="false"
OnRowDataBound="gvHappyCall_RowDataBound">
<AlternatingRowStyle CssClass="NormalTextVNC" BackColor="#E2E9E7"></AlternatingRowStyle>
<Columns>
<asp:TemplateField HeaderText="Account Manager">
<ItemTemplate>
<asp:Label ID="lblAccountManager" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.AccountManager") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OrderID">
<ItemTemplate>
<asp:HyperLink ID="hyperOrderID" runat="server" ForeColor="White" NavigateUrl='<%#Eval("OrderGuid","/Documents/HappyCall.aspx?OrderID={0}") %>'
Text='<%#Eval("OrderID") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Customer Name">
<ItemTemplate>
<asp:Label ID="lblCustomerName" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CustomerName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Company Name">
<ItemTemplate>
<asp:Label ID="lblCompanyName" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CompanyName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile Number">
<ItemTemplate>
<asp:Label ID="lblMobileNumber" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.MobileNumber") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dispatch Date">
<ItemTemplate>
<asp:Label ID="lblConnectionDate" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.DespatchDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="lblStatus" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField headertext="Date Called" dataformatstring="{0:dd/MM/yyyy}" datafield="EditedDate" HeaderStyle-Width="100px" />
<asp:TemplateField HeaderText="Allocated To">
<ItemTemplate>
<asp:Label ID="lblAllocatedTo" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.AllocatedTo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<%--<asp:TemplateField HeaderText="Last Action">
<ItemTemplate>
<asp:Label ID="lblLAstAction" CssClass="gvItem" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.LastAction") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>--%>
<asp:TemplateField HeaderText="Check">
<ItemTemplate>
<asp:CheckBox runat="server" ID="chkAssignWelcomeCall" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:DropDownList runat="server" ID="ddlAllocateTo" CssClass="floatright">
<asp:ListItem>Name of Admin to Allocate</asp:ListItem>
</asp:DropDownList>
<div class="WhiteSpace"></div>
<asp:Button runat="server" ID="btnAllocate" class="floatright" Text="Allocate" OnClick="btnAllocate_click" />
</div>
</ContentTemplate>
</ajaxToolkit:TabPanel>
【问题讨论】:
-
btnAllocate在哪里,tbHappyCall在哪里,是什么,当你可以直接引用gvHappyCall而你根本不使用变量s时,为什么还要迭代它的控件?跨度> -
BtnAllocate 位于 gridview 下,抱歉 tbHappyCall 是 Ajax 选项卡的名称,我尝试直接引用 gvHappyCall 但即使其中有 4 行,它也只返回计数 1 所以我对内部带有控件的 ajax 选项卡进行了一些谷歌搜索,我遇到了一个博客,上面说我必须 foreach 控件等。
-
你用调试器看看会发生什么?我假设某处有例外。什么是网格的数据源(例如
DataTable或SqlDataSource)?您是在 page_load 中的每次回发时重新绑定 GridView 还是仅在第一次加载时重新绑定?你能显示 GridView 的 aspx 标记吗? -
嗨,我在上面添加了 HTML 标记,我使用数据表来绑定网格,我使用了调试器,当我查看控件计数时,我再次注意到它的计数为 1 ,但是当我 foreach gridview 行等时它甚至没有进入那个循环
-
澄清一下:你不需要遍历你的TabPanel的控件集合来找到你的GridView,它是直接可用的,不要混淆它的
Controls.Count和GridView.Rows.Count的数量。另一个问题:ViewState 是否在某处被禁用? 编辑 我看到 GridView 的 ViewState 被禁用。我认为当你启用它时它会起作用;)