Prev: How to use AjaxToolKit in asp.net for DHTML Editor
Next: Serializing objects to JSON with properties that return interfaces
From: DavidC on 12 Mar 2010 11:43 How can I get the RowIndex of a GridView when clicking on a checkbox in read-only mode? I have done it with the code below in a ListView but I cannot figure out how to do the same in a GridView. Thanks. Sub Check_Clicked(ByVal sender As Object, ByVal e As EventArgs) ' Gets the CheckBox object that fired the event. Dim chkBox As CheckBox = CType(sender, CheckBox) ' Gets the item that contains the CheckBox object. Dim item As ListViewDataItem = CType(chkBox.Parent, ListViewDataItem) Dim intTransID As Int32 = Convert.ToInt32(lvIncExpTrans.DataKeys(item.DataItemIndex).Value) If VendorClass.ChangeIncExpTransPick(intTransID, chkBox.Checked) = False Then Dim tb As TextBox = Page.Master.FindControl("txtMsg") ' Replace ListView1 with ID of your ListView Control 'tb.Text = "TransID sent = " & lvIncExpTrans.DataKeys(item.DataItemIndex).Value tb.Text = "The update to the checkbox failed." End If lvIncExpTrans.DataBind() End Sub -- David
From: Mr. Arnold on 12 Mar 2010 14:35 "DavidC" wrote: > How can I get the RowIndex of a GridView when clicking on a checkbox in > read-only mode? I have done it with the code below in a ListView but I > cannot figure out how to do the same in a GridView. Thanks. > It's back to the ItemDataBound. If that event is wired in the asp:DataGrid, then the bound data for the *one* row that was selected by the user will be selected, as it rolls through the rows. e.Item.ItemIndex is the row that the user selected. I suspect that you're going to have to set AutoPostBack="true" so the event will fier and comes to the codebehind file method. protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { e.Item.Attributes.Add("onmouseover", "this.style.cursor='hand'"); e.Item.Attributes.Add("onclick", "javascript:__doPostBack" + "('" + e.Item.UniqueID + "$ctl00' , 'Select$" + e.Item.ItemIndex + "')"); } } Or you may have to do something with client OnClick and javascript.
From: DavidC on 13 Mar 2010 11:16 I do have it set as autopostback in the GridView as shown below. Could I just add the DataKey field to the Check_Clicked function and read it from there? Thanks. <asp:TemplateField HeaderText="Cleared" SortExpression="Reconciled" ItemStyle-BackColor="White" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:CheckBox ID="ckReconciled" runat="server" Checked='<%# Bind("Reconciled") %>' onclick="fillclearedamt(this);" AutoPostBack="True" oncheckedchanged="Check_Clicked" /> </ItemTemplate> <ItemStyle BackColor="White" HorizontalAlign="Center" /> </asp:TemplateField> -- David "Mr. Arnold" wrote: > > > "DavidC" wrote: > > > How can I get the RowIndex of a GridView when clicking on a checkbox in > > read-only mode? I have done it with the code below in a ListView but I > > cannot figure out how to do the same in a GridView. Thanks. > > > > It's back to the ItemDataBound. > > If that event is wired in the asp:DataGrid, then the bound data for the > *one* row that was selected by the user will be selected, as it rolls through > the rows. > > e.Item.ItemIndex is the row that the user selected. > > I suspect that you're going to have to set AutoPostBack="true" so the event > will fier and comes to the codebehind file method. > > > protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e) > { > if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == > ListItemType.AlternatingItem) > { > e.Item.Attributes.Add("onmouseover", "this.style.cursor='hand'"); > e.Item.Attributes.Add("onclick", "javascript:__doPostBack" + > "('" + e.Item.UniqueID + "$ctl00' , 'Select$" + e.Item.ItemIndex > + "')"); > } > } > > > Or you may have to do something with client OnClick and javascript. >
From: Mr. Arnold on 13 Mar 2010 12:15 DavidC wrote: > I do have it set as autopostback in the GridView as shown below. Could I > just add the DataKey field to the Check_Clicked function and read it from > there? Thanks. > > <asp:TemplateField HeaderText="Cleared" SortExpression="Reconciled" > ItemStyle-BackColor="White" ItemStyle-HorizontalAlign="Center"> > <ItemTemplate> > <asp:CheckBox ID="ckReconciled" runat="server" > Checked='<%# Bind("Reconciled") %>' onclick="fillclearedamt(this);" > AutoPostBack="True" oncheckedchanged="Check_Clicked" /> > </ItemTemplate> > <ItemStyle BackColor="White" HorizontalAlign="Center" /> > </asp:TemplateField> > That's autopostback on the Grid control itself needs to be set to true. I don't see how an event of the checkbox has any knowledge of an event belonging to the Grid. They are two different controls with different event arguments. You need the event that happened on the Grid so that you can get e.item.itemindex. E.item.itemindex is a grid control event. You need to address a grid event, which should be the Grid_ItemDataBound() event. You go to the method and do a FindControl("ckReconcile"), and if it comes back not null on the find, then you have the hit on the row the control is bound in the grid and capture e.itemitemindex (the row), which can be saved to a hidden field and used later.
From: DavidC on 13 Mar 2010 14:31
I will give that a try. Thanks. -- David "Mr. Arnold" wrote: > DavidC wrote: > > I do have it set as autopostback in the GridView as shown below. Could I > > just add the DataKey field to the Check_Clicked function and read it from > > there? Thanks. > > > > <asp:TemplateField HeaderText="Cleared" SortExpression="Reconciled" > > ItemStyle-BackColor="White" ItemStyle-HorizontalAlign="Center"> > > <ItemTemplate> > > <asp:CheckBox ID="ckReconciled" runat="server" > > Checked='<%# Bind("Reconciled") %>' onclick="fillclearedamt(this);" > > AutoPostBack="True" oncheckedchanged="Check_Clicked" /> > > </ItemTemplate> > > <ItemStyle BackColor="White" HorizontalAlign="Center" /> > > </asp:TemplateField> > > > > That's autopostback on the Grid control itself needs to be set to true. > I don't see how an event of the checkbox has any knowledge of an event > belonging to the Grid. They are two different controls with different > event arguments. > > You need the event that happened on the Grid so that you can get > e.item.itemindex. E.item.itemindex is a grid control event. You need to > address a grid event, which should be the Grid_ItemDataBound() event. > > You go to the method and do a FindControl("ckReconcile"), and if it > comes back not null on the find, then you have the hit on the row the > control is bound in the grid and capture e.itemitemindex (the row), > which can be saved to a hidden field and used later. > > . > |