DataGrid.SelectedIndex is always -1

Discussion in 'ASP .Net' started by js, May 19, 2005.

  1. js

    js Guest

    I have a DataGrid with a command button on the first column of the
    DataGrid. The DataGrid is databound to a dataset with DataKeyFiled
    bound to "ControlNumber" field in the dataset. I am trying to save the
    row's ControlNumber to a page level variable when the corresponding
    item command is clicked . However, no matter what I do, the page
    property is alway blank.

    I also tried use CheckBox in the ItemTemplate. The CheckBox's
    AutoPostBack is set to false. Another Command button outside the
    DataGrid is used do the postback. Strangely, even I have one box
    checked, upon post back the SelectedIndex is always -1.

    Does anyone know how to save the "ControlNumber" to the page variable
    strControlNumber? Thanks.

    The following is the event handler for the DataGrid's OnItemCommand
    event.

    ////////// CodeBehind partial content /////////
    public class CustomPaging : System.Web.UI.Page
    protected string strControlNumber;
    public string ControlNumber
    {
    get
    {
    if (strControlNumber == null)
    return "no value";
    else
    return strControlNumber;
    }
    }
    private void MyDataGrid_ItemCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    TableCell CellControlNumber = e.Item.Cells[1];
    strControlNumber = CellControlNumber.Text;
    Server.Transfer("inventory.aspx");
    }

    private void btnConfirmSelection_Click(object sender, System.EventArgs
    e)
    {
    strControlNumber = MyDataGrid.SelectedIndex.ToString();
    Server.Transfer("inventory.aspx");
    }

    /////////// ASPX partial content /////////
    <asp:button id="btnConfirmSelection" runat="server" Text="Confirm
    Selection"/>
    <asp:datagrid id="MyDataGrid" runat="server" CssClass="InventoryTable"
    AutoGenerateColumns="False"
    AllowSorting="True">
    <Columns>
    <asp:TemplateColumn>
    <ItemTemplate>
    <asp:Checkbox ID="chkSelected" Runat="server"/>
    <asp:Button Text="Select" runat="server" />
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn>
    <HeaderTemplate>Control<br />Number</HeaderTemplate>
    <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "ControlNumber") %>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:datagrid>
    js, May 19, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Reed

    DataGrid SelectedIndex type problem

    Jeff Reed, Nov 19, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    351
    Jeff Reed
    Nov 19, 2003
  2. S. Justin Gengo

    ListBox SelectedIndex is always -1

    S. Justin Gengo, Feb 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,750
  3. K Bryan
    Replies:
    4
    Views:
    2,447
    Andrew de la Harpe
    Apr 15, 2004
  4. Deryck
    Replies:
    4
    Views:
    508
    derek giroulle
    Jun 22, 2004
  5. b_gul_t

    ListBox SelectedIndex is always -1

    b_gul_t, Mar 5, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    239
    b_gul_t
    Mar 7, 2005
Loading...

Share This Page