首页 新闻 会员 周边 捐助

gridview的编辑模板问题

0
悬赏园豆:10 [已解决问题] 解决于 2010-06-24 16:32

前台代码为:

代码
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames
="id" EnableModelValidation="True"
HorizontalAlign
="Center" onrowcommand="GridView1_RowCommand"
onrowediting
="GridView1_RowEditing">
<Columns>
<asp:TemplateField>

<ItemTemplate>
<%#Eval("id") %>
<%#Eval("bie") %>
<%#Eval("name") %>
<asp:Button ID="Button1" runat="server" Text="编辑" CommandName="Edit"/>
</ItemTemplate>

<EditItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"id") %>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#bind("name") %>'></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="更新" CommandName="Updata" />
<asp:Button ID="Button2" runat="server" Text="取消" CommandName="Canel" />
</EditItemTemplate>

</asp:TemplateField>

</Columns>
</asp:GridView>

后台代码为:

代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string str = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ceshiConnectionString2"].ConnectionString;
OleDbConnection con
= new OleDbConnection(str);
OleDbDataAdapter adp
= new OleDbDataAdapter("select * from [chan]", con);
System.Data.DataSet
set = new System.Data.DataSet();
adp.Fill(
set, "first");
GridView1.DataSource
= set.Tables["first"];
DataBind();
}
}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//if (e.CommandName == "Edit")
//{

GridView1.EditIndex
= 3;
//}
}

我有两个疑问:首先为什么当去掉  if (!IsPostBack)时单击编辑按钮gridview不进入编辑模板?其次是当加上  if (!IsPostBack)时为什么第一次单击编辑按钮时不进入编辑模板,只有当单击第二次时才进入?

时间都去哪了的主页 时间都去哪了 | 初学一级 | 园豆:51
提问于:2010-06-10 23:52
< >
分享
最佳答案
0

1.前台
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
                        ForeColor="#333333" GridLines="None" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
                        OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit">
                        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                        <Columns>
                            <asp:BoundField DataField="id" HeaderText="用户ID" ReadOnly="True" />
                            <asp:BoundField DataField="name" HeaderText="用户姓名" />
                            <asp:BoundField DataField="sex" HeaderText="性别" />
                            <asp:BoundField DataField="nPlace" HeaderText="籍贯" />
                            <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
                            <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
                            <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
                        </Columns>
                        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                        <AlternatingRowStyle BackColor="White" />
                    </asp:GridView>

1.后台
    SqlConnection sqlcon;
    SqlCommand sqlcom;
    string strCon = "Data Source=(local);Database=db_04;Uid=sa;Pwd=";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();//不写到IsPostBack里,每次按钮回发都会执行!

       //把绑定代码写到一个函数里,方便重复利用。
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bind();//编辑完成后要执行绑定!
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string sqlstr = "delete from tb_Member where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        sqlcon = new SqlConnection(strCon);
        sqlcom = new SqlCommand(sqlstr,sqlcon);
        sqlcon.Open();
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        bind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        sqlcon = new SqlConnection(strCon);
        string sqlstr = "update tb_Member set name='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',sex='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',nPlace='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='"
            + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        sqlcom=new SqlCommand(sqlstr,sqlcon);
        sqlcon.Open();
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        GridView1.EditIndex = -1;
        bind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bind();
    }
    public void bind()
    {
        string sqlstr = "select * from tb_Member";
        sqlcon = new SqlConnection(strCon);
        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
        DataSet myds = new DataSet();
        sqlcon.Open();
        myda.Fill(myds, "tb_Member");
        GridView1.DataSource = myds;
        GridView1.DataKeyNames = new string[] { "id" };
        GridView1.DataBind();
        sqlcon.Close();
    }

收获园豆:10
Astar | 高人七级 |园豆:40805 | 2010-06-11 08:58
写得很完美了
熊猫疯狂者 | 园豆:280 (菜鸟二级) | 2010-06-11 11:39
其他回答(1)
0

http://blog.csdn.net/21aspnet/archive/2007/03/25/1540301.aspx

邀月 | 园豆:25475 (高人七级) | 2010-06-11 08:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册