1

由于某种原因,在我的代码中 container.DateItemIndex 没有在我的代码中返回任何值。

这是我的asp.net:

   <asp:Label ID="Label1" runat="server" Text=""></asp:Label>

<form id="form1" runat="server"> 
         <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
             AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1" 
             EnableModelValidation="True" onrowcommand="GridView1_RowCommand" OnRowUpdating="GridView1_RowUpdating" >

             <Columns>
                 <asp:CommandField ShowEditButton="True" ControlStyle-CssClass="savefile"/>
                 <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
                     ReadOnly="True" SortExpression="ID" />
                 <asp:BoundField DataField="event_name" HeaderText="event_name" 
                     SortExpression="event_name" />

                 <asp:TemplateField HeaderText="PDF">

           <ItemTemplate>

               <asp:Button ID="Button1" CommandArgument='<%# Container.DataItemIndex %>' CommandName="DownloadFile" runat="server" Text="Button" />

            </ItemTemplate>

           <EditItemTemplate>

               <asp:FileUpload ID="FileUpload1" runat="server" /> // shown only in edit mode

           </EditItemTemplate>

          </asp:TemplateField>

             </Columns>
         </asp:GridView>
</form>

和我的 C# 代码:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    if (e.CommandName == "DownloadFile")
    {

        Label1.Text = e.CommandArgument.ToString();

    }

}

为什么我没有从 CommandArgument='<%# Container.DataItemIndex %>' 获得任何值

4

4 回答 4

4

替换CommandArgument='<%# Container.DataItemIndex %>'CommandArgument='<%#DataBinder.Eval(Container, "DataItemIndex")%>'

于 2012-08-06T17:41:07.790 回答
1

尝试在RowDataBound上动态更改按钮属性:

    void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
      {

        if(e.Row.RowType == DataControlRowType.DataRow)
        {
          var button =  e.Row.FindControl("Button1");

          button.CommandArgument = e.Row.DataItemIndex;
          button.CommandName="DownloadFile";         
          button.Text="Button";
          // COLUMN_INDEX where button shoud be
          e.Row.Cells[COLUMN_INDEX].Controls.Add(button);
         }

      }
于 2012-08-06T17:42:34.560 回答
0

每隔一段时间,您需要获取触发事件的 GridView 中的行索引。您可以设置 CommandArgument 来存储行的索引,以便在后面的代码中使用。在网格视图中:

<asp:GridView ID="gridView" runat="server" DataKeyNames="myId" OnRowCommand=" gridView_RowCommand">
    <Columns>
        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:Button ID="btnAdd" runat="server" CommandArgument='<%# Container.DataItemIndex %>'
                    CommandName="Add" Text="Add" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

在代码隐藏中引用:

protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Add")
    {
        int myId = (int)gridView.DataKeys[Convert.ToInt32(e.CommandArgument)].Value;
    }
}
于 2012-08-06T17:56:18.083 回答
-1

试试这个……我一直都喜欢……

<asp:TemplateField HeaderText="SNo." >
                <ItemTemplate>
                    <%# Container.DataItemIndex + 1 %>
                </ItemTemplate>
</asp:TemplateField>
于 2014-10-14T10:26:27.467 回答