zoukankan      html  css  js  c++  java
  • gridview问题解答 :按钮的Enalbe状态 / 首页\尾页\上一页等 / 显示短时间,年\月\日 / 数据库存是1和0,显示为男和女等!

    用一个GridView显示的数据表,表中一些行可以删除,另外的行不能删除。

    解决:

    1 数据源: sqldatasource在获取数据时,计算数据是否可以删除。这里我使用的是一个sql的function。下面是我的selectcommand:

    SELECT [ContactID], [LastName], [FirstName], [EnglishName], [WorkPhone], [MobilePhone], [Email], [Address], [City], [Province], dbo.CanDelContact([ContactID]) as CanDel  FROM [ContactInfo]

    2 GridView中设置数据模板列的Enable属性:

                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:Button ID="ButtonDelete" runat="server" CommandName="Delete"  Text="Delete" Enabled='<%# System.Convert.ToInt32(Eval("CanDel")) > 0 %>' />
                                    </ItemTemplate>
                                </asp:TemplateField>

    <%# System.Convert.ToInt32(Eval("CanDel")) > 0 %> 是在数据绑定时自动计算的。<%#表示使用数据绑顶。你可以在<%%>中间使用C#的语句进行计算。

    要注意的是:GridView的asp:CommandField是不支持数据绑定的,我试过

    <asp:CommandField ShowDeleteButton=<%# System.Convert.ToInt32(Eval("CanDel")) > 0 %>  ButtonType =Button  />

    系统便以出错,因为CommandField不支持数据绑定。

    所以只好增加一个模板列来解决这个问题。只要设置CommandName="Delete" ,就可以了。

    ******************************************************************

    <%# PrintIsOpen(Convert.ToBoolean(DateBinder.Eval(Container.DataItem,"isopen")))%>
    cs代码是这样的:
    public string PrintIsOpen(bool bIsOpen)
    {
    string strHtml="";
    if(bIsOpen)
    {
    strHtml="<img src=1>";
    }
    else
    {
    strHtml="<img src=2>";
    }
    return strhHtml;
    }

    或者说是这样的,如果我没看错你的这个判断是布尔值。
    <%# Convert.ToBoolean(DateBinder.Eval(Container.DataItem,"isopen"))=true ?  "img":"<img>"%>

     

    首页\尾页\上一页\下一页\当前第几页\总共几页\跳转到第几页(下拉框)

    protected void lnkPre_Click(object sender, EventArgs e)
        {
            if (this.GridView1.PageIndex > 0)
            {
                this.GridView1.PageIndex = this.GridView1.PageIndex - 1;
                this.GridView1.DataBind();
            }
        }
        protected void lnkNext_Click(object sender, EventArgs e)
        {
            if (this.GridView1.PageIndex < this.GridView1.PageCount)
            {
                this.GridView1.PageIndex = this.GridView1.PageIndex + 1;
                this.GridView1.DataBind();
            }

        }
        protected void lnkLast_Click(object sender, EventArgs e)
        {

            this.GridView1.PageIndex = this.GridView1.PageCount;
            this.GridView1.DataBind();
        }
        protected void lnkFirst_Click(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = 0;
            this.GridView1.DataBind();

     

        }

    protected void GridView1_DataBound(object sender, EventArgs e)
        { 
            int i = this.GridView1.PageIndex + 1;
            labTotal.Text = "第" + i.ToString() + "页/" + "共" + this.GridView1.PageCount.ToString() + "页";
        }


    <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton runat="server" CommandName="Delete" OnClientClick="return confirm('真的删除吗');" Text="删除"></asp:LinkButton>
                    </ItemTemplate>
              </asp:TemplateField>


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
          foreach (GridViewRow row in GridView1.Rows)
            {
                Button delButton =(Button)row.Cells[0].Controls[0];
                delButton.Attributes.Add("onclick", "return confirm('你确认删除吗?');");
            }
       }


    gridView在显示时间时怎么样显示为短时间,即只显示年\月\日
    DataFormatString="{0:d}"
    DataFormatString="{0:yyyy/MM/dd}"
    HtmlEncode="False"
    GridView中怎么对一个字符型列进行格式化?
    <%# DataBinder.Eval(Container.DataItem,"Field").ToString().Substring(0,5)%>
    <asp:HyperLinkColumn HeaderText="sdf" DataNavigateUrlField="id" DataNavigateUrlFormatString="javascript:alert({0})"  DataTextField="id" >
    HtmlEncode="false"

    我想在GRIDVIEW的第一列加个编号 编号规则就是 1、2、3、4、5。。。。。。请问怎么做?
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = e.Row.DataItemIndex.ToString();
            }
       }


    GridView中有个列是男1 女0,数据库存是1和0,怎么显示为男和女呢?
    用模板列:
    <asp:TemplateColumn HeaderText="性别">
          <ItemTemplate>
               <asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Sex").ToString()="1"?"男":"女" %>' ID="Label1">
               </asp:Label>
          </ItemTemplate>
    </asp:TemplateColumn>

  • 相关阅读:
    如何解决IIS配置HTTPS证书后刷新消失问题
    Nginx https服务器证书安装步骤
    Websocket如何建立连接
    Websocket原理
    浅谈Vue中的$set的使用
    vue-router路由如何实现传参
    vue-router路由详细
    vue 中注册全局组件
    java基础
    练习例题(进度条效果和选项卡效果)
  • 原文地址:https://www.cnblogs.com/Fooo/p/596776.html
Copyright © 2011-2022 走看看