GridView commandname

来源:互联网 发布:建筑施工安全网络平台 编辑:程序博客网 时间:2024/04/30 09:20
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>


原创粉丝点击