当前位置:编程学习 > C#/ASP.NET >>

datalist中嵌套 GridView1如何触发更新事件

<asp:DataList ID="DataList1" runat="server" RepeatLayout="Flow" DataKeyField="c_id"  
            OnItemCommand="DataList1_ItemCommand" HorizontalAlign="Left" 
            CaptionAlign="Left">
   <HeaderTemplate>
     客户
   </HeaderTemplate>
<ItemTemplate>
        <asp:LinkButton ID="LinkButton1" CommandName="select" runat="server"><%# Eval("c_Name")%></asp:LinkButton>
        
       <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="cp_TableName,cp_Table_id,cp_IsPrime" style="font-size:12px;" 
            onrowcancelingedit="GridView1_RowCancelingEdit" 
            onrowediting="GridView1_RowEditing" onrowdeleting="GridView1_RowDeleting" 
            onrowupdating="GridView1_RowUpdating">
            <Columns>
                <asp:BoundField DataField="cp_TableName" FooterText="资料" HeaderText="资料" />
                <asp:BoundField DataField="cp_Table_id" FooterText="资料" HeaderText="资料" />
                <asp:BoundField DataField="cp_IsPrime" FooterText="资料" HeaderText="资料" />
                <asp:CommandField ShowEditButton="True" DeleteText="" InsertText="" />
                <asp:CommandField ShowDeleteButton="True" />
            </Columns>
        </asp:GridView>
</ITemTemplate>
</asp:DataList>



 protected void Page_Load(object sender, EventArgs e)
    {
        datalist();
    }

    string conns = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
    public void datalist()
    {
        string sql = "select top 10* from Dict_Customer";
        SqlConnection conn = new SqlConnection(conns);
        conn.Open();
        SqlDataAdapter mydataadapter = new SqlDataAdapter(sql, conn);
        DataSet mydataset = new DataSet();
        mydataadapter.Fill(mydataset);
        this.DataList1.DataSource = mydataset;
        this.DataList1.DataBind();
    }

    static int index = 0;
    static string id = "";
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "select")
        {
            id = DataList1.DataKeys[e.Item.ItemIndex].ToString();
            index = e.Item.ItemIndex;
            GridView gr = ((System.Web.UI.WebControls.GridView)DataList1.Items[index].FindControl("GridView1"));
            grd();
        }
    }

    public void grd()
    {
        GridView gr = ((System.Web.UI.WebControls.GridView)DataList1.Items[index].FindControl("GridView1"));
        string sql = "select * from  Customer_ContactPerson where cp_Table_id=" + id;
        SqlConnection conn = new SqlConnection(conns);
        SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        adapter.Fill(ds);
        gr.DataSource = ds;
        gr.DataBind();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView gr = ((System.Web.UI.WebControls.GridView)DataList1.Items[index].FindControl("GridView1"));
        gr.EditIndex = e.NewEditIndex;
        gr.EditRowStyle.BackColor = System.Drawing.Color.White;
        grd();
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView gr = ((System.Web.UI.WebControls.GridView)DataList1.Items[index].FindControl("GridView1"));
        gr.EditIndex = -1;
        grd();
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridView gr = ((System.Web.UI.WebControls.GridView)DataList1.Items[index].FindControl("GridView1"));
        string sql = "delete Customer_ContactPerson where cp_Table_id=" + id;
        SqlConnection con = new SqlConnection(conns);
        con.Open();
        SqlCommand com = new SqlCommand(sql, con);
        com.ExecuteNonQuery();
        gr.EditIndex = -1;
        grd();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

    }
这里GridView1_RowUpdating没有执行..........郁闷中 --------------------编程问答-------------------- 自己先顶下 --------------------编程问答-------------------- 很好,很强大 --------------------编程问答-------------------- 用addhandler方式添加 --------------------编程问答-------------------- 顶下先 再看 --------------------编程问答-------------------- 按照常规的做法应该就可以了 --------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- <asp:CommandField ShowDeleteButton="True" />
修改下这个就行了
<asp:CommandField ShowDeleteButton="True"  CausesValidation="false" />

补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,