13 Answers, 1 is accepted
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e) { if (e.CommandName == "YourCommandName") { // For Normal mode GridDataItem item = e.Item as GridDataItem; TextBox TextBox1 = item.FindControl("TextBox1") as TextBox; // Access your TextBox Here // For Edit mode GridEditableItem eitem = e.Item as GridEditableItem; TextBox TextBox2 = eitem.FindControl("TextBox2") as TextBox; // From Item Template TextBox TextBox3 = eitem["ColumnUniqueName"].Controls[0] as TextBox; // From Bound Column // Access your TextBox Here } }Thanks,
Jayesh Goyani
I have tried this code but i am getting an error like "Object reference not set to an instance of an object."
my code
Protected Sub rgdEditDevice_ItemCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles rgdEditDevice.ItemCommand If e.CommandName = "EditTest" Then Dim eitem As GridDataItem = TryCast(e.Item, GridDataItem) Dim item As GridDataItem = TryCast(e.Item, GridDataItem) Dim TextBox1 As RadTextBox = TryCast(eitem.FindControl("txtEditIP"), RadTextBox) Dim a As String = TextBox1.Text End If End Subhtml
<FormTemplate> <table> <tr> <td align="left" valign="top"> <telerik:RadTextBox ID="txtEditIP" Text='<%# Bind("IP") %>' runat="server" Width="100px"> </telerik:RadTextBox> </td> </tr> </table> </FormTemplate>please clarify any errors in this code
Regards,
Prassin
Can you please provide your RadGrid's structure/Code ?
So, we can help you to solve your issue.
Thanks,
Jayesh Goyani
This is the html
<telerik:RadGrid ID="rgdEditDevice" runat="server" CellSpacing="0" DataSourceID="SqlDataSource1" AllowFilteringByColumn="True" AllowPaging="True" AllowSorting="True" Width="565px" ShowStatusBar="True" AllowAutomaticInserts="True" AllowAutomaticDeletes="True" AllowAutomaticUpdates="True" EnableHeaderContextMenu="True" Height="340px" PageSize="14" GridLines="None" OnUpdateCommand="RadGrid1_UpdateCommand"> <GroupingSettings CaseSensitive="false" /> <FilterMenu EnableImageSprites="False"> </FilterMenu> <PagerStyle Mode="NumericPages" AlwaysVisible="true" /> <ClientSettings> <Scrolling AllowScroll="True" UseStaticHeaders="True" /> <Selecting AllowRowSelect="true"></Selecting> </ClientSettings> <MasterTableView AutoGenerateColumns="False" DataKeyNames="DeviceID" DataSourceID="SqlDataSource1" AllowAutomaticInserts="true" HierarchyDefaultExpanded="True" CommandItemDisplay="Top" CommandItemSettings-ShowRefreshButton="false"> <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings> <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column"> <HeaderStyle Width="20px"></HeaderStyle> </RowIndicatorColumn> <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column"> <HeaderStyle Width="20px"></HeaderStyle> </ExpandCollapseColumn> <Columns> <telerik:GridEditCommandColumn ButtonType="ImageButton"> <HeaderStyle Width="3%" /> </telerik:GridEditCommandColumn> <telerik:GridBoundColumn DataField="DeviceID" DataType="System.Int32" FilterControlAltText="Filter DeviceID column" HeaderText="DeviceID" ReadOnly="True" SortExpression="DeviceID" UniqueName="DeviceID" Visible="false"> </telerik:GridBoundColumn> <telerik:GridBoundColumn DataField="SerialNumber" HeaderText="SerialNumber" SortExpression="SerialNumber" UniqueName="SerialNumber" DataType="System.String" Visible="false" FilterControlAltText="Filter SerialNumber column"> </telerik:GridBoundColumn> <telerik:GridBoundColumn DataField="Port" FilterControlAltText="Filter Port column" HeaderText="Port" SortExpression="Port" UniqueName="Port" Visible="false" DataType="System.Int64"> </telerik:GridBoundColumn> <telerik:GridBoundColumn DataField="IP" HeaderText="IP" SortExpression="IP" UniqueName="IP" FilterControlAltText="Filter IP column" Visible="false"> </telerik:GridBoundColumn> <telerik:GridBoundColumn DataField="DeviceName" HeaderText="DeviceName" SortExpression="DeviceName" AllowFiltering="false" UniqueName="DeviceName" FilterControlAltText="Filter DeviceName column" HeaderStyle-Width="150px"> </telerik:GridBoundColumn> <telerik:GridBoundColumn DataField="DeviceRemark" FilterControlAltText="Filter DeviceRemark column" HeaderText="DeviceRemark" SortExpression="DeviceRemark" UniqueName="DeviceRemark"> </telerik:GridBoundColumn> <telerik:GridBoundColumn DataField="LocationID" FilterControlAltText="Filter LocationID column" HeaderText="LocationID" Visible="false" SortExpression="LocationID" UniqueName="LocationID" DataType="System.Int32"> </telerik:GridBoundColumn> <telerik:GridButtonColumn ConfirmText="Delete this employee?" ButtonType="ImageButton" CommandName="Delete" Text="Delete" UniqueName="DeleteColumn1"> <HeaderStyle Width="40px" /> <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" /> </telerik:GridButtonColumn> </Columns> <EditFormSettings EditFormType="Template"> <EditColumn FilterControlAltText="Filter EditCommandColumn column"> </EditColumn> <FormTemplate> <table> <tr> <td align="left" valign="top"> Device name: </td> <td align="left" valign="top"> <telerik:RadTextBox ID="txtDevice" Text='<%# Bind("DeviceName") %>' runat="server"> </telerik:RadTextBox> <asp:RequiredFieldValidator ID="rvName" runat="server" ControlToValidate="txtDevice" ErrorMessage="Should enter value" /> </td> <td align="left" valign="top"> IP: </td> <td align="left" valign="top"> <telerik:RadTextBox ID="txtEditIP" Text='<%# Bind("IP") %>' runat="server" Width="100px"> </telerik:RadTextBox> <%--<asp:TextBox ID="txtEditIP" Text='<%# Bind("IP") %>' runat="server" Width="100px"> </asp:TextBox>--%> </td> <td align="left" valign="top"> Port: </td> <td align="left" valign="top"> <telerik:RadTextBox ID="txtEditPort" Text='<%# Bind("Port") %>' runat="server" Width="100px"> </telerik:RadTextBox> </td> </tr> <%--<tr> <td align="left" valign="top" colspan="3"> <telerik:RadButton ID="RadButton1" runat="server" Text="Test Connect" CommandName="EditTest"> <Icon PrimaryIconCssClass="rbOk" PrimaryIconLeft="4" PrimaryIconTop="4" /> </telerik:RadButton> Serial: <telerik:RadTextBox ID="txtEditSerial" Text='<%# Bind("SerialNumber") %>' runat="server" Width="100px" Enabled="false"> </telerik:RadTextBox> </td> </tr>--%> <tr> <td align="left" valign="middle"> Remark: </td> <td align="left" valign="top" colspan="6"> <telerik:RadTextBox ID="txtRemarks" Text='<%# Bind("DeviceRemark") %>' runat="server" TextMode="MultiLine" Width="400px"> </telerik:RadTextBox> </td> </tr> <tr> <td> <telerik:RadButton ID="btnUpdate" Text='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "Insert", "Update") %>' runat="server" CommandName='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "PerformInsert", "Update")%>' Icon-PrimaryIconCssClass="rbOk"> </telerik:RadButton> </td> <td> <telerik:RadButton ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel" Icon-PrimaryIconCssClass="rbCancel"> </telerik:RadButton> </td> </tr> </table> </FormTemplate> </EditFormSettings> <PagerStyle AlwaysVisible="True" CssClass="footer-bg"></PagerStyle> </MasterTableView> </telerik:RadGrid>thanks,
Prassin
Try the following to achieve your scenario.
VB:
Protected Sub RadGrid1_ItemCommand(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs) If e.CommandName = "EditTest" Then Dim editedItem As GridEditableItem = DirectCast(e.Item, GridEditableItem) Dim txt As RadTextBox = DirectCast(DirectCast(editedItem, Telerik.Web.UI.GridDataItem).EditFormItem.FindControl("txtDevice"), RadTextBox) End IfEnd SubThanks,
Shinu.
try with below code snippet.
Protected Sub RadGrid1_ItemCommand(sender As Object, e As GridCommandEventArgs) If e.CommandName = "EditTest" Then Dim eitem As GridEditableItem = TryCast(e.Item, GridEditableItem) Dim txtEditIP As RadTextBox = TryCast(eitem.FindControl("txtEditIP"), RadTextBox) End IfEnd Sub'ORProtected Sub RadGrid1_ItemCommand(sender As Object, e As GridCommandEventArgs) If e.CommandName = "EditTest" Then Dim eitem As GridEditFormItem = TryCast(e.Item, GridEditFormItem) Dim txtEditIP As RadTextBox = TryCast(eitem.FindControl("txtEditIP"), RadTextBox) End IfEnd SubThanks,
Jayesh Goyani
Could you elaborate on your specific scenario? Generally, if you want to access controls in edit mode, you can use the approaches demonstrated in the following section:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows#accessing-controls-in-editinsert-mode
Regards,
Eyup
Telerik
please check this link. Thanks for reply
http://stackoverflow.com/questions/33784829/why-my-e-item-in-radgrid-itemcommand-event-always-null/33799873?noredirect=1#comment55399903_33799873
You are using e.Item as GridEditFormItem, but when the EditMode is InPlace, you need to use GridEditableItem editableItem = e.Item as GridEditableItem. Or GridDataInsertItem as explained in the provided section in my previous reply.
Alternatively, you can use the RadGrid1.MasterTableView.GetInsertItem() method.
Looking forward to hearing from you.
Regards,
Eyup
Telerik
Thanks a lot. I tried another way. I managed to put datasource and parameters on client side, and it works just fine. I gave up e.items things.
But I have another problem about raddropdownlist/ radcombobox in Radgrid returns null to parameters of datasource(insert and update). I've seen the doc of telerik demo. still can't find the problem.
I've already replied to your query in the following forum:
http://www.telerik.com/forums/raddropdownlist-in-radgrid-cause-datarowview-error
I suggest that we continue this new issue on the mentioned thread.
Regards,
Eyup
Telerik
I had the same issue. It never worked for me using FindControl on e.Item with any cast I tried, if the command was triggered by a button in FormTemplate and the control I was looking for was in same container (just next to this button).
Finally, it worked for me by using FindControl on e.CommandSource, which holds the initiating control. You just need to cast it to actual type first (e.g. Button or LinkButton) and up you go...
