VS2005中GridView簡單應用

[查詢]按鈕:查詢數據庫 ,顯示信息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 對[刪除]點擊按鈕的事件進行服務器端處理!

posted on 2006-02-09 17:29 freeliver54 阅读(583) 评论(4)  编辑 收藏 引用 网摘 所属分类: VS技術實踐

<script type="text/javascript"> // </script>

评论

 

#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  
回复  更多评论   

# re: VS2005中GridView簡單應用 2007-01-17 08:45 freeliver54

一般情况下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();  回复  更多评论   

# re: VS2005中GridView簡單應用 2007-01-17 08:46 freeliver54

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();  回复  更多评论   

# re: VS2005中GridView簡單應用 2007-01-29 11:49 freeliver54

自定义数据绑定:
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();
}  回复  更多评论   

  #  re: VS2005中GridView簡單應用 2006-12-30 19:04 freeliver54
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值