asp.net - Dynamically create multiple ImageButton controls and wire-up Click event -
i'm pretty new asp.net. please forgive me knowledge :) assuming want create 4 imagebuttons. if click on imagebutton, move me page different stt (<- name). here's code:
(int i= 0; i< 4; i++) { imagebutton image = new imagebutton(); image.click += (s, args) => { response.redirect("~/showroom.aspx?stt=" + (i)); }; //other things }
now problem when click on imagebutton. i'll redirected showroom.aspx stt = 4 (which after loop). how can redirected page desired stt.
edit: clarify. want clicking on imagebutton 1 move me showroom.aspx stt = 0. imagebutton 2 move me page stt=1 , on.
problem
"~/showroom.aspx?stt=" + (i)
means captures variable i
rather value @ time of delegates creation.
solution
create url outside of delegate.
for (int = 0; < 3; i++) { string url = string.format("~/showroom.aspx?stt={0}", i); var image = new imagebutton(); image.click += (s, args) => response.redirect(url); }
Comments
Post a Comment