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

[Solved] Don't save data when I use EditFormType = WebUserControl

2 Answers 74 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sami
Top achievements
Rank 2
Iron
Iron
Iron
Sami asked on 19 Aug 2013, 10:14 PM
Hi, I have a problem when I use a WebUserControl and EditMode = PopUp to edit a record in the grid... when the popup is opened everything works fine, but when I click the update button, the record is not updated, and if I insert a new record, the record is inserted with all the fields empty, even if I fill all of them.

Could you please help me?

Greetings,
Sami

2 Answers, 1 is accepted

Sort by
0
Sami
Top achievements
Rank 2
Iron
Iron
Iron
answered on 20 Aug 2013, 12:27 AM
Hi, I forgot to tell you that I'm using EntityDataSource... how can I get the values from the UserControl to set them in the Entity in the "Updating" or "Inserting" events...?

Thanks...
Sami
0
Accepted
Princy
Top achievements
Rank 2
answered on 20 Aug 2013, 10:15 AM
Hi Sami,

Please try the below code snippet,it shows editforms with usercontrol.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" AllowSorting="True"
    AutoGenerateColumns="False" OnNeedDataSource="RadGrid1_NeedDataSource" OnUpdateCommand="RadGrid1_UpdateCommand"
    OnInsertCommand="RadGrid1_InsertCommand" OnDeleteCommand="RadGrid1_DeleteCommand">
    <MasterTableView CommandItemDisplay="Top" DataKeyNames="OrderID">
        <Columns>
            <telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
            </telerik:GridEditCommandColumn>
            <telerik:GridBoundColumn UniqueName="OrderID" HeaderText="OrderID" DataField="OrderID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="ShipName" HeaderText="ShipName" DataField="ShipName">
            </telerik:GridBoundColumn>
            <telerik:GridButtonColumn UniqueName="DeleteColumn" Text="Delete" CommandName="Delete">
            </telerik:GridButtonColumn>
        </Columns>
        <EditFormSettings UserControlName="EmployeeDetailsCS.ascx" EditFormType="WebUserControl">
            <EditColumn UniqueName="EditCommandColumn1">
            </EditColumn>
        </EditFormSettings>
    </MasterTableView>
</telerik:RadGrid>

C#:
 
protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
    this.RadGrid1.DataSource = this.Employees;
    this.Employees.PrimaryKey = new DataColumn[] { this.Employees.Columns["OrderID"] };
}
 
protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
{
    GridEditableItem editedItem = e.Item as GridEditableItem;
    UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
    //Prepare new row to add it in the DataSource
    DataRow[] changedRows = this.Employees.Select("OrderID = " + editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["OrderID"]);
 
    if (changedRows.Length != 1)
    {
        RadGrid1.Controls.Add(new LiteralControl("Unable to locate the Order for updating."));
        e.Canceled = true;
        return;
    }
 
    //Update new values
    Hashtable newValues = new Hashtable();
 
    newValues["OrderID"] = (userControl.FindControl("TextBox2") as TextBox).Text;
    newValues["ShipName"] = (userControl.FindControl("TextBox3") as TextBox).Text;  
 
    changedRows[0].BeginEdit();
    try
    {
        foreach (DictionaryEntry entry in newValues)
        {
            changedRows[0][(string)entry.Key] = entry.Value;
        }
        changedRows[0].EndEdit();
        this.Employees.AcceptChanges();
    }
    catch (Exception ex)
    {
        changedRows[0].CancelEdit();
 
        Label lblError = new Label();
        lblError.Text = "Unable to update Orders. Reason: " + ex.Message;
        lblError.ForeColor = System.Drawing.Color.Red;
        RadGrid1.Controls.Add(lblError);
        e.Canceled = true;
    }
}
 
protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
{
    UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
 
    //Create new row in the DataSource
    DataRow newRow = this.Employees.NewRow();
    //Insert new values
    Hashtable newValues = new Hashtable();
 
    newValues["OrderID"] = (userControl.FindControl("TextBox2") as TextBox).Text;
    newValues["ShipName"] = (userControl.FindControl("TextBox3") as TextBox).Text;      
 
    //make sure that unique primary key value is generated for the inserted row
    newValues["OrderID"] = (int)this.Employees.Rows[this.Employees.Rows.Count - 1]["OrderID"] + 1;
    try
    {
        foreach (DictionaryEntry entry in newValues)
        {
            newRow[(string)entry.Key] = entry.Value;
        }
        this.Employees.Rows.Add(newRow);
        this.Employees.AcceptChanges();
    }
    catch (Exception ex)
    {
        Label lblError = new Label();
        lblError.Text = "Unable to insert Orders. Reason: " + ex.Message;
        lblError.ForeColor = System.Drawing.Color.Red;
        RadGrid1.Controls.Add(lblError);
        e.Canceled = true;
    }
}
protected void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e)
{
    string ID = (e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["OrderID"].ToString();
    DataTable employeeTable = this.Employees;
    if (employeeTable.Rows.Find(ID) != null)
    {
        employeeTable.Rows.Find(ID).Delete();
        employeeTable.AcceptChanges();
    }
}

ASCX:
<table id="Table2" cellspacing="2" cellpadding="1" width="100%" border="1" rules="none"
style="border-collapse: collapse">
<tr class="EditFormHeader">
    <td colspan="2">
        <b>Order Details</b>
    </td>
</tr
<tr>
    <td>
        <table id="Table4" cellspacing="1" cellpadding="1" width="300" border="0">
            <tr>
                <td>
                    OrderID:
                </td>
                <td>
                    <asp:TextBox ID="TextBox2" Text='<%# DataBinder.Eval( Container, "DataItem.OrderID") %>'
                        runat="server" TabIndex="8">
                    </asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    ShipName:
                </td>
                <td>
                    <asp:TextBox ID="TextBox3" Text='<%# DataBinder.Eval( Container, "DataItem.ShipName") %>'
                        runat="server" TabIndex="9">
                    </asp:TextBox>
                </td>
            </tr>
        </table>
    </td>
    <td>
    </td>
</tr>
<tr>
    <td align="right" colspan="2">
        <asp:Button ID="btnUpdate" Text="Update" runat="server" CommandName="Update" Visible='<%# !(DataItem is Telerik.Web.UI.GridInsertionObject) %>'>
        </asp:Button>
        <asp:Button ID="btnInsert" Text="Insert" runat="server" CommandName="PerformInsert"
            Visible='<%# DataItem is Telerik.Web.UI.GridInsertionObject %>'></asp:Button>
          
        <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
            CommandName="Cancel"></asp:Button>
    </td>
</tr>
</table>

ASCX.CS:
private object _dataItem = null;
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{  
    InitializeComponent();
    base.OnInit(e);
}        
 
#endregion
public object DataItem
{
    get
    {
        return this._dataItem;
    }
    set
    {
        this._dataItem = value;
    }
}

Thanks,
Princy
Tags
Grid
Asked by
Sami
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Sami
Top achievements
Rank 2
Iron
Iron
Iron
Princy
Top achievements
Rank 2
Share this question
or