datalis 里嵌套gridview ,然后datalist控件 添加的按钮,怎么使用按钮发送对应行的GridView内容,求解决方法,急急急!
这是前台代码:<asp:DataList ID="DataList1" runat="server" 
            onitemdatabound="DataList1_ItemDataBound" 
            onitemcommand="DataList1_ItemCommand">
            <ItemTemplate>
                <table style="width:100%;">
                    <tr>
                        <td rowspan="2">
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("CostCenterOwnerName") %>'></asp:Label>
                            <br />
                            <asp:Label ID="Label3" runat="server" Text='<%# Eval("CostCenterId") %>'></asp:Label>
                        </td>
                        <td>
                            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                                onrowcommand="GridView1_RowCommand" onrowdatabound="GridView1_RowDataBound" 
                                ShowFooter="True">
                                <Columns>
                                    <asp:BoundField HeaderText="FirstName" DataField="Firstname" 
                                        FooterText="Total" />
                                    <asp:BoundField HeaderText="LastName" DataField="Lastname" />
                                    <asp:BoundField HeaderText="Monthly Amount Paid" DataField="Price" />
                                    <asp:BoundField HeaderText="Cost Center" DataField="CostCenter" />
                                    <asp:BoundField HeaderText=" G/L  Posted by Finance" 
                                        DataField="PostedbyFrinance" />
                                    <asp:BoundField HeaderText="Carrier" DataField="Carrier" />
                                    <asp:BoundField HeaderText="Phone Type" DataField="PhoneType" />
                                    <asp:BoundField HeaderText="Phone Number" DataField="PhoneNumber" />
                                    <asp:BoundField HeaderText="E-mail" DataField="Mail" />
                                    <asp:BoundField HeaderText="Self-reported title" DataField="Posttitle" />
                                    <asp:BoundField HeaderText="HR job title" DataField="HRJobTitle" />
                                    <asp:BoundField HeaderText="Division" DataField="Division" />
                                    <asp:BoundField HeaderText="VP" DataField="VP" />
                                    <asp:BoundField HeaderText="Function" DataField="Department" />
                                    <asp:BoundField HeaderText="Plant" DataField="Plant" />
                                    <asp:BoundField HeaderText="Region" DataField="Region" />
                                </Columns>
                            </asp:GridView>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Label ID="Label2" runat="server" Text="NoData"></asp:Label>
                            <asp:Button ID="Button1" runat="server" Text="Send" onclick="Button1_Click" 
                                CommandArgument='<%# Eval("CostCenterId") %>' CommandName="Send" />
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:DataList>怎么点击按钮发送的是对用行的GridView 的内容,请大家帮帮忙!谢谢了

解决方案 »

  1.   

    在该行进行 DataList1.Items[当前行的index].FindControl("GridView1")
      

  2.   

    在Button1_Click里面可以这样得到protected void Button1_Click(object sender, EventArgs e)
    {
      Button b = sender as Button;
      DataListItem bl = b.Parent as DataListItem;
      GridView gv = bl.FindControl("GridView1") as GridView;
    }
      

  3.   

    我把源码贴一下,我不知道是什么错误,为什么发的总是全部内容,不是相对应的
     protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            GridView GV = (GridView)e.Item.FindControl("GridView1");
        } 
     protected void Button1_Click(object sender, EventArgs e)
        { Button b = sender as Button;
                DataListItem bl = b.Parent as DataListItem;
                GridView GView = bl.FindControl("GridView1") as GridView;            string tableStr = @"<table border='0' cellpadding='0'  cellspacing='0' style='font-family:宋体; font-size:14px; color:Blue;'>
               <tr><td>您好:</td></tr><tr><td>&nbsp;&nbsp;&nbsp;如下為" + (DateTime.Now.Date.Month - 1) + @"月份,部門手機話費明細,請收悉!</td></tr></table><br/><p></p><table border='0' cellpadding='0'  cellspacing='0' style='font-family:宋体; font-size:14px; color:Blue;' align='left'>
               <tr><td>手機管理系統維護組<br /> 敬呈! </td></tr>
               </table><br/><p></p><br/><p></p><table border='1' cellpadding='0' cellspacing='0' align='left'>
               <tr style='color: #FFFFFF;background-color: #FF9900;' align='center'><td>FirstName</td><td>LastName</td><td>MonthlyAccountPaid</td><td>CostCenter</td><td>PhoneType</td><td>PhoneNumber</td><td>E-Mail</td></tr>
               <tr style='color: #FFFFFF;background-color: #FF9900;' align='center'><td>Total</td><td style='font-size:12px;width:1000px;'>" + Session["a"].ToString() + @"</td></tr>";
                string FirstName = ""; string Lastname = ""; string MonthlyAccountPaid = "";
                string CostCenter = ""; string PhoneType = ""; string PhoneNumber = "";
                string Mail = "";
               
                foreach (DataListItem item in DataList1.Items)
                {
                    GridView gv = (GridView)item.FindControl("GridView1");
                    foreach (GridViewRow row in gv.Rows)
                    {                    FirstName += row.Cells[0].Text + "<br/>";
                        Lastname += row.Cells[1].Text + "<br/>";
                        MonthlyAccountPaid += row.Cells[2].Text + "<br/>";
                        CostCenter += row.Cells[3].Text + "<br/>";
                        PhoneType += row.Cells[6].Text + "<br/>";
                        PhoneNumber += row.Cells[7].Text + "<br/>";
                        Mail += row.Cells[8].Text + "<br/>";
                    }
                
                    tableStr += @"<tr align='center'><td style='font-size:12px;width:200px;'>" + FirstName + @"</td><td style='font-size:12px;width:200px;'>" + Lastname + @"</td>
                    <td style='font-size:12px;width:200px;'>" + MonthlyAccountPaid + @"</td>
                    <td style='font-size:12px;width:200px;'>" + CostCenter + @"</td>
                    <td style='font-size:12px;width:200px;'>" + PhoneType + @"</td><td style='font-size:12px;width:200px;'>" + PhoneNumber + @"</td>
                    <td style='font-size:12px;width:200px;'>" + Mail + @"</td></tr>";
                    tableStr += @"</table>";
                    SendSMTPMail("Sina.com", "[email protected]", "", "[email protected]", "MobileManagementSystem", tableStr);
                    Response.Write("<script language=javascript>alert('發送成功!');</script>");
                }         }
    麻烦帮我看一下。谢谢!
      

  4.   

    哥啊 你的代码好混乱。。
    直接DataList1.Items[要取Gridview的索引].FindControl("Gridview的ID")这样就取出一个Gridview了 
    你在取数据发送。。
      

  5.   

            <asp:DataList ID="DataList1" runat="server" 
                onitemdatabound="DataList1_ItemDataBound">
            <HeaderTemplate><table></HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:Label ID="Label1" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
                    <td>
                        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                        <Columns>
                            <asp:BoundField DataField="ID" />
                            <asp:BoundField DataField="Name" />
                        </Columns>
                        </asp:GridView>
                    </td>
                    <td>
                        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
                    </td>
                </tr>
            </ItemTemplate>
            <FooterTemplate></table></FooterTemplate>
            </asp:DataList>    protected void Button1_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            DataListItem item = btn.NamingContainer as DataListItem;
            GridView g = item.FindControl("GridView1") as GridView;
            Response.Write(g.Rows[0].Cells[0].Text + "==" + g.Rows[0].Cells[1].Text);
        }