Reputation: 388
I'm building a food ordering kinda web project. I add foods on page load with asp.net user controls and they all have unique ids. But I need to add foods to cart with jquery and display them right next to foods list in user controls. Now, I call a webmethod, and the method creates a dummy page, a form and adds my control, removes the form tag and returns the remainder to my ajax call and JS adds the contents to my page. But all my user controls are having the same ctl00_Panel1 id.
This is how I call my webmethod
$.ajax({
type: "POST",
url: "kategorilistesi.aspx/RenderControl",
data: "{id:'" + id + "',miktar:'" + miktar + "',baslik:'" + baslik + "',aciklama:'" + aciklama + "',tutar:'" + tutarf + "'}",
//data: "{controlName:'" + "~/UserControls/ksepetitem.ascx" + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$.unblockUI();
$('#sepetxy').append(response.d);
},
failure: function (msg) {
$.unblockUI();
alert(msg);
}
});
And this is my Webmethod
[WebMethod]
public static string RenderControl(int id, string miktar, string baslik, string aciklama, double tutar)
{
kategorilistesi m = new kategorilistesi();
string uid = Guid.NewGuid().ToString();
ArrayList cart = (ArrayList)m.Session["cart"];
Siparis n = new Siparis(id, Double.Parse(miktar), baslik, aciklama, tutar, uid);
cart.Add(n);
m.Session["cart"] = cart;
Page page = new Page();
UserControl userControl = (UserControl)page.LoadControl("~/UserControls/ksepetitem.ascx");
userControl.EnableViewState = true;
ksepetitem a = (ksepetitem)userControl;
a.YemekAdi = baslik;
a.YemekFiyat = tutar;
a.YemekMiktar = Double.Parse(miktar);
a.Guid = uid;
HtmlForm form = new HtmlForm();
form.Controls.Add(userControl);
page.Controls.Add(form);
StringWriter textWriter = new StringWriter();
HttpContext.Current.Server.Execute(page, textWriter, false);
return m.CleanHtml(textWriter.ToString());
}
how can I have unique id's for my controls? I'm using GUID's but I dont know how to add use them in this situation
Upvotes: 1
Views: 517
Reputation: 388
Turned out that I only needed to assign the control id myself with my GUID. Like this,
....YemekFiyat = tutar;
a.YemekMiktar = Double.Parse(miktar);
a.Guid = uid;
a.ID = uid;
HtmlForm form = new HtmlForm();
form.Controls.Add(userControl);
page.Controls.Add(form);......
Upvotes: 1