GridView commandname
生活随笔
收集整理的這篇文章主要介紹了
GridView commandname
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 在asp.net2.0中,當需要在GridView的ItemDataBound之類的事件中需要獲取當前行的一些關聯性的數據值,但這些數據值又不能直接體現在GridView的列中顯示出來,這時可以采用DataKeyNames的方式來獲取此類數據,看下面的代碼示例:Code
//前臺代碼:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="Grup" OnRowCommand="GridView1_RowCommand" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("GrupName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField Text="按鈕" />
</Columns>
</asp:GridView>
//其中:Grup為我們想使用但不需要顯示的列。(如果有多個字段,使用逗號分開)//后臺代碼:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack )
{
DataTable dt = new DataTable();
dt.Columns.Add("Grup");
dt.Columns.Add("GrupName");dt.Rows.Add(new object[] { 0,"營業部" });
dt.Rows.Add(new object[] { 1,"市場部" });this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
}protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
// 獲取當前行索引
int index = Convert.ToInt32(e.CommandArgument);// 取出當前行數據鍵值對象中的值
string strGrup = ((GridView)sender).DataKeys[index].Values["Grup"].ToString();
}2 如果使用模板列中放置按鈕控件的方式,要想在按鈕事件中獲取對應行的字段值:Code
//按鈕的CommandArgument屬性設置為想綁定的字段,如: <asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" CommandArgument=' <%#Eval("Grup") %>' />
</ItemTemplate>
</asp:TemplateField> //按鈕事件: protected void Button2_Click(object sender, EventArgs e)
{
string strGrup = ((Button)sender).CommandArgument.ToString();
}
gridview CommandArgument 傳多個參數
<asp:TemplateField HeaderText="操作" SortExpression="applytypeid"><ItemTemplate><asp:Button ID="btnApply" CssClass="button" runat="server" CausesValidation="false"CommandName="Apply" CommandArgument='<%#String.Format("{0}_{1}",Eval("basemodelid"),Eval("ApplyID")) %>' Visible='<%#Eval("applytypeid").ToString()=="1" ?false:true %>' Text="申請CR表單" /><asp:Button ID="btnPriceApply" CssClass="button" runat="server" CausesValidation="false"CommandName="PriceApply" CommandArgument='<%#String.Format("{0}_{1}",Eval("basemodelid"),Eval("ApplyID")) %>' Text="價格申請單" /><asp:Button ID="btnDispose" CssClass="button" runat="server" CausesValidation="false"CommandName="Dispose" CommandArgument='<%#String.Format("{0}_{1}",Eval("basemodelid"),Eval("ApplyID")) %>' Text="配置申請單" /></ItemTemplate></asp:TemplateField>
總結
以上是生活随笔為你收集整理的GridView commandname的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: commandname+commanda
- 下一篇: MAC下载Android源码下载 And