Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > DataGrid.SelectedIndex is always -1

Reply
Thread Tools

DataGrid.SelectedIndex is always -1

 
 
js
Guest
Posts: n/a
 
      05-19-2005
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>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wireless Lan on/off switch always litup Terry Wireless Networking 1 08-14-2005 07:15 PM
File copy always interrupted over wireless network =?Utf-8?B?TWF0cml4Y3ViZWQ=?= Wireless Networking 1 02-28-2005 06:00 PM
Why do I always have to repair my network connection after rebooti =?Utf-8?B?Q29uZnVzZWRfQ29uc3VtZXI=?= Wireless Networking 2 11-28-2004 02:24 PM
belkin 54g always works on 11 Mbps hans Wireless Networking 2 11-03-2004 01:17 PM
Trying to create a CSS box that is always is always the width of an image placed inside it (and no wider) Deryck HTML 4 06-22-2004 08:25 PM



Advertisments