Hi
I want to pass these 2 fields. How i should pass in below code to User Control. Please help me
C#
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

Gowtham CpRank #1,552 · 1K reputationPosted Aug 28, 2024, 5:10 AM EDT
To pass parameters to a User Control in a concise way, you can encode them and include them directly in the HTML attributes. Here’s how you can do it:
1. Encode Parameters
Int32 recno = Convert.ToInt32(Common.CommonFunction.EncodePasswordToBase64(row["docno"].ToString())); string sp = Common.CommonFunction.EncodePasswordToBase64("sp_Inv"); // Encode parameters for safe use in JavaScript string recnoEncoded = HttpUtility.JavaScriptStringEncode(recno.ToString()); string spEncoded = HttpUtility.JavaScriptStringEncode(sp);2. Generate HTML with Parameters
string deleteAttributes = Status == "Blocked" ? "style='pointer-events: none; opacity: 0.5;' onclick='return false;'" : $"data-toggle='modal' data-target='#modal_Delete' onclick='BindData(this, {recnoEncoded}, \"{spEncoded}\");'"; htmlTable.Append($"");3. JavaScript Function
function BindData(element, recno, sp) { // Use recno and sp parameters as needed console.log("Record Number:", recno); console.log("Stored Procedure:", sp); // Your logic to handle the parameters }Explanation:
Encode the
recnoandspparameters for safe inclusion in JavaScript, and embed them directly into the HTML. TheBindDatafunction will then handle these parameters when the user interacts with the element.Jignesh KumarRank #91 · 22.9K reputationPosted Aug 28, 2024, 5:16 AM EDT
Hello Ramco,
You can do below change in htmlTable,
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);'"; // Generate HTML with hidden fields htmlTable.Append($@" ");Using Query string you can pass in jquery/Javascript,
function BindData(element) { var recno = element.getAttribute('data-recno'); var sp = element.getAttribute('data-sp'); // Redirect with query string window.location.href = `/YourController/YourAction?recno=${recno}&sp=${sp}`; }in you page :
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string recno = Request.QueryString["recno"]; string sp = Request.QueryString["sp"]; // Process the values as needed } }Ramco RamcoRank #462 · 5.4K reputationPosted Aug 28, 2024, 5:27 AM EDT
Hi Jignesh
I want to access these 2 values in User Control. I have made below changes in Html
string deleteAttributes = Status == "Blocked" ? "style='pointer-events: none; opacity: 0.5;' onclick='return false;'" : "data-toggle='modal' data-target='#modal_Delete' "; htmlTable.Append($"");Thanks
Jignesh KumarRank #91 · 22.9K reputationPosted Aug 28, 2024, 5:47 AM EDT
Hello Raco,
using javascript you can do that,
function BindData(element) { var recno = element.getAttribute('data-recno'); var sp = element.getAttribute('data-sp'); // Redirect with query string window.location.href = `/Mainpage.aspx?recno=${recno}&sp=${sp}`; }in your main page, Find User control and create SetData method and pass that values,
protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) return; string recno = Request.QueryString["recno"]; string sp = Request.QueryString["sp"]; // Assuming your user control is named MyUserControl if (recno != null && sp != null) { MyUserControl control = (MyUserControl)FindControl("MyUserControl"); if (control != null) { control.SetData(recno, sp); } } }In your user control,
public partial class MyUserControl : UserControl { public void SetData(string recno, string sp) { // Use recno and sp as needed // For example, bind data based on these values // Example: // int recordNumber = int.Parse(recno); // // Perform your data binding logic here } }Ramco RamcoRank #462 · 5.4K reputationPosted Aug 28, 2024, 5:16 AM EDT
Hi Gowtham
How.
Regards
Aman GuptaRank #193 · 12.4K reputationPosted Aug 28, 2024, 12:24 PM EDT
Hi Ramco,
To pass the two fields recno and sp to a User Control in your ASP.NET WebForm, you need to follow a few steps. Assuming you're trying to pass these values to a user control when the delete button is clicked, you'll typically use either a QueryString, Session, or ViewState depending on how you're handling the request.
1. Using QueryString (If the values are needed in a different page or user control):
You can append these values as query string parameters in the URL when you're invoking the user control or the page where the user control is included.
string url = $"YourPage.aspx?recno={recno}&sp={sp}"; htmlTable.Append($"");Then in the user control, you can retrieve these values:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { int recno = Convert.ToInt32(Request.QueryString["recno"]); string sp = Request.QueryString["sp"]; // Now you can use recno and sp } }2. Using Session (If you don't want to expose these values in the URL):
Store these values in a session before navigating to the user control.
Session["recno"] = recno; Session["sp"] = sp; htmlTable.Append($"");In the user control, retrieve these session values:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { int recno = (int)Session["recno"]; string sp = Session["sp"].ToString(); // Now you can use recno and sp } }3. Passing Values Directly to User Control:
If the user control is on the same page, you can pass these values directly as properties.
In the page where the control is used:
In the user control:
public int Recno { get; set; } public string Sp { get; set; } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // Now you can use Recno and Sp } }4. JavaScript (For Client-Side Handling):
If you want to handle this client-side using JavaScript and pass values back to the server later:
htmlTable.Append($"");Then in JavaScript, you can handle the click event:
function BindData(element) { var recno = $(element).data('recno'); var sp = $(element).data('sp'); // Use recno and sp as needed }Later, you can pass these values back to the server via an AJAX request or form submission.
Choosing the Right Method:
QueryString is simple but exposes data in the URL.
Session is secure but shared across the user's session.
Direct Properties is ideal when you are working within the same page.
JavaScript Data Attributes are useful when you need to handle data on the client side first.
Choose the method that best suits your application's architecture and security requirements.