Container.DataItemIndex不在gridview内部工作

时间:2012-08-06 17:08:19

标签: c# asp.net

由于某种原因,我的代码容器.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 ='&lt;%#Container.DataItemIndex%&gt;'

获取任何值

4 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分: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);
         }

      }

答案 2 :(得分:0)

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

<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;
    }
}

答案 3 :(得分:0)

尝试这个.....我总是那样......

<asp:TemplateField HeaderText="SNo." >
                <ItemTemplate>
                    <%# Container.DataItemIndex + 1 %>
                </ItemTemplate>
</asp:TemplateField>