美文网首页
GridView控件

GridView控件

作者: Klart | 来源:发表于2017-11-01 09:55 被阅读13次

有时候人傻起来,连自己都觉得可怕。
今天需要做一个很简单的查询修改的功能,在老的系统上加一个新的功能,C#,原本想用jQuery运用ajax动态加载的,突然修改DOM不会了,瞬间尴尬了。吓得的我赶紧百度了一波。。。
但想想还是觉得用匹配一点的webform会好一点,于是就用了GridView控件了。

由于好久没写了,所以基本忘光了。

在gridview中绑定了一个textbox,
用((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim()
获取textbox控件中的值时报错,说是:无法将类型为
“System.Web.UI.LiteralControl”的对象强制转换为类型“System.Web.UI.WebControls.TextBox”,
将上面的写法改为:
((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].FindControl("TextBox1"))).Text.ToString().Trim()后,
又报错说是“未将对象的引用设置到对象的实例” ,最后将其改为:
((TextBox)(GridView1.Rows[i].Cells[1].Controls[0]).FindControl("TextBox1")).Text.Trim ().ToString ()),
运行成功!想了一下,确实有道理,
(TextBox)(GridView1.Rows[i].Cells[1].Controls[0])是强制类型转换,
转换为textbox控件,后面再跟findcontrol(),找到特定的textbox1控件,然后获取其值。

日了狗了,

前两天也是做一个表格,也想用ajax的方式向后台发请求,然请求发成功了,但后台也可以发数据,但前台获取不到,查了一下bootstrap table插件需要特定的json格式的数据,但我后台也是传json,百度一天,无果。先放一放,等有时间把他个挖透来。

还是用GridView写的
今天就把完整的GridView增删改查给写全来,省的忘记,不过确实忘记了,昨天花了一下午才搞定还加班到八点。
好了!牢骚发完了,开始写代码吧!

前台代码框奉上
<asp:GridView ID="GridView1" runat="server" CellPadding="3" ForeColor="Black" 
                GridLines="Vertical" AutoGenerateColumns="False" ShowFooter="True" 
                Width="99%"  BackColor="White" 
                BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" 
//这里的方法对应后台的方法
                onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" 
                Height="151px" onrowcancelingedit="GridView1_RowCancelingEdit" 
                OnPageIndexChanging="GridView1_PageIndexChanging"
                AllowPaging="True" onrowdeleting="GridView1_RowDeleting" >
                <Columns>
//因为需要编辑修改,所以用Templatefield
                    <asp:TemplateField HeaderText="题目ID(不可改)">
                        <ItemTemplate>
                            <asp:Label ID="Label12" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                        </ItemTemplate>
                        <FooterStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="題目類型">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox13" runat="server" Text='<%# Bind("topictype") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label13" runat="server" Text='<%# Bind("topictype") %>'></asp:Label>
                        </ItemTemplate>
                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"  />
                    </asp:TemplateField>
                    <asp:CommandField ShowEditButton="True" />
                    <asp:CommandField ShowDeleteButton="True" />
                </Columns>    
            </asp:GridView>
后台代码
//查询
 protected void 查询(object sender, EventArgs e)
    {
        if (txtSkuno.Text.Trim() == "")
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "alert('Input can not be null!');", true);
            return;
        }
        string strSkuno = txtSkuno.Text.Trim().ToUpper();
        string strSql =SQL语句;
        DataTable dt = Common.getDataBySQL(strSql);
        Session["TaskTable"] = dt;
        //把查到的数据存到Session里,以便后续编辑
        BindData();
    }
//绑定数据
    private void BindData()
    {
        GridView1.DataSource = Session["TaskTable"];
     }
//分页 前台需要把AllowPaging设为true,设置两次
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        btnSearchBySku_Click(e,e);
    }
    //修改
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string ID = ((Label)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).FindControl("Label12")).Text.Trim().ToString();
        string topictype = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).FindControl("TextBox13")).Text.Trim().ToString();
       问题?:需要退回到上一个界面//现在的问题是退回到上一个界面但是没有刷新数据
 GridView1.EditIndex = -1;
        //btnSearchBySku_Click(e, e);
        //Bind data to the GridView control.
        BindData();
    }
    //编辑
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindData();
    }
    //取消编辑
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;

        //Bind data to the GridView control.
        BindData();
    }
 //删除
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string ID = ((Label)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).FindControl("Label12")).Text.Trim().ToString();
        string strSql =删除语句
        DataTable ds = Common.getDataBySQL(strSql);
        Response.Write("<script language='JavaScript'>");
        Response.Write("alert('删除成功!');");
        Response.Write("</script>");
    }

最后还是有些小问题没有解决(虽然用了取巧的方式解决了)

相关文章

网友评论

      本文标题:GridView控件

      本文链接:https://www.haomeiwen.com/subject/jcznjxtx.html