gridview的简单介绍

GridView控件是Asp.net 1.1版本流行控件DataGrid的继承者,功能比DataGrid增强不少,但是也有很大的不同啊。将最近使用这个控件的经验同各位同学分享如下:
   1/掩藏字段的处理:DataGrid可以将字段直接设置为Visible=false,可以通过Cell[x].Text取到值。 GridView这个功能失效了,可以使用运行时来设定该列为掩藏。处理RowDataBound事件。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
   e.Row.Cells[5].Visible = false;
}
   2/ 获取所选列的数据:DataGrid可以直接通过所选行来获取,GridView同样的代码无法运行。GridView 可以通过GridViewRow来获取。BtnAudit是模版列中的按钮。
GridViewRow grdRow = (GridViewRow)btnAudit.Parent.Parent;

GridView是VS2005中對VS2003的DataGrid的增強替代控件
下面展示一下它的基本常見應用
效果圖如下:

[查詢]按鈕:查詢數據庫 ,顯示信息Table 並 綁定GridView
//查詢按鈕
protected void btnQue_Click(object sender, EventArgs e)
 {
this.tableInfo.Visible = true;
SqlConnection sqlconn = new SqlConnection("server=localhost;database=db;uid=uid;pwd=pwd;");
sqlconn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from table", sqlconn);
DataSet ds = new DataSet();
sda.Fill(ds);
this.grvInfo.DataSource = ds;
this.grvInfo.DataBind();
sda.Dispose();
ds.Dispose();
sqlconn.Close(); 
}

[全選]按鈕:
  //全選
    protected void btnAllCh_Click(object sender, EventArgs e)
    {
        foreach(GridViewRow currow in grvInfo.Rows)
        {
            ((CheckBox)currow.Cells[0].Controls[1]).Checked = true;
        }
    }
類似的[取消全選]的編碼為:
    // 取消全選
    protected void btnNoCh_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow currow in grvInfo.Rows)
        {
            ((CheckBox)currow.Cells[0].Controls[1]).Checked = false;
        }
    }
[明細]按鈕:
該按鈕的操作,要寫在GridView ROW按鈕事件--- RowCommand
    // GridView ROW按鈕事件 RowCommand
    protected void grvInfo_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       if(e.CommandName.Equals("mingxin"))
        {          
        ImageButton lb = (ImageButton)e.CommandSource;
        GridViewRow curgvr = (GridViewRow)lb.Parent.Parent;
        string paraValue = curgvr.Cells[2].Text.ToString();
        //RegisterClientScriptBlock("openmingxin", "<script language='javascript'>window.open(src='mingxin.aspx?pihao="+ paraValue+"','newwindow','');</script>");    
        ClientScript.RegisterClientScriptBlock(this.GetType(), "aa", "<script language='javascript'>alert("+paraValue +"');</script>");
        }
    }     
[刪除]按鈕:
可以在HTML原始檔中加上
<asp:BoundField HeaderText="審核" />
<asp:TemplateField HeaderText="刪除">
<ItemStyle  HorizontalAlign="Center" />                           
<ItemTemplate>
<asp:ImageButton runat="server" ImageUrl="~/image/del.JPG" ID="delid"  CommandName="del"  OnClientClick="return confirm('確實要刪除嗎?');" />
</ItemTemplate>
</asp:TemplateField>
同時也可以如[明細]按鈕在GridView ROW按鈕事件--- RowCommand 對[刪除]點擊按鈕的事件進行服務器端處理!
============================
GridView中 样板列 加入 按钮
============================
前台:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
    <Columns>
        <asp:BoundField DataField="personName" />
        <asp:BoundField DataField="personAge" />
        <asp:TemplateField HeaderText="操作">
        <ItemTemplate>
            <asp:Button ID="btn_OK" runat="server" Text="确定"
            CommandArgument='<%# Eval("personName") %>' CommandName="btn_OK" />
            <asp:Button ID="btn_Cancel" runat="server" Text="取消" CommandName="btn_Cancel"
            CommandArgument='<%# Eval("personName") %>' />
        </ItemTemplate>
       </asp:TemplateField>
    </Columns>
