由于我的代码容器中的某些原因,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 =''中获取任何值

最佳答案

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

关于c# - Container.DataItemIndex在GridView内部不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11832785/

10-11 23:36