473,486 Members | 2,429 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

DataGrid.SelectedIndex is always -1

js
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>

Nov 19 '05 #1
0 1410

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
35932
by: R Agam | last post by:
How do I get the selected Rows index in the DataGrid? Thanks, Ronen
0
1899
by: Shravan | last post by:
Hi, I have a custom datagrid extended from .Net datagrid, where I want vertical scrollbar to be visible always, for that I handled visiblechanged of VerticalScrollbar and making it visible...
0
1135
by: Jeff Reed | last post by:
I have this in my coding for a DataGrid: <EditItemTemplate> <asp:DropDownList id="eCategoryList" runat="server" CssClass="selectBoxSmall" SelectedIndex='<%# DataBinder.Eval(Container.DataItem),...
0
885
by: Alberto Teixeira | last post by:
Hi, I have a bound datagrid. I need to be able to select a row by clicking any of the cells and have it fire of the SelectedIndexChanged event. How can I do this? Thanks Alberto
5
3380
by: js | last post by:
I am having a problem with a dynamicly added DataGrid that always shows the first data page even the message in the DataGrid.PreRender shows the correct datapage as the DropDownList control's...
7
3559
by: Joel Reinford | last post by:
I am looking for a way to set the selectedindex of a datagrid based on the datakey value. For example given this grid with a datakey of OrderID: OrderID Item 54 A 98 ...
2
1140
by: Ryan Liu | last post by:
private void dt_RowDeleted(object sender, DataRowChangeEventArgs e) { int id= (int)e.Row; if(id >5) { e.Row.RejectChanges(); MessageBox.Show(this, "Can not delete!" + id); }
0
1318
by: Kim | last post by:
Im having a problem with one of my datagrids. Basically the datagrid has paging included in it and when a user clicks on page other than the first page and the user selects a row the value is...
1
1785
by: pieandpeas | last post by:
he selectedindex of my gridview is always one behind for example, if i click on the EDIT.gif on row 1, then 5 , then 7, if i break the code and check the selectedindex, it is equal to 5. if i...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7132
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7180
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5439
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4870
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4564
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
600
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
266
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.