</asp:GridView>
后台:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    //直接利用参数
    string strName = e.CommandArgument.ToString();
    //一种取得当前行的方法
    GridViewRow currRow = (GridViewRow)((Button)e.CommandSource).Parent.Parent;
    string strAge = currRow.Cells[1].Text.ToString();
    if (e.CommandName == "btn_OK")
    {
        this.TextBox1.Text = "确定按钮 " + strName + " " + strAge;
    }
    if (e.CommandName == "btn_Cancel")
    {
        this.TextBox1.Text = "取消按钮 " + strName + " " + strAge;
    }
}

 

#region gv_showResult 数据绑定时的事件
/// <summary>
/// gv_showResult 数据绑定时的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gv_showResult_RowDataBound(object sender, GridViewRowEventArgs e)
{
//设定宽度
e.Row.Cells[0].Width = 240;
e.Row.Cells[1].Width = 340;

if (e.Row.RowIndex == -1)
return;
//加载鼠标单击事件
string strID = e.Row.Cells[0].Text.Trim();
string strName = e.Row.Cells[1].Text.Trim();
string strJS = "document.all.txt_ID.value='" + strID + "';";
strJS += "document.all.hidtxt_ID.value='" + strID + "';";
strJS += "var tmpName = escape('" + strName + "');";
strJS += "document.all.txt_Name.value=unescape(tmpName);";
//加载点击后 变色
strJS += "OnSelectBgColor(this,gv_showResult)";
e.Row.Attributes.Add("onclick", strJS);
e.Row.Attributes.Add("onmouseover", "mouseover(this);");
e.Row.Attributes.Add("onmouseout", "mouseout(this,gv_showResult);");

}
#endregion 

一般情况下GridView 隐藏列
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="hidtxt_currID" runat="server" Text='<%# Bind("currID") %>' style="display:none"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
后台抓值
string strCurrID = ((TextBox)this.gv_Selected.Rows[i].Cells[0].FindControl("hidtxt_currID")).Text.Trim();  

ajax情况下GridView 隐藏列
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="hidtxt_currID" runat="server" Text='<%# Bind("currID") %>' style="display:none"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
注意<HeaderTemplate>也需要display:none
后台抓值
string strCurrID = ((TextBox)this.gv_Selected.Rows[i].Cells[0].FindControl("hidtxt_currID")).Text.Trim();  

自定义数据绑定:
VS2003 DataGrid 的 ItemDataBound 事件:
e.Item.Cells[0].Text=((String)DataBinder.Eval(e.Item.DataItem, "Name"));
VS2005 GridView 的 RowDataBound 事件:
if (e.Row.RowIndex >= 0)
{
DropDownList tmpDrpSex = (DropDownList)e.Row.FindControl("drp_Sex");
tmpDrpSex.Items.Clear();
tmpDrpSex.Items.Add(new ListItem("女","0"));
tmpDrpSex.Items.Add(new ListItem("男","1"));
tmpDrpSex.SelectedValue = DataBinder.Eval(e.Row.DataItem, "ASex").ToString();
}

//某个字段的内容太长时
//在GridView上的该字段上显示截断前多少字的内容
//当鼠标放上时 弹出小提示框 显示完整内容
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex == -1)
return;
string strAllContent = ((TextBox)e.Row.Cells[0].FindControl("hidtxt_AllContent")).Text.Trim();
e.Row.Cells[5].Attributes.Add("title", strAllContent);
}

//鼠标经过时
//显手形
e.Row.Attributes.Add("onmouseover", "this.style.cursor='hand'");
e.Row.Attributes.Add("onmouseout", "this.style.cursor=''"); 

 string strId = grdRow.Cells[0].Text;
 string memberId = grdRow.Cells[5].Text;
  3/ 最终删除一条数据之前进行确认,这个可以使用摸版列,在摸版列中放置按钮控件,其中有一个客户端事件onclientclick,这里可以写确认处理javascript脚本.例如:
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="btnRefuse" runat="server" OnClick="btnRefuse_Click" Text="拒绝"  OnClientClick="return confirm(' 你真的要拒绝这个用户加入俱乐部?')"/>
                </ItemTemplate>
            </asp:TemplateField>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值