天天看点

获取GridView中RowCommand的当前选中行的索引或主键Id

    这两天一直在考虑如何设置新闻发布系统的文章审核模式。初步思路是通过一个"Button"来控制审核状态,在绑定GridView的时候通过表中的字段isshow来设置"Button"的属性。如果isshow=1, 则  btn.Text = "已审核"; btn.Enabled = false;如果isshow=0,则   btn.Text = "审核";btn.Enabled = true; isshow=0是指文章刚被编辑好,管理员还未审核这篇文章是否正确,当管理员认为无误以后,点击“审核”按钮,将isshow设置为1,这样前台就能看到被审核过的文章。

       下面列出前台aspx中的关于GridView的代码:

<asp:GridView ID="pageGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="info_id"  

          OnRowCommand="pageGridView_RowCommand">  

          <Columns>  

              <asp:BoundField DataField="info_id" HeaderText="序号" SortExpression="info_id" HtmlEncode="False">  

                  <HeaderStyle BackColor="DarkGoldenrod" Font-Size="Small" ForeColor="White" />  

                  <ItemStyle Font-Size="Small" />  

              </asp:BoundField>  

              <asp:TemplateField HeaderText="标题">  

                  <ItemTemplate>  

                      <asp:HyperLink ID="HyperLink1" CssClass="biao_t" runat="server" Font-Size="Small"  

                          Target="_blank" Text='<%#DataBinder.Eval(Container.DataItem,"INFO_TITLE")%>'  

                          NavigateUrl='<%#"../eNews/showNews.aspx?info_id="+HttpUtility.UrlEncode(DataBinder.Eval(Container.DataItem,"INFO_ID").ToString())%>'>HyperLink</asp:HyperLink>  

                  </ItemTemplate>  

                  <ItemStyle HorizontalAlign="Left" />  

              </asp:TemplateField>  

              <asp:BoundField DataField="info_addtime" HeaderText="时间" SortExpression="info_addtime"  

                  DataFormatString="[{0:yyyy-MM-dd}]" HtmlEncode="False">  

              <asp:TemplateField HeaderText="删除">  

                      <asp:CheckBox ID="CheckBox1" runat="server" />  

                  <HeaderTemplate>  

                      <input id="chkSelectAll" onclick="ChooseAll()" type="checkbox" name="chkSelectAll"><font  

                          size="2">全选</font>  

                  </HeaderTemplate>  

              <asp:TemplateField HeaderText="编辑">  

                      <asp:HyperLink ID="HyperLink2" runat="server" CssClass="sh_hr" NavigateUrl='<%#"editNews.aspx?info_id="+HttpUtility.UrlEncode(DataBinder.Eval(Container.DataItem,"INFO_ID").ToString())%>'  

                          Text="编辑">  

                      </asp:HyperLink>  

              <asp:TemplateField HeaderText="审核状态">  

                      <asp:Button ID="Button1" runat="server" Text="Button" CommandArgument='<%#Eval("info_id") %>'  

                          CommandName="approval" />  

          </Columns>  

      </asp:GridView>  

      从中我们看出我们绑定了“序号”“标题”“时间”“全选”“编辑”“审核”这一些字段。有些直接就是数据库中的字段,比如“序号”和“时间”。而有些则不是,可能是超链接,也可能是按钮等等。

      下面我们看pageGridView_RowCommand()中的定义:

protected void pageGridView_RowCommand(object sender, GridViewCommandEventArgs e)  

   {  

       if(e.CommandName=="approval")  

       {  

           //取ID的值方法一   success  

           GridViewRow drv = ((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值  

           int id = Convert.ToInt32(pageGridView.DataKeys[drv.RowIndex].Value); //此获取的值为GridView中绑定数据库中的主键值  

           //取ID的值方法二   只有当GridView中有info_id字段是才成功  

           //此获取的值为GridView中绑定数据库中的主键值,取值方法是选中的行中的第一列的值,drv.RowIndex取得是选中行的索引  

           int id = Convert.ToInt32(pageGridView.Rows[drv.RowIndex].Cells[0].Text);  

           //取ID的值方法三  success  

           //因为在客户端中就已经将LinkButton的CommandArgument与主键Id给绑定了所以在此可以直接用e.CommandArgument得出主键ID的值  

           int id = Convert.ToInt32(e.CommandArgument.ToString());   

           if (NewsBus.setIsShow(id))  

           {  

               this.Label1.Text = "成功!";  

               this.GridViewBind();  

           }  

           else  

               this.Label1.Text = "失败!";  

       }  

   }  

      其中方法一和方法三如注释中显示的,都通过了验证,只有方法二情况比较特殊,pageGridView.Rows[drv.RowIndex].Cells[0].Text获得的只是GridView表格中的第“索引”行的第一列,如果我们没有在GridView中添加

<asp:BoundField DataField="info_id" HeaderText="序号" SortExpression="info_id" HtmlEncode="False">  

                   <HeaderStyle BackColor="DarkGoldenrod" Font-Size="Small" ForeColor="White" />  

                   <ItemStyle Font-Size="Small" />  

               </asp:BoundField>  

那么就获取不到当前行的主键,所以方法二的应用是有前提的。我们这里就没有添加“序号”列,所以使用了方法三。

本文转自xwdreamer博客园博客,原文链接:http://www.cnblogs.com/xwdreamer/archive/2010/03/28/2297144.html,如需转载请自行联系原作者