GridView里有个列,我在里面用了一个 LinkButton 
 <asp:TemplateField HeaderText="员工">
                <HeaderStyle HorizontalAlign="Left" Width="50px" />
                <ItemTemplate>
                    <asp:LinkButton ID="lbtnyg" runat="server" CommandArgument='<%# Eval("id") %>' OnClick="lbtnyg_Click"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
我知道可以获取 LinkButton的CommandArgument,可是我还需要一个参数,所以我在GridView的 DataKeyNames="id,name"设置了两个参数
 protected void lbtnyg_Click(object sender, EventArgs e)
    {        int area = Convert.ToInt32(((LinkButton)(sender)).CommandArgument);    }
我希望能在 lbtnyg_Click 获取 DataKeyNames的name参数,有没朋友有做过

解决方案 »

  1.   

    GridView 里面的 LinkButton?
    你直接用GridView 的_RowCommand事件就好了,GridView里面的任何按钮都会触发该事件
    在RowCommand事件里获取值的方法:
    int area = Convert.ToInt32(e.CommandArgument.ToString());
    GridViewRow row = (e.CommandSource as Control).NamingContainer as GridViewRow;
    string name = gridviewName.DataKeys[row.RowIndex].Values["name"].ToString();
      

  2.   

    gridview里面放一个隐藏域<asp:HiddenField ID="LnName" runat="server" Value='<%#Eval("Name") %>' />
    然后在lbtnyg_click事件中 FOR循环一下找到Name
      for (int i = 0; i < this.grLeaveWord.Rows.Count; i++)
                {
         string name= ((HiddenField)grLeaveWord.Items[i].FindControl("LnName")).Value;
                } 
    用这种方法试试
      

  3.   

    string name = gridviewName.DataKeys[row.RowIndex].Values["name"].ToString();
    没有row.RowIndex啊
      

  4.   

    不是吧用我的方法这里的DataKeyNames="id,name"就不需要name了
    不是加了一个隐藏域了吗
      

  5.   

    string name = gridviewName.DataKeys[row.RowIndex].Values["name"].ToString(); 
    没有row.RowIndex啊
    ------------------------
    在GridView的RowCommand事件里操作
    不要用LinkButton按钮的OnClick事件处理,因为在GridView中任何按钮的Click事件都会触发GridView的RowCommand事件
    具体代码:
    protected void gridviewName_RowCommand(object sender, GridViewCommandEventArgs e)
    {
      int area = Convert.ToInt32(e.CommandArgument.ToString()); 
      GridViewRow row = (e.CommandSource as Control).NamingContainer as GridViewRow; 
      string name = gridviewName.DataKeys[row.RowIndex].Values["name"].ToString;
    }
      

  6.   

    我知道可以获取 LinkButton的CommandArgument
    ====
    谁说commandArgument不能绑定两个字段了?
    你在获取时再字符串处理一下.
      

  7.   

    当然commandArgument属性声明语句中你要写绑定字段的
      

  8.   

    我知道可以获取 LinkButton的CommandArgument 
    ==== 
    谁说commandArgument不能绑定两个字段了? 
    你在获取时再字符串处理一下.
    /**************************************************
    绑定 id.和name要怎么绑定啊
    <asp:LinkButton ID="lbtnyg" runat="server" CommandArgument='<%# Eval("id") %>' CommandName="lbtnygOpen"></asp:LinkButton>
    然后怎么获取
    int area = Convert.ToInt32(((LinkButton)(sender)).CommandArgument);