Hi
I have below code in Department.aspx. How i will pass Id & Table Name to Delete Modal which is User Control
C#
string deleteAttributes = Status == "Blocked" ? "style='pointer-events: none; opacity: 0.5;' onclick='return false;'" : "data-toggle='modal' data-target='#modal_Delete' onclick='BindData(this);'";
htmlTable.Append($"");Code
×
Cancel
DeleteThanks

Ramco RamcoRank #462 · 5.4K reputationPosted Aug 28, 2024, 12:24 AM EDT
Hi Naimish
How i cna directly pass Id & Table name to user control in below code
string deleteAttributes = Status == "Blocked" ? "style='pointer-events: none; opacity: 0.5;' onclick='return false;'" : $"data-toggle='modal' data-target='#modal_Delete' data-id='{id}' data-table='{tableName}' onclick='BindData(this);'"; htmlTable.Append($"");Thanks
Naimish MakwanaRank #324 · 7.7K reputationPosted Aug 28, 2024, 12:54 AM EDT
To pass the
IdandTable Namedirectly to a user control (e.g., the delete modal), you can modify the user control to accept these values via properties or methods. Here's how you can achieve that:Step 1: Modify the User Control
Assuming the modal is encapsulated in a user control (
DeleteModal.ascx), you can create public properties to accept theIdandTable Name:DeleteModal.ascx.cs (Code-Behind):
public partial class DeleteModal : System.Web.UI.UserControl { public string RecordId { get; set; } public string TableName { get; set; } protected void Page_Load(object sender, EventArgs e) { // You can now use RecordId and TableName in your modal } public void BindData(string id, string tableName) { RecordId = id; TableName = tableName; // Perform any additional data binding or preparation } }Step 2: Modify the Delete Button
Instead of using JavaScript, you can directly set the properties of the user control when generating the HTML:
Department.aspx.cs:
DeleteModal deleteModal = (DeleteModal)Page.LoadControl("~/Controls/DeleteModal.ascx"); string deleteAttributes = Status == "Blocked" ? "style='pointer-events: none; opacity: 0.5;' onclick='return false;'" : $"data-toggle='modal' data-target='#modal_Delete' onclick='BindData(\"{id}\", \"{tableName}\");'"; htmlTable.Append($"");Step 3: Update the BindData Method
Ensure that the
BindDatamethod in the user control is called to pass the values:Department.aspx.cs (Inside the Page_Load or any other event handler)
protected void BindData(string id, string tableName) { deleteModal.BindData(id, tableName); // Optionally show the modal here if you want to trigger it from server-side code }Step 4: Modify the User Control's Frontend
Ensure your user control is integrated with your modal setup:
DeleteModal.ascx:
Are You Sure, You want to Delete This ?? × " runat="server" /> " runat="server" /> Cancel DeleteStep 5: Trigger the Modal with Parameters
Make sure the modal is correctly linked to the properties you've set:
'>By using this approach, the
IdandTable Namewill be passed directly to your user control, and the modal will be prepared with the correct information without needing client-side scripts to pass the data.Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Aug 28, 2024, 12:59 AM EDT
Hi Naimish
Can the values be encrypted like below
AES.Encrypt(colum.UserID.ToString())
Thanks
Naimish MakwanaRank #324 · 7.7K reputationPosted Aug 28, 2024, 1:07 AM EDT
Yes, you can encrypt the values like
UserIdandTableNamebefore passing them to the user control or through HTML attributes. To achieve this, you would use AES encryption in your code. Here's how you can implement it:Step 1: Implement AES Encryption
First, implement an AES encryption method. Below is an example of how you can do this:
AES Encryption Helper Class:
using System; using System.IO; using System.Security.Cryptography; using System.Text; public static class AES { private static readonly string key = "your-encryption-key"; // Replace with your actual key private static readonly string iv = "your-encryption-iv"; // Replace with your actual IV public static string Encrypt(string plainText) { if (string.IsNullOrEmpty(plainText)) return null; using (Aes aesAlg = Aes.Create()) { aesAlg.Key = Encoding.UTF8.GetBytes(key); aesAlg.IV = Encoding.UTF8.GetBytes(iv); ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); using (MemoryStream msEncrypt = new MemoryStream()) { using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) { swEncrypt.Write(plainText); } return Convert.ToBase64String(msEncrypt.ToArray()); } } } } }Step 2: Encrypt Values in Your Code
Now, use this encryption method to encrypt the
UserIdandTableNamebefore passing them:string encryptedUserId = AES.Encrypt(column.UserId.ToString()); string encryptedTableName = AES.Encrypt(tableName); string deleteAttributes = Status == "Blocked" ? "style='pointer-events: none; opacity: 0.5;' onclick='return false;'" : $"data-toggle='modal' data-target='#modal_Delete' data-id='{encryptedUserId}' data-table='{encryptedTableName}' onclick='BindData(this);'"; htmlTable.Append($"");Step 3: Update BindData Function to Decrypt (if necessary)
If you need to decrypt these values on the client side, you would typically pass them back to the server where they would be decrypted. For example, if you want to bind data in your server-side method, you can decrypt the values as follows:
BindData Method:
protected void BindData(string encryptedId, string encryptedTableName) { string userId = AES.Decrypt(encryptedId); // Implement Decrypt method similarly to Encrypt string tableName = AES.Decrypt(encryptedTableName); deleteModal.BindData(userId, tableName); // Optionally show the modal here if you want to trigger it from server-side code }Step 4: Ensure the Decrypt Method Exists
Just as you encrypt the values, ensure you have a corresponding
Decryptmethod in theAESclass:public static string Decrypt(string cipherText) { if (string.IsNullOrEmpty(cipherText)) return null; using (Aes aesAlg = Aes.Create()) { aesAlg.Key = Encoding.UTF8.GetBytes(key); aesAlg.IV = Encoding.UTF8.GetBytes(iv); ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); using (MemoryStream msDecrypt = new MemoryStream(Convert.FromBase64String(cipherText))) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (StreamReader srDecrypt = new StreamReader(csDecrypt)) { return srDecrypt.ReadToEnd(); } } } } }With this setup, your
UserIdandTableNamewill be encrypted before being passed to the modal, and you can decrypt them on the server side if necessary. This adds a layer of security to the data being passed in the HTML.Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Aug 28, 2024, 3:39 AM EDT
Hi Naimish
I want to pass these 2 fields. How i should pass in below code to User Control. Please help me
Int32 recno = Convert.ToInt32(Common.CommonFunction.EncodePasswordToBase64(row["docno"].ToString()));
string sp = Common.CommonFunction.EncodePasswordToBase64("sp_Inv");
string deleteAttributes = Status == "Blocked" ? "style='pointer-events: none; opacity: 0.5;' onclick='return false;'" : "data-toggle='modal' data-target='#modal_Delete' onclick='BindData(this);'"; htmlTable.Append($"");Thanks
Naimish MakwanaRank #324 · 7.7K reputationPosted Aug 28, 2024, 12:18 AM EDT
To pass the
IdandTable Nameto the delete modal, you can use data attributes to store these values and then retrieve them in the JavaScript functionBindData. Here’s how you can modify your code:1. Modify the
deleteAttributesstring to include data attributes forIdandTable Name:string deleteAttributes = Status == "Blocked" ? "style='pointer-events: none; opacity: 0.5;' onclick='return false;'" : $"data-toggle='modal' data-target='#modal_Delete' data-id='{id}' data-table='{tableName}' onclick='BindData(this);'"; htmlTable.Append($"");2. Update the
BindDataJavaScript function to retrieve these data attributes and set them in the modal:function BindData(element) { var id = $(element).data('id'); var tableName = $(element).data('table'); // Set these values in hidden fields or directly in the modal if needed $('#hiddenIdField').val(id); $('#hiddenTableField').val(tableName); // Optionally, you can display these values in the modal $('#modal_Delete .modal-body').text(`Are you sure you want to delete the record with Id: ${id} from table: ${tableName}?`); }3. Add hidden fields in the modal to store these values if needed:
4. Update the
lnkRecordDelete_Clickevent handler in your code-behind to use these hidden fields:protected void lnkRecordDelete_Click(object sender, EventArgs e) { string id = hiddenIdField.Value; string tableName = hiddenTableField.Value; // Your delete logic here }This approach ensures that the
IdandTable Nameare passed to the modal and can be used in your delete logic.Thanks