gridview等控件可以这样绑定字段

 /// <summary>
    /// 删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <= gvProducts.Rows.Count - 1; i++)
        {

            CheckBox cbox = (CheckBox)gvProducts.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true)
            {
                int ID = Convert.ToInt32(gvProducts.DataKeys[i].Value);
                clsIProduct Rpro = new clsRProduct();
                int m = Rpro.Delete(ID);
            }
        }
        Bind();
    }
    protected void lbtnUpdateRecommend_Command(object sender, CommandEventArgs e)
    {
      
    }

    /// <summary>
    /// 搜索
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    
   
    protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType==DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onMouseOver","SetNewColor(this);");
            e.Row.Attributes.Add("onMouseOut", "SetOldColor(this);");
        }
    }

 

 <asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
                GridLines="None" CellPadding="0" CssClass="talbe_list" OnRowDataBound="gvProducts_RowDataBound">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" />
                        </ItemTemplate>
                        <HeaderStyle CssClass="th20 tou" />
                        <ItemStyle CssClass="nolet" />
                    </asp:TemplateField>
                     <asp:TemplateField HeaderText="留言公司">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemStyle CssClass="left2" />
                        <ItemTemplate>
                            <%# PB_Dailiinfo(Convert.ToInt32(Eval("ID").ToString()))%>
                            <%# GetNewTitle(DataBinder.Eval(Container.DataItem, "RequestContent").ToString(), Eval("ID").ToString())%>
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="留言内容">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemStyle CssClass="left2" />
                        <ItemTemplate>
                            <%# GetNewTitle(DataBinder.Eval(Container.DataItem, "RequestContent").ToString(), Eval("ID").ToString())%>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="Phone" HeaderText="联系方式">
                        <HeaderStyle CssClass="th80" />
                    </asp:BoundField>
                    <asp:BoundField DataField="AddDate" HeaderText="日期">
                        <HeaderStyle CssClass="th80" />
                    </asp:BoundField>
                    <asp:BoundField DataField="UserName" HeaderText="留言人">
                        <HeaderStyle CssClass="th80" />
                    </asp:BoundField>
                    <asp:TemplateField HeaderText="操作选项">
                        <ItemTemplate>
                            <!-- <a href='ProductEdit.aspx?ID=<%# Eval("ID") %>'>编辑</a> |-->
                            <asp:LinkButton CommandName="delete" ID="lnkdelete" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID") %>'
                                OnCommand="lnkdelete_Command" runat="server" Text="删除" OnClientClick="return confirm('你确定要删除吗?')"></asp:LinkButton>
                        </ItemTemplate>
                        <HeaderStyle CssClass="th80" />
                    </asp:TemplateField>
                </Columns>
                <HeaderStyle CssClass="header" />
            </asp:GridView>
  public string PB_Dailiinfo(int id)
    {

        int msgid = 0;
        string gongsi = "";
        int chanpin = 0;
        string result = "";
        try
        {
            msgid = Convert.ToInt32(id);
            if (msgid > 0)
            {
                string sqlstr = "select [ProductId],[CompanyName] from W_VisitorRequest where ID='" + msgid + "'";
                DataTable dt = SqlOperate.SqlHelper.ExecuteSqlQuery(sqlstr);
                if (dt.Rows.Count > 0)
                {
                    try { gongsi = dt.Rows[0]["CompanyName"].ToString(); }
                    catch (Exception) { }
                    try { chanpin = Convert.ToInt32(dt.Rows[0]["ProductId"].ToString()); }
                    catch (Exception) { }
                    if (gongsi != "")
                    {
                        result = gongsi;
                    }
                    else if (chanpin != 0)
                    {
                        string sqlstr1 = "select * from C_Supply where ID='" + chanpin + "'";
                        DataTable dt1 = SqlOperate.SqlHelper.ExecuteSqlQuery(sqlstr1);

                        string gongsiname = "";


                        try { gongsiname = SqlOperate.SqlHelper.ExecuteScalar("select CompanyName from U_UserInfo where UserID='" + dt1.Rows[0]["UserID"] + "'").ToString(); }
                        catch (Exception) { }
                        result = gongsiname;
                    }
                }
                return result;
            }
            else
            {
                return null;
            }
        }
        catch (Exception) { return "没有此信息!"; }

    }

 

 private void Bind()
    {
        btnDelete.Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')");
        string strURl = Request.RawUrl;
        strURl = strURl.Substring(0, strURl.IndexOf(".aspx")) + ".aspx";
        PageNo1.Strurl = strURl;
        string strWhere = "";
        PageNo1.Table = "W_VisitorRequest";
        PageNo1.FldName = "AddDate";
        PageNo1.StrGetFields = "W_VisitorRequest.*";
        PageNo1.PageSize = 12;
        if (Request.QueryString["page"] != null && Request.QueryString["page"] != "")
        {
            PageNo1.Pageindex = Convert.ToInt32(Request.QueryString["page"].ToString());
            if (Session["strWhere"] != null)
                strWhere = Session["strWhere"].ToString();
            PageNo1.Where = strWhere;
            DataTable dt = PageNo1.BindViewDesc();
            if (dt.DefaultView.Count == 0)
            {
                lblMessage.Visible = true;
                lblMessage.Text = "暂无数据信息";
            }
            else
                lblMessage.Visible = false; 
            gvProducts.DataSource = dt.DefaultView;
            gvProducts.DataBind();
        }
        else {
            PageNo1.Where = "";
            Session["strWhere"] = "";
            PageNo1.Pageindex = 1;
            DataTable dt = PageNo1.BindViewDesc();
            if (dt.DefaultView.Count == 0)
            {
                lblMessage.Visible = true;
                lblMessage.Text = "暂无数据信息";
            }
            else
                lblMessage.Visible = false;
            gvProducts.DataSource = dt.DefaultView;
            gvProducts.DataBind();
        }
    }

 

转载于:https://www.cnblogs.com/ycbk/archive/2011/11/18/2254056.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值