This is a migrated thread and some comments may be shown as answers.

[Solved] can't get the value of controls from grid

2 Answers 146 Views
Grid
This is a migrated thread and some comments may be shown as answers.
jlesidt
Top achievements
Rank 1
jlesidt asked on 29 Jun 2009, 07:25 PM
I have a created a radgrid which takes data from a table and displays it in rows. Each row  has an edit button which when clicked opens details of that row. The user should be able to update this.

The aspx code has the radgrid part:

 <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="EditDataSource" GridLines="None" OnItemCommand="RadGrid1_ItemUpdated">

    <MasterTableView AllowAutomaticUpdates="false" AllowAutomaticInserts="false" autogeneratecolumns="False" datasourceid="EditDataSource">

    <columns>

        

            <telerik:GridEditCommandColumn UniqueName="EditCommandColumn">

            </telerik:GridEditCommandColumn>

             <telerik:GridBoundColumn HeaderText="OrganizationName" HeaderButtonType="TextButton" DataField="OrganizationName">

            </telerik:GridBoundColumn>

             <telerik:GridBoundColumn HeaderText="OrganizationName" HeaderButtonType="TextButton" DataField="ContactID">

            </telerik:GridBoundColumn>

            <telerik:GridBoundColumn HeaderText="firstname" HeaderButtonType="TextButton" DataField="FirstName">

            </telerik:GridBoundColumn>

            <telerik:GridBoundColumn HeaderText="lastname" HeaderButtonType="TextButton" DataField="LastName">

            </telerik:GridBoundColumn>

            <telerik:GridBoundColumn HeaderText="Title" HeaderButtonType="TextButton" DataField="Title">

            </telerik:GridBoundColumn>

              </columns>

            <EditFormSettings UserControlName="Contact.ascx" EditFormType="WebUserControl">

                <EditColumn UniqueName="EditCommandColumn1"></EditColumn>

                <PopUpSettings ScrollBars="None"></PopUpSettings>

            </EditFormSettings>

    </MasterTableView>

    </telerik:RadGrid>




the details are displayed using a user control which looks like this:


private object _dataItem = null;
public object DataItem
{
    get
    {
        return this._dataItem;
    }
    set
    {
        this._dataItem = value;
    }
}
  
</script>

<table id="OrgTable">
<tr><td><strong>Contact Details:</strong></td></tr>
<tr>
 <td>Contact ID</td>
    <td><asp:TextBox id="ID" runat="server" Text='<%# DataBinder.Eval( Container, "dataItem.ContactID" ) %>'>
    </asp:textbox></td>
    <td>Prefix</td>
    <td><asp:TextBox id="prefix" runat="server" Text='<%# DataBinder.Eval( Container, "dataItem.prefix" ) %>'>
    </asp:textbox></td>
    <td>First Name</td>
    <td><asp:TextBox id="firstname" runat="server" Text='<%# DataBinder.Eval( Container, "dataItem.FirstName" ) %>'>
    </asp:textbox></td>
    </tr>
    <tr>
     <td>Middle Name</td>
    <td><asp:TextBox id="middlename" runat="server" Text='<%# DataBinder.Eval( Container, "dataItem.MiddleName" ) %>'>
    </asp:Textbox></td>
     <td>Last Name</td>
    <td><asp:TextBox id="lastname" runat="server" Text='<%# DataBinder.Eval( Container, "dataItem.LastName" ) %>'>
    </asp:Textbox></td>
</tr>
<tr>
     <td>Title</td>
    <td><asp:TextBox id="title" runat="server" Text='<%# DataBinder.Eval( Container, "dataItem.Title" ) %>'>
    </asp:Textbox></td>
   
</tr>
<tr>
   <asp:Button CommandName="Update" runat="server" text="Update"  />
   
</tr>
</table>
 


I am not sure how I can update the table on clicking the update button. 


The code behind of the aspx has the following:


protected void RadGrid1_ItemUpdated(object source, GridCommandEventArgs e)
    {
        //Get the GridEditableItem of the RadGrid     
        GridEditableItem editedItem = (GridEditableItem)e.Item;  
        conn = new OleDbConnection(connectionstring);
        conn.Open();
        //Get the primary key value using the DataKeyValue.     
  
        
       // string ContactID = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["ContactID"].ToString();

        //Access the textbox from the edit form template and store the values in string variables.     
        
       string FirstName = (editedItem.FindControl("FirstName ") as TextBox).Text;
        string Title = (editedItem["Title"].Controls[0] as TextBox).Text;

        
            //Open the SqlConnection     
           
           
            string updateQuery = "something ";
        
         cmd = new OleDbCommand(updateQuery);
            cmd.ExecuteNonQuery();
          Close the SqlConnection     
            conn.Close();


        
    }
 


The variables Firstname and Title don't have the updated values. I dont think the event is triggered when I click on the update button either. Any help would be greatly appreciated.


Thanks,

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 30 Jun 2009, 03:58 AM
Hi,

Try performing the Update operation in the UpdateCommand event.  Refer the following links for getting more details about performing Insert/Update/Delete operation manually.
Manual Insert/Update/Delete using FormTemplate and Sql backend
Insert/Update/Delete at database level with queries

Thanks
Shinu
0
jlesidt
Top achievements
Rank 1
answered on 30 Jun 2009, 06:41 PM
Thanks. That worked.
Tags
Grid
Asked by
jlesidt
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
jlesidt
Top achievements
Rank 1
Share this question
